/// <summary> /// Converts the specified <paramref name="value"/> into its raw format and writes it to the output. /// The value has to be of enumeration or primitive type. Only one WriteRawValue call should be made before this object gets disposed. /// </summary> /// <param name="value">The (non-binary) value to write.</param> /// <remarks>We do not accept binary values here; WriteBinaryValue should be used for binary data.</remarks> internal void WriteRawValue(object value) { Debug.Assert(!(value is byte[]), "!(value is byte[])"); string valueAsString; ODataEnumValue enumValue = value as ODataEnumValue; if (enumValue != null) { this.textWriter.Write(enumValue.Value); } else if (value is Geometry || value is Geography) { PrimitiveConverter.Instance.WriteJsonLight(value, jsonWriter); } else if (ODataRawValueUtils.TryConvertPrimitiveToString(value, out valueAsString)) { this.textWriter.Write(valueAsString); } else { // throw an exception because the value is neither enum nor primitive throw new ODataException(Strings.ODataUtils_CannotConvertValueToRawString(value.GetType().FullName)); } }
/// <summary> /// Asynchronously reads the content of a text reader as string and, if <paramref name="expectedPrimitiveTypeReference"/> is specified and primitive type conversion /// is enabled, converts the string to the expected type. /// </summary> /// <param name="expectedPrimitiveTypeReference">The expected type of the value being read or null if no type conversion should be performed.</param> /// <returns> /// A task that represents the asynchronous read operation. /// The value of the TResult parameter contains the raw value that was read from the text reader /// either as string or converted to the provided <paramref name="expectedPrimitiveTypeReference"/>. /// </returns> private async Task <object> ReadRawValueAsync(IEdmPrimitiveTypeReference expectedPrimitiveTypeReference) { string stringFromStream = await this.textReader.ReadToEndAsync() .ConfigureAwait(false); if (expectedPrimitiveTypeReference != null && this.MessageReaderSettings.EnablePrimitiveTypeConversion) { return(ODataRawValueUtils.ConvertStringToPrimitive(stringFromStream, expectedPrimitiveTypeReference)); } else { return(stringFromStream); } }
/// <summary> /// Reads the content of a text reader as string and, if <paramref name="expectedPrimitiveTypeReference"/> is specified and primitive type conversion /// is enabled, converts the string to the expected type. /// </summary> /// <param name="expectedPrimitiveTypeReference">The expected type of the value being read or null if no type conversion should be performed.</param> /// <returns>The raw value that was read from the text reader either as string or converted to the provided <paramref name="expectedPrimitiveTypeReference"/>.</returns> private object ReadRawValue(IEdmPrimitiveTypeReference expectedPrimitiveTypeReference) { string stringFromStream = this.textReader.ReadToEnd(); object rawValue; if (expectedPrimitiveTypeReference != null && this.MessageReaderSettings.EnablePrimitiveTypeConversion) { rawValue = ODataRawValueUtils.ConvertStringToPrimitive(stringFromStream, expectedPrimitiveTypeReference); } else { rawValue = stringFromStream; } return(rawValue); }
/// <summary> /// Asynchronously converts the specified <paramref name="value"/> into its raw format and writes it to the output. /// The value has to be of enumeration or primitive type. Only one WriteRawValue call should be made before this object gets disposed. /// </summary> /// <param name="value">The (non-binary) value to write.</param> /// <returns>A task that represents the asynchronous write operation.</returns> /// <remarks>We do not accept binary values here; WriteBinaryValue should be used for binary data.</remarks> /// <returns>A task that represents the asynchronous write operation.</returns> internal Task WriteRawValueAsync(object value) { Debug.Assert(!(value is byte[]), "!(value is byte[])"); if (value is ODataEnumValue enumValue) { return(this.textWriter.WriteAsync(enumValue.Value)); } if (value is Geometry || value is Geography) { return(TaskUtils.GetTaskForSynchronousOperation( () => PrimitiveConverter.Instance.WriteJsonLight(value, jsonWriter))); } if (ODataRawValueUtils.TryConvertPrimitiveToString(value, out string valueAsString)) { return(this.textWriter.WriteAsync(valueAsString)); } // Value is neither enum nor primitive return(TaskUtils.GetFaultedTask( new ODataException(Strings.ODataUtils_CannotConvertValueToRawString(value.GetType().FullName)))); }