private ODataResource ReadSingleton(string payload, bool odataSimplified = false) { var settings = new ODataMessageReaderSettings(); var messageInfo = new ODataMessageInfo { IsResponse = true, MediaType = new ODataMediaType("application", "json"), IsAsync = false, Model = this.userModel, Container = ContainerBuilderHelper.BuildContainer(null) }; using (var inputContext = new ODataJsonLightInputContext( new StringReader(payload), messageInfo, settings)) { inputContext.Container.GetRequiredService <ODataSimplifiedOptions>() .EnableReadingODataAnnotationWithoutPrefix = odataSimplified; var jsonLightReader = new ODataJsonLightReader(inputContext, singleton, webType, /*readingFeed*/ false); while (jsonLightReader.Read()) { if (jsonLightReader.State == ODataReaderState.ResourceEnd) { ODataResource entry = jsonLightReader.Item as ODataResource; return(entry); } } } return(null); }
private IEnumerable <Tuple <ODataItem, ODataDeltaReaderState, ODataReaderState> > ReadItem(string payload, IEdmModel model = null, IEdmNavigationSource navigationSource = null, IEdmEntityType entityType = null, bool odataSimplified = false) { MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(payload)); ODataMessageReaderSettings settings = new ODataMessageReaderSettings(); settings.ShouldIncludeAnnotation = s => true; settings.ODataSimplified = odataSimplified; using (var inputContext = new ODataJsonLightInputContext( ODataFormat.Json, stream, new ODataMediaType("application", "json"), Encoding.UTF8, settings, /*readingResponse*/ true, /*synchronous*/ true, model ?? new EdmModel(), /*urlResolver*/ null)) { var jsonLightReader = new ODataJsonLightDeltaReader(inputContext, navigationSource, entityType); while (jsonLightReader.Read()) { yield return(new Tuple <ODataItem, ODataDeltaReaderState, ODataReaderState>(jsonLightReader.Item, jsonLightReader.State, jsonLightReader.SubState)); } } }
private ODataNavigationLink ReadSingletonNavigationLink(string payload) { MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(payload)); ODataMessageReaderSettings settings = new ODataMessageReaderSettings(); using (ODataJsonLightInputContext inputContext = new ODataJsonLightInputContext( ODataFormat.Json, stream, new ODataMediaType("application", "json"), Encoding.UTF8, settings, /*readingResponse*/ true, /*synchronous*/ true, this.userModel, /*urlResolver*/ null)) { var jsonLightReader = new ODataJsonLightReader(inputContext, singleton, webType, /*readingFeed*/ false); while (jsonLightReader.Read()) { if (jsonLightReader.State == ODataReaderState.NavigationLinkEnd) { ODataNavigationLink navigationLink = jsonLightReader.Item as ODataNavigationLink; return(navigationLink); } } } return(null); }
private IEnumerable <Tuple <ODataItem, ODataDeltaReaderState, ODataReaderState> > ReadItem(string payload, IEdmModel model = null, IEdmNavigationSource navigationSource = null, IEdmEntityType entityType = null, bool enableReadingODataAnnotationWithoutPrefix = false) { var settings = new ODataMessageReaderSettings { ShouldIncludeAnnotation = s => true, }; var messageInfo = new ODataMessageInfo { IsResponse = true, MediaType = new ODataMediaType("application", "json"), IsAsync = false, Model = model ?? new EdmModel(), Container = ContainerBuilderHelper.BuildContainer(null) }; using (var inputContext = new ODataJsonLightInputContext( new StringReader(payload), messageInfo, settings)) { inputContext.Container.GetRequiredService <ODataSimplifiedOptions>() .EnableReadingODataAnnotationWithoutPrefix = enableReadingODataAnnotationWithoutPrefix; var jsonLightReader = new ODataJsonLightDeltaReader(inputContext, navigationSource, entityType); while (jsonLightReader.Read()) { yield return(new Tuple <ODataItem, ODataDeltaReaderState, ODataReaderState>(jsonLightReader.Item, jsonLightReader.State, jsonLightReader.SubState)); } } }
private void VerifyDateValueReader(string payload, string edmTypeName, object expectedResult) { IEdmModel model = new EdmModel(); IEdmPrimitiveTypeReference typeReference = new EdmPrimitiveTypeReference((IEdmPrimitiveType)model.FindType(edmTypeName), true); MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(payload)); object actualValue; using (ODataJsonLightInputContext inputContext = new ODataJsonLightInputContext( ODataFormat.Json, stream, JsonLightUtils.JsonLightStreamingMediaType, Encoding.UTF8, new ODataMessageReaderSettings(), /*readingResponse*/ true, /*synchronous*/ true, model, /*urlResolver*/ null)) { ODataJsonLightPropertyAndValueDeserializer deserializer = new ODataJsonLightPropertyAndValueDeserializer(inputContext); deserializer.JsonReader.Read(); actualValue = deserializer.ReadNonEntityValue( /*payloadTypeName*/ null, typeReference, /*duplicatePropertyNamesChecker*/ null, /*collectionValidator*/ null, /*validateNullValue*/ true, /*isTopLevelPropertyValue*/ true, /*insideComplexValue*/ false, /*propertyName*/ null); } actualValue.Should().Be(expectedResult, "payload ->{0}<- for type '{1}'", payload, edmTypeName); }
private void VerifyDateValueReader(string payload, string edmTypeName, object expectedResult) { IEdmModel model = new EdmModel(); IEdmPrimitiveTypeReference typeReference = new EdmPrimitiveTypeReference((IEdmPrimitiveType)model.FindType(edmTypeName), true); var messageInfo = new ODataMessageInfo { IsResponse = true, MediaType = JsonLightUtils.JsonLightStreamingMediaType, IsAsync = false, Model = new EdmModel(), }; object actualValue; using (var inputContext = new ODataJsonLightInputContext( new StringReader(payload), messageInfo, new ODataMessageReaderSettings())) { var deserializer = new ODataJsonLightPropertyAndValueDeserializer(inputContext); deserializer.JsonReader.Read(); actualValue = deserializer.ReadNonEntityValue( /*payloadTypeName*/ null, typeReference, /*propertyAndAnnotationCollector*/ null, /*collectionValidator*/ null, /*validateNullValue*/ true, /*isTopLevelPropertyValue*/ true, /*insideResourceValue*/ false, /*propertyName*/ null); } actualValue.Should().Be(expectedResult, "payload ->{0}<- for type '{1}'", payload, edmTypeName); }
private ODataNestedResourceInfo ReadSingletonNavigationLink(string payload) { var messageInfo = new ODataMessageInfo { IsResponse = true, MediaType = new ODataMediaType("application", "json"), IsAsync = false, Model = this.userModel, }; using (var inputContext = new ODataJsonLightInputContext( new StringReader(payload), messageInfo, new ODataMessageReaderSettings())) { var jsonLightReader = new ODataJsonLightReader(inputContext, singleton, webType, /*readingFeed*/ false); while (jsonLightReader.Read()) { if (jsonLightReader.State == ODataReaderState.NestedResourceInfoEnd) { ODataNestedResourceInfo navigationLink = jsonLightReader.Item as ODataNestedResourceInfo; return(navigationLink); } } } return(null); }
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); }
private ODataJsonLightServiceDocumentDeserializer CreateODataJsonServiceDocumentDeserializer(MemoryStream stream, IODataUrlResolver urlResolver = null) { ODataMessageReaderSettings settings = new ODataMessageReaderSettings(); ODataJsonLightInputContext inputContext = new ODataJsonLightInputContext(ODataFormat.Json, stream, new ODataMediaType("application", "json"), Encoding.UTF8, settings, true /*readingResponse*/, true /*sync*/, new EdmModel() /*edmModel*/, urlResolver); return(new ODataJsonLightServiceDocumentDeserializer(inputContext)); }
private void VerifyNonPrimitiveTypeRoundtrip(object value, string propertyName) { var properties = new[] { new ODataProperty { Name = propertyName, Value = value } }; var entry = new ODataEntry() { TypeName = "NS.Student", Properties = properties }; ODataMessageWriterSettings settings = new ODataMessageWriterSettings { Version = ODataVersion.V4 }; MemoryStream stream = new MemoryStream(); using (ODataJsonLightOutputContext outputContext = new ODataJsonLightOutputContext( ODataFormat.Json, new NonDisposingStream(stream), new ODataMediaType("application", "json"), Encoding.UTF8, settings, /*writingResponse*/ false, /*synchronous*/ true, model, /*urlResolver*/ null)) { var jsonLightWriter = new ODataJsonLightWriter(outputContext, this.studentSet, this.studentInfo, /*writingFeed*/ false); jsonLightWriter.WriteStart(entry); jsonLightWriter.WriteEnd(); } stream.Position = 0; object actualValue = null; using (ODataJsonLightInputContext inputContext = new ODataJsonLightInputContext( ODataFormat.Json, stream, JsonLightUtils.JsonLightStreamingMediaType, Encoding.UTF8, new ODataMessageReaderSettings(), /*readingResponse*/ false, /*synchronous*/ true, model, /*urlResolver*/ null)) { var jsonLightReader = new ODataJsonLightReader(inputContext, this.studentSet, this.studentInfo, /*readingFeed*/ false); while (jsonLightReader.Read()) { if (jsonLightReader.State == ODataReaderState.EntryEnd) { ODataEntry entryOut = jsonLightReader.Item as ODataEntry; actualValue = entryOut.Properties.Single(p => p.Name == propertyName).ODataValue; } } } TestUtils.AssertODataValueAreEqual(actualValue as ODataValue, value as ODataValue); }
/// <summary> /// Detects the payload kind(s) from the message stream. /// </summary> /// <param name="messageInfo">The context information for the message.</param> /// <param name="settings">Configuration settings of the OData reader.</param> /// <returns>An enumerable of zero, one or more payload kinds that were detected from looking at the payload in the message stream.</returns> private static IEnumerable <ODataPayloadKind> DetectPayloadKindImplementation( ODataMessageInfo messageInfo, ODataMessageReaderSettings settings) { var detectionInfo = new ODataPayloadKindDetectionInfo(messageInfo, settings); messageInfo.Encoding = detectionInfo.GetEncoding(); using (var jsonLightInputContext = new ODataJsonLightInputContext(messageInfo, settings)) { return(jsonLightInputContext.DetectPayloadKind(detectionInfo)); } }
/// <summary> /// Detects the payload kind(s) from the message stream. /// </summary> /// <param name="messageInfo">The context information for the message.</param> /// <param name="settings">Configuration settings of the OData reader.</param> /// <returns>An enumerable of zero, one or more payload kinds that were detected from looking at the payload in the message stream.</returns> private static Task <IEnumerable <ODataPayloadKind> > DetectPayloadKindImplementationAsync( ODataMessageInfo messageInfo, ODataMessageReaderSettings settings) { var detectionInfo = new ODataPayloadKindDetectionInfo(messageInfo, settings); messageInfo.Encoding = detectionInfo.GetEncoding(); var jsonLightInputContext = new ODataJsonLightInputContext(messageInfo, settings); return(jsonLightInputContext.DetectPayloadKindAsync(detectionInfo) .FollowAlwaysWith(t => { jsonLightInputContext.Dispose(); })); }
private ODataJsonLightServiceDocumentDeserializer CreateODataJsonServiceDocumentDeserializer(MemoryStream stream, IODataPayloadUriConverter urlResolver = null) { var messageInfo = new ODataMessageInfo { Encoding = Encoding.UTF8, IsResponse = true, MediaType = new ODataMediaType("application", "json"), IsAsync = false, Model = new EdmModel(), PayloadUriConverter = urlResolver, MessageStream = stream }; var inputContext = new ODataJsonLightInputContext(messageInfo, new ODataMessageReaderSettings()); return(new ODataJsonLightServiceDocumentDeserializer(inputContext)); }
/// <summary> /// Detects the payload kind(s) from the message stream. /// </summary> /// <param name="messageStream">The message stream to read from for payload kind detection.</param> /// <param name="readingResponse">true if reading a response message; otherwise false.</param> /// <param name="detectionInfo">Additional information available for the payload kind detection.</param> /// <returns>An enumerable of zero, one or more payload kinds that were detected from looking at the payload in the message stream.</returns> private IEnumerable <ODataPayloadKind> DetectPayloadKindImplementation( Stream messageStream, bool readingResponse, ODataPayloadKindDetectionInfo detectionInfo) { using (ODataJsonLightInputContext jsonLightInputContext = new ODataJsonLightInputContext( this, messageStream, detectionInfo.ContentType, detectionInfo.GetEncoding(), detectionInfo.MessageReaderSettings, readingResponse, /*synchronous*/ true, detectionInfo.Model, /*urlResolver*/ null)) { return(jsonLightInputContext.DetectPayloadKind(detectionInfo)); } }
private static object ConvertFromResourceOrCollectionValue(string value, IEdmModel model, IEdmTypeReference typeReference) { ODataMessageReaderSettings settings = new ODataMessageReaderSettings(); settings.Validations &= ~ValidationKinds.ThrowOnUndeclaredPropertyForNonOpenType; settings.ReadUntypedAsString = false; using (StringReader reader = new StringReader(value)) { ODataMessageInfo messageInfo = new ODataMessageInfo { MediaType = new ODataMediaType(MimeConstants.MimeApplicationType, MimeConstants.MimeJsonSubType), Model = model, IsResponse = false, IsAsync = false, MessageStream = null, }; using (ODataJsonLightInputContext context = new ODataJsonLightInputContext(reader, messageInfo, settings)) { ODataJsonLightPropertyAndValueDeserializer deserializer = new ODataJsonLightPropertyAndValueDeserializer(context); // TODO: The way JSON array literals look in the URI is different that response payload with an array in it. // The fact that we have to manually setup the underlying reader shows this different in the protocol. // There is a discussion on if we should change this or not. deserializer.JsonReader.Read(); // Move to first thing object rawResult = deserializer.ReadNonEntityValue( null /*payloadTypeName*/, typeReference, null /*DuplicatePropertyNameChecker*/, null /*CollectionWithoutExpectedTypeValidator*/, true /*validateNullValue*/, false /*isTopLevelPropertyValue*/, false /*insideResourceValue*/, null /*propertyName*/); deserializer.ReadPayloadEnd(false); return(rawResult); } } }
/// <summary> /// Converts the given string <paramref name="value"/> to an ODataComplexValue or ODataCollectionValue and returns it. /// Tries in both JSON light and Verbose JSON. /// </summary> /// <remarks>Does not handle primitive values.</remarks> /// <param name="value">Value to be deserialized.</param> /// <param name="version">ODataVersion to be compliant with.</param> /// <param name="model">Model to use for verification.</param> /// <param name="typeReference">Expected type reference from deserialization. If null, verification will be skipped.</param> /// <returns>An ODataComplexValue or ODataCollectionValue that results from the deserialization of <paramref name="value"/>.</returns> internal static object ConvertFromComplexOrCollectionValue(string value, ODataVersion version, IEdmModel model, IEdmTypeReference typeReference) { ODataMessageReaderSettings settings = new ODataMessageReaderSettings(); using (StringReader reader = new StringReader(value)) { using (ODataJsonLightInputContext context = new ODataJsonLightInputContext( ODataFormat.Json, reader, new MediaType(MimeConstants.MimeApplicationType, MimeConstants.MimeJsonSubType), settings, version, false /*readingResponse*/, true /*synchronous*/, model, null /*urlResolver*/, null /*payloadKindDetectionState*/)) { ODataJsonLightPropertyAndValueDeserializer deserializer = new ODataJsonLightPropertyAndValueDeserializer(context); // TODO: The way JSON array literals look in the URI is different that response payload with an array in it. // The fact that we have to manually setup the underlying reader shows this different in the protocol. // There is a discussion on if we should change this or not. deserializer.JsonReader.Read(); // Move to first thing object rawResult = deserializer.ReadNonEntityValue( null /*payloadTypeName*/, typeReference, null /*DuplicatePropertyNameChecker*/, null /*CollectionWithoutExpectedTypeValidator*/, true /*validateNullValue*/, false /*isTopLevelPropertyValue*/, false /*insideComplexValue*/, null /*propertyName*/); deserializer.ReadPayloadEnd(false); Debug.Assert(rawResult is ODataComplexValue || rawResult is ODataCollectionValue, "rawResult is ODataComplexValue || rawResult is ODataCollectionValue"); return(rawResult); } } }
/// <summary> /// Detects the payload kind(s) from the message stream. /// </summary> /// <param name="messageStream">The message stream to read from for payload kind detection.</param> /// <param name="readingResponse">true if reading a response message; otherwise false.</param> /// <param name="detectionInfo">Additional information available for the payload kind detection.</param> /// <returns>An enumerable of zero, one or more payload kinds that were detected from looking at the payload in the message stream.</returns> private Task <IEnumerable <ODataPayloadKind> > DetectPayloadKindImplementationAsync( Stream messageStream, bool readingResponse, ODataPayloadKindDetectionInfo detectionInfo) { ODataJsonLightInputContext jsonLightInputContext = new ODataJsonLightInputContext( this, messageStream, detectionInfo.ContentType, detectionInfo.GetEncoding(), detectionInfo.MessageReaderSettings, readingResponse, /*synchronous*/ false, detectionInfo.Model, /*urlResolver*/ null); return(jsonLightInputContext.DetectPayloadKindAsync(detectionInfo) .FollowAlwaysWith(t => { jsonLightInputContext.Dispose(); })); }
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); }
/// <summary> /// Converts the given string <paramref name="value"/> to an ODataComplexValue or ODataCollectionValue and returns it. /// Tries in both JSON light and Verbose JSON. /// </summary> /// <remarks>Does not handle primitive values.</remarks> /// <param name="value">Value to be deserialized.</param> /// <param name="version">ODataVersion to be compliant with.</param> /// <param name="model">Model to use for verification.</param> /// <param name="typeReference">Expected type reference from deserialization. If null, verification will be skipped.</param> /// <returns>An ODataComplexValue or ODataCollectionValue that results from the deserialization of <paramref name="value"/>.</returns> internal static object ConvertFromComplexOrCollectionValue(string value, ODataVersion version, IEdmModel model, IEdmTypeReference typeReference) { DebugUtils.CheckNoExternalCallers(); ODataMessageReaderSettings settings = new ODataMessageReaderSettings(); // Try read as JSON (light) so long as a custom model was provided if (model.IsUserModel()) { try { using (StringReader reader = new StringReader(value)) { using (ODataJsonLightInputContext context = new ODataJsonLightInputContext( ODataFormat.Json, reader, new MediaType(MimeConstants.MimeApplicationType, MimeConstants.MimeJsonSubType), settings, version, false /*readingResponse*/, true /*synchronous*/, model, null /*urlResolver*/, null /*payloadKindDetectionState*/)) { ODataJsonLightPropertyAndValueDeserializer deserializer = new ODataJsonLightPropertyAndValueDeserializer(context); // TODO: The way JSON array literals look in the URI is different that response payload with an array in it. // The fact that we have to manually setup the underlying reader shows this different in the protocol. // There is a discussion on if we should change this or not. deserializer.JsonReader.Read(); // Move to first thing object rawResult = deserializer.ReadNonEntityValue( null /*payloadTypeName*/, typeReference, null /*DuplicatePropertyNameChecker*/, null /*CollectionWithoutExpectedTypeValidator*/, true /*validateNullValue*/, false /*isTopLevelPropertyValue*/, false /*insideComplexValue*/, null /*propertyName*/); deserializer.ReadPayloadEnd(false); Debug.Assert(rawResult is ODataComplexValue || rawResult is ODataCollectionValue, "rawResult is ODataComplexValue || rawResult is ODataCollectionValue"); return(rawResult); } } } catch (ODataException) { // Swallow. We'll try reading it as Verbose JSON instead. // If an error occurs then, we'll surface that error message. // Since we shipped Verbose JSON only support in 5.0, it's nice to preserve the Verbose JSON error messages. } } // Read as Verbose JSON using (StringReader reader = new StringReader(value)) { using (ODataVerboseJsonInputContext context = new ODataVerboseJsonInputContext( ODataFormat.VerboseJson, reader, settings, version, false /*readingResponse*/, true /*synchronous*/, model, null /*urlResolver*/)) { ODataVerboseJsonPropertyAndValueDeserializer deserializer = new ODataVerboseJsonPropertyAndValueDeserializer(context); deserializer.ReadPayloadStart(false); object rawResult = deserializer.ReadNonEntityValue( typeReference, null /*DuplicatePropertyNameChecker*/, null /*CollectionWithoutExpectedTypeValidator*/, true /*validateNullValue*/, null /*propertyName*/); deserializer.ReadPayloadEnd(false); Debug.Assert(rawResult is ODataComplexValue || rawResult is ODataCollectionValue, "rawResult is ODataComplexValue || rawResult is ODataCollectionValue"); return(rawResult); } } }
private void VerifyComplexRoundtrip(string propertyName, ODataResourceSet resourceSet, params ODataResource[] resources) { var nestedResourceInfo = new ODataNestedResourceInfo() { Name = propertyName, IsCollection = resourceSet != null }; var entry = new ODataResource() { TypeName = "NS.Student" }; ODataMessageWriterSettings settings = new ODataMessageWriterSettings { Version = ODataVersion.V4 }; MemoryStream stream = new MemoryStream(); var messageInfoForWriter = new ODataMessageInfo { MessageStream = new NonDisposingStream(stream), MediaType = new ODataMediaType("application", "json"), Encoding = Encoding.UTF8, IsResponse = false, IsAsync = false, Model = model }; using (var outputContext = new ODataJsonLightOutputContext(messageInfoForWriter, settings)) { var jsonLightWriter = new ODataJsonLightWriter(outputContext, this.studentSet, this.studentInfo, /*writingFeed*/ false); jsonLightWriter.WriteStart(entry); jsonLightWriter.WriteStart(nestedResourceInfo); if (resourceSet != null) { jsonLightWriter.WriteStart(resourceSet); } foreach (var value in resources) { jsonLightWriter.WriteStart(value); jsonLightWriter.WriteEnd(); } if (resourceSet != null) { jsonLightWriter.WriteEnd(); } jsonLightWriter.WriteEnd(); jsonLightWriter.WriteEnd(); } stream.Position = 0; List <ODataResource> actualResources = new List <ODataResource>(); var messageInfoForReader = new ODataMessageInfo { Encoding = Encoding.UTF8, IsResponse = false, MediaType = JsonLightUtils.JsonLightStreamingMediaType, IsAsync = false, Model = model, MessageStream = stream }; using (var inputContext = new ODataJsonLightInputContext(messageInfoForReader, new ODataMessageReaderSettings())) { var jsonLightReader = new ODataJsonLightReader(inputContext, this.studentSet, this.studentInfo, /*readingFeed*/ false); while (jsonLightReader.Read()) { if (jsonLightReader.State == ODataReaderState.ResourceEnd) { actualResources.Add(jsonLightReader.Item as ODataResource); } } } var count = actualResources.Count; actualResources.RemoveAt(count - 1); TestUtils.AssertODataResourceSetAreEqual(actualResources, resources.ToList()); }
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 void VerifyNonPrimitiveTypeRoundtrip(object value, string propertyName) { var properties = new[] { new ODataProperty { Name = propertyName, Value = value } }; var entry = new ODataResource() { TypeName = "NS.Student", Properties = properties }; var stream = new MemoryStream(); var messageInfoForWriter = new ODataMessageInfo { MessageStream = new NonDisposingStream(stream), MediaType = new ODataMediaType("application", "json"), Encoding = Encoding.UTF8, IsResponse = false, IsAsync = false, Model = model }; var settings = new ODataMessageWriterSettings { Version = ODataVersion.V4, Validations = ~ValidationKinds.ThrowOnUndeclaredPropertyForNonOpenType }; using (var outputContext = new ODataJsonLightOutputContext(messageInfoForWriter, settings)) { var jsonLightWriter = new ODataJsonLightWriter(outputContext, this.studentSet, this.studentInfo, /*writingFeed*/ false); jsonLightWriter.WriteStart(entry); jsonLightWriter.WriteEnd(); } stream.Position = 0; object actualValue = null; var messageInfoForReader = new ODataMessageInfo { Encoding = Encoding.UTF8, IsResponse = false, MediaType = JsonLightUtils.JsonLightStreamingMediaType, IsAsync = false, Model = model, MessageStream = stream }; using (var inputContext = new ODataJsonLightInputContext(messageInfoForReader, new ODataMessageReaderSettings())) { var jsonLightReader = new ODataJsonLightReader(inputContext, this.studentSet, this.studentInfo, /*readingFeed*/ false); while (jsonLightReader.Read()) { if (jsonLightReader.State == ODataReaderState.ResourceEnd) { ODataResource entryOut = jsonLightReader.Item as ODataResource; actualValue = entryOut.Properties.Single(p => p.Name == propertyName).ODataValue; } } } TestUtils.AssertODataValueAreEqual(actualValue as ODataValue, value as ODataValue); }