public virtual void WriteStreamValue(ODataBinaryStreamValue streamValue) { IJsonStreamWriter streamWriter = this.JsonWriter as IJsonStreamWriter; if (streamWriter == null) { // write as a string this.JsonWriter.WritePrimitiveValue(new StreamReader(streamValue.Stream).ReadToEnd()); } else { Stream stream = streamWriter.StartStreamValueScope(); streamValue.Stream.CopyTo(stream); stream.Flush(); stream.Dispose(); streamWriter.EndStreamValueScope(); } }
public async Task WriteStreamValueAsync_WritesStreamValue() { var stream = new MemoryStream(); var writer = new BinaryWriter(stream); writer.Write("1234567890"); writer.Flush(); stream.Position = 0; var streamValue = new ODataBinaryStreamValue(stream); var result = await SetupJsonLightValueSerializerAndRunTestAsync( (jsonLightValueSerializer) => { return(jsonLightValueSerializer.WriteStreamValueAsync(streamValue)); }); Assert.Equal("\"CjEyMzQ1Njc4OTA=\"", result); }
/// <summary> /// Asynchronously writes a binary stream value. /// </summary> /// <param name="streamValue">The binary stream value.</param> /// <returns>A task that represents the asynchronous write operation.</returns> public virtual async Task WriteStreamValueAsync(ODataBinaryStreamValue streamValue) { IJsonStreamWriterAsync streamWriter = this.AsynchronousJsonWriter as IJsonStreamWriterAsync; if (streamWriter == null) { // write as a string string value = await new StreamReader(streamValue.Stream).ReadToEndAsync().ConfigureAwait(false); await this.AsynchronousJsonWriter.WritePrimitiveValueAsync(value).ConfigureAwait(false); } else { Stream stream = await streamWriter.StartStreamValueScopeAsync().ConfigureAwait(false); await streamValue.Stream.CopyToAsync(stream).ConfigureAwait(false); await stream.FlushAsync().ConfigureAwait(false); stream.Dispose(); await streamWriter.EndStreamValueScopeAsync().ConfigureAwait(false); } }
/// <summary> /// Writes a stream property. /// </summary> /// <param name="streamValue">The stream value to be written</param> /// <param name="isOpenPropertyType">If the property is open.</param> private void WriteStreamProperty(ODataBinaryStreamValue streamValue, bool isOpenPropertyType) { this.JsonWriter.WriteName(this.currentPropertyInfo.WireName); this.JsonLightValueSerializer.WriteStreamValue(streamValue); }
internal void WriteProperty( ODataProperty property, IEdmStructuredType owningType, bool isTopLevel, IDuplicatePropertyNameChecker duplicatePropertyNameChecker, ODataResourceMetadataBuilder metadataBuilder) { this.WritePropertyInfo(property, owningType, isTopLevel, duplicatePropertyNameChecker, metadataBuilder); ODataValue value = property.ODataValue; // handle ODataUntypedValue ODataUntypedValue untypedValue = value as ODataUntypedValue; if (untypedValue != null) { WriteUntypedValue(untypedValue); return; } ODataStreamReferenceValue streamReferenceValue = value as ODataStreamReferenceValue; if (streamReferenceValue != null && !(this.JsonLightOutputContext.MetadataLevel is JsonNoMetadataLevel)) { Debug.Assert(!isTopLevel, "Stream properties are not allowed at the top level."); WriteStreamValue(streamReferenceValue, property.Name, metadataBuilder); return; } if (value is ODataNullValue || value == null) { this.WriteNullProperty(property); return; } bool isOpenPropertyType = this.IsOpenProperty(property); ODataPrimitiveValue primitiveValue = value as ODataPrimitiveValue; if (primitiveValue != null) { this.WritePrimitiveProperty(primitiveValue, isOpenPropertyType); return; } ODataEnumValue enumValue = value as ODataEnumValue; if (enumValue != null) { this.WriteEnumProperty(enumValue, isOpenPropertyType); return; } ODataResourceValue resourceValue = value as ODataResourceValue; if (resourceValue != null) { if (isTopLevel) { throw new ODataException(Strings.ODataMessageWriter_NotAllowedWriteTopLevelPropertyWithResourceValue(property.Name)); } this.WriteResourceProperty(property, resourceValue, isOpenPropertyType); return; } ODataCollectionValue collectionValue = value as ODataCollectionValue; if (collectionValue != null) { if (isTopLevel) { if (collectionValue.Items != null && collectionValue.Items.Any(i => i is ODataResourceValue)) { throw new ODataException(Strings.ODataMessageWriter_NotAllowedWriteTopLevelPropertyWithResourceValue(property.Name)); } } this.WriteCollectionProperty(collectionValue, isOpenPropertyType); return; } ODataBinaryStreamValue streamValue = value as ODataBinaryStreamValue; if (streamValue != null) { this.WriteStreamProperty(streamValue, isOpenPropertyType); return; } }