/// <summary> /// Write a literal value in JSON Light format. /// </summary> /// <param name="model">EDM Model to use for validation and type lookups.</param> /// <param name="messageWriterSettings">Settings to use when writing.</param> /// <param name="textWriter">TextWriter to use as the output for the value.</param> /// <param name="writeValue">Delegate to use to actually write the value.</param> private static void WriteJsonLightLiteral(IEdmModel model, ODataMessageWriterSettings messageWriterSettings, TextWriter textWriter, Action <ODataJsonLightValueSerializer> writeValue) { IEnumerable <KeyValuePair <string, string> > parameters = new Dictionary <string, string> { { MimeConstants.MimeIeee754CompatibleParameterName, messageWriterSettings.IsIeee754Compatible.ToString() } }; ODataMediaType mediaType = new ODataMediaType(MimeConstants.MimeApplicationType, MimeConstants.MimeJsonSubType, parameters); // Calling dispose since it's the right thing to do, but when created from a custom-built TextWriter // the output context Dispose will not actually dispose anything, it will just cleanup itself. // TODO: URI parser will also support DI container in the future but set the container to null at this moment. ODataMessageInfo messageInfo = new ODataMessageInfo { Model = model, IsAsync = false, IsResponse = false, MediaType = mediaType }; using (ODataJsonLightOutputContext jsonOutputContext = new ODataJsonLightOutputContext(textWriter, messageInfo, messageWriterSettings)) { ODataJsonLightValueSerializer jsonLightValueSerializer = new ODataJsonLightValueSerializer(jsonOutputContext); writeValue(jsonLightValueSerializer); jsonLightValueSerializer.AssertRecursionDepthIsZero(); } }
private ODataJsonLightValueSerializer CreateODataJsonLightValueSerializer(bool writingResponse) { var context = new ODataJsonLightOutputContext(ODataFormat.Json, stream, new ODataMediaType("application", "json"), Encoding.Default, settings, writingResponse, true, model, null); var serializer = new ODataJsonLightValueSerializer(context); return(serializer); }
private object WriteThenReadValue(object clrValue, IEdmTypeReference typeReference, ODataVersion version, bool isIeee754Compatible) { MemoryStream stream = new MemoryStream(); ODataMessageWriterSettings settings = new ODataMessageWriterSettings { Version = version }; settings.SetServiceDocumentUri(new Uri("http://odata.org/test/")); ODataMediaType mediaType = isIeee754Compatible ? new ODataMediaType("application", "json", new KeyValuePair <string, string>("IEEE754Compatible", "true")) : new ODataMediaType("application", "json"); using (ODataJsonLightOutputContext outputContext = new ODataJsonLightOutputContext( ODataFormat.Json, new NonDisposingStream(stream), mediaType, Encoding.UTF8, settings, /*writingResponse*/ true, /*synchronous*/ true, this.model, /*urlResolver*/ null)) { ODataJsonLightValueSerializer serializer = new ODataJsonLightValueSerializer(outputContext); serializer.WritePrimitiveValue(clrValue, typeReference); } stream.Position = 0; object actualValue; using (ODataJsonLightInputContext inputContext = new ODataJsonLightInputContext( ODataFormat.Json, stream, mediaType, Encoding.UTF8, new ODataMessageReaderSettings(), /*readingResponse*/ true, /*synchronous*/ true, this.model, /*urlResolver*/ null)) { ODataJsonLightPropertyAndValueDeserializer deserializer = new ODataJsonLightPropertyAndValueDeserializer(inputContext); deserializer.JsonReader.Read(); actualValue = deserializer.ReadNonEntityValue( /*payloadTypeName*/ null, typeReference, /*duplicatePropertyNamesChecker*/ null, /*collectionValidator*/ null, /*validateNullValue*/ true, /*isTopLevel*/ true, /*insideComplexValue*/ false, /*propertyName*/ null); } return(actualValue); }
/// <summary> /// Constructs a <see cref="JsonLightInstanceAnnotationWriter"/> that can write a collection of <see cref="ODataInstanceAnnotation"/>. /// </summary> /// <param name="valueSerializer">The <see cref="ODataJsonLightValueSerializer"/> to use for writing values of instance annotations. /// The <see cref="IJsonWriter"/> that is also used internally will be acquired from the this instance.</param> /// <param name="typeNameOracle">The oracle to use to determine the type name to write for entries and values.</param> internal JsonLightInstanceAnnotationWriter(ODataJsonLightValueSerializer valueSerializer, JsonLightTypeNameOracle typeNameOracle) { Debug.Assert(valueSerializer != null, "valueSerializer should not be null"); this.valueSerializer = valueSerializer; this.typeNameOracle = typeNameOracle; this.jsonWriter = this.valueSerializer.JsonWriter; this.odataAnnotationWriter = new JsonLightODataAnnotationWriter(this.jsonWriter, valueSerializer.JsonLightOutputContext.ODataSimplifiedOptions.EnableWritingODataAnnotationWithoutPrefix, this.valueSerializer.MessageWriterSettings.Version); this.writerValidator = this.valueSerializer.MessageWriterSettings.Validator; }
/// <summary> /// Constructs a <see cref="JsonLightInstanceAnnotationWriter"/> that can write a collection of <see cref="ODataInstanceAnnotation"/>. /// </summary> /// <param name="valueSerializer">The <see cref="ODataJsonLightValueSerializer"/> to use for writing values of instance annotations. /// The <see cref="IJsonWriter"/> that is also used internally will be acquired from the this instance.</param> /// <param name="typeNameOracle">The oracle to use to determine the type name to write for entries and values.</param> internal JsonLightInstanceAnnotationWriter(ODataJsonLightValueSerializer valueSerializer, JsonLightTypeNameOracle typeNameOracle) { Debug.Assert(valueSerializer != null, "valueSerializer should not be null"); this.valueSerializer = valueSerializer; this.typeNameOracle = typeNameOracle; this.jsonWriter = this.valueSerializer.JsonWriter; this.odataAnnotationWriter = this.valueSerializer.ODataAnnotationWriter; this.asynchronousJsonWriter = this.valueSerializer.AsynchronousJsonWriter; this.asynchronousODataAnnotationWriter = this.valueSerializer.AsynchronousODataAnnotationWriter; this.writerValidator = this.valueSerializer.MessageWriterSettings.Validator; }
/// <summary> /// Write a literal value in JSON Light format. /// </summary> /// <param name="model">EDM Model to use for validation and type lookups.</param> /// <param name="messageWriterSettings">Settings to use when writing.</param> /// <param name="textWriter">TextWriter to use as the output for the value.</param> /// <param name="writeValue">Delegate to use to actually write the value.</param> private static void WriteJsonLightLiteral(IEdmModel model, ODataMessageWriterSettings messageWriterSettings, TextWriter textWriter, Action <ODataJsonLightValueSerializer> writeValue) { // Calling dispose since it's the right thing to do, but when created from a custom-built TextWriter // the output context Dispose will not actually dispose anything, it will just cleanup itself. using (ODataJsonLightOutputContext jsonOutputContext = new ODataJsonLightOutputContext(ODataFormat.Json, textWriter, messageWriterSettings, model)) { ODataJsonLightValueSerializer jsonLightValueSerializer = new ODataJsonLightValueSerializer(jsonOutputContext); writeValue(jsonLightValueSerializer); jsonLightValueSerializer.AssertRecursionDepthIsZero(); } }
/// <summary> /// Write a literal value in JSON Light format. /// </summary> /// <param name="model">EDM Model to use for validation and type lookups.</param> /// <param name="messageWriterSettings">Settings to use when writing.</param> /// <param name="textWriter">TextWriter to use as the output for the value.</param> /// <param name="writeValue">Delegate to use to actually write the value.</param> private static void WriteJsonLightLiteral(IEdmModel model, ODataMessageWriterSettings messageWriterSettings, TextWriter textWriter, Action <ODataJsonLightValueSerializer> writeValue) { // Calling dispose since it's the right thing to do, but when created from a custom-built TextWriter // the output context Dispose will not actually dispose anything, it will just cleanup itself. // TODO: URI parser will also support DI container in the future but set the container to null at this moment. ODataMessageInfo messageInfo = new ODataMessageInfo { Model = model, IsAsync = false, IsResponse = false }; using (ODataJsonLightOutputContext jsonOutputContext = new ODataJsonLightOutputContext(textWriter, messageInfo, messageWriterSettings)) { ODataJsonLightValueSerializer jsonLightValueSerializer = new ODataJsonLightValueSerializer(jsonOutputContext); writeValue(jsonLightValueSerializer); jsonLightValueSerializer.AssertRecursionDepthIsZero(); } }
private ODataJsonLightValueSerializer CreateODataJsonLightValueSerializer(bool writingResponse, IServiceProvider container = null) { var messageInfo = new ODataMessageInfo { MessageStream = stream, MediaType = new ODataMediaType("application", "json"), #if NETCOREAPP1_1 Encoding = Encoding.GetEncoding(0), #else Encoding = Encoding.Default, #endif IsResponse = writingResponse, IsAsync = false, Model = model, Container = container }; var context = new ODataJsonLightOutputContext(messageInfo, settings); var serializer = new ODataJsonLightValueSerializer(context); return(serializer); }
private JsonLightInstanceAnnotationWriter CreateJsonLightInstanceAnnotationWriter(bool writingResponse, IServiceProvider container = null, bool isAsync = false) { var messageInfo = new ODataMessageInfo { MessageStream = this.stream, MediaType = new ODataMediaType("application", "json"), #if NETCOREAPP1_1 Encoding = Encoding.GetEncoding(0), #else Encoding = Encoding.Default, #endif IsResponse = writingResponse, IsAsync = isAsync, Model = model, Container = container }; var context = new ODataJsonLightOutputContext(messageInfo, this.settings); this.jsonLightValueSerializer = new ODataJsonLightValueSerializer(context); return(new JsonLightInstanceAnnotationWriter(this.jsonLightValueSerializer, new JsonMinimalMetadataTypeNameOracle())); }
private static string WriteInstanceAnnotation(ODataInstanceAnnotation instanceAnnotation, IEdmModel model) { var stringWriter = new StringWriter(); var outputContext = new ODataJsonLightOutputContext( ODataFormat.Json, stringWriter, new ODataMessageWriterSettings { Version = ODataVersion.V4, ShouldIncludeAnnotation = ODataUtils.CreateAnnotationFilter("*") }, model); var valueSerializer = new ODataJsonLightValueSerializer(outputContext); // The JSON Writer will complain if there is no active scope, so start an object scope. valueSerializer.JsonWriter.StartObjectScope(); var instanceAnnotationWriter = new JsonLightInstanceAnnotationWriter(valueSerializer, new JsonMinimalMetadataTypeNameOracle()); // The method under test. instanceAnnotationWriter.WriteInstanceAnnotation(instanceAnnotation); valueSerializer.JsonWriter.EndObjectScope(); return(stringWriter.ToString()); }
private object WriteThenReadValue(object clrValue, IEdmTypeReference typeReference, ODataVersion version, bool isIeee754Compatible) { var stream = new MemoryStream(); var settings = new ODataMessageWriterSettings { Version = version }; settings.SetServiceDocumentUri(new Uri("http://odata.org/test/")); var mediaType = isIeee754Compatible ? new ODataMediaType("application", "json", new KeyValuePair <string, string>("IEEE754Compatible", "true")) : new ODataMediaType("application", "json"); var messageInfoForWriter = new ODataMessageInfo { MessageStream = new NonDisposingStream(stream), MediaType = mediaType, Encoding = Encoding.UTF8, IsResponse = true, IsAsync = false, Model = this.model, Container = this.container }; using (var outputContext = new ODataJsonLightOutputContext(messageInfoForWriter, settings)) { var serializer = new ODataJsonLightValueSerializer(outputContext); serializer.WritePrimitiveValue(clrValue, typeReference); } stream.Position = 0; var messageInfoForReader = new ODataMessageInfo { Encoding = Encoding.UTF8, IsResponse = true, MediaType = mediaType, IsAsync = false, Model = this.model, MessageStream = stream, Container = this.container }; object actualValue; using (var inputContext = new ODataJsonLightInputContext( messageInfoForReader, new ODataMessageReaderSettings())) { var deserializer = new ODataJsonLightPropertyAndValueDeserializer(inputContext); deserializer.JsonReader.Read(); actualValue = deserializer.ReadNonEntityValue( /*payloadTypeName*/ null, typeReference, /*propertyAndAnnotationCollector*/ null, /*collectionValidator*/ null, /*validateNullValue*/ true, /*isTopLevel*/ true, /*insideResourceValue*/ false, /*propertyName*/ null); } return(actualValue); }
private object WriteAsUntypedThenReadValue(string value, IEdmTypeReference typeReference, ODataVersion version) { var stream = new MemoryStream(); var settings = new ODataMessageWriterSettings { Version = version }; settings.SetServiceDocumentUri(new Uri("http://tempuri.org/")); var mediaType = new ODataMediaType("application", "json"); var messageInfoForWriter = new ODataMessageInfo { MessageStream = new NonDisposingStream(stream), MediaType = mediaType, Encoding = Encoding.UTF8, IsResponse = true, IsAsync = false, Model = this.model, Container = this.container }; using (var outputContext = new ODataJsonLightOutputContext(messageInfoForWriter, settings)) { var serializer = new ODataJsonLightValueSerializer(outputContext); // Writing the value as untyped it remains in its original form serializer.WriteUntypedValue(new ODataUntypedValue { RawValue = value }); } stream.Position = 0; var messageInfoForReader = new ODataMessageInfo { Encoding = Encoding.UTF8, IsResponse = true, MediaType = mediaType, IsAsync = false, Model = this.model, MessageStream = stream, Container = this.container }; object actualValue; using (var inputContext = new ODataJsonLightInputContext( messageInfoForReader, new ODataMessageReaderSettings())) { var deserializer = new ODataJsonLightPropertyAndValueDeserializer(inputContext); deserializer.JsonReader.Read(); actualValue = deserializer.ReadNonEntityValue( /*payloadTypeName*/ null, typeReference, /*propertyAndAnnotationCollector*/ null, /*collectionValidator*/ null, /*validateNullValue*/ true, /*isTopLevel*/ true, /*insideResourceValue*/ false, /*propertyName*/ null); } return(actualValue); }