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); }
/// <summary> /// Constructor. /// </summary> /// <param name="jsonLightInputContext">The input to read the payload from.</param> /// <param name="navigationSource">The navigation source we are going to read entities for.</param> /// <param name="expectedEntityType">The expected entity type for the resource to be read (in case of resource reader) or entries in the resource set to be read (in case of resource set reader).</param> public ODataJsonLightDeltaReader( ODataJsonLightInputContext jsonLightInputContext, IEdmNavigationSource navigationSource, IEdmEntityType expectedEntityType) { this.underlyingReader = new ODataJsonLightReader(jsonLightInputContext, navigationSource, expectedEntityType, /*readingResourceSet*/ true, /*readingParameter*/ false, /*readingDelta*/ true, /*listener*/ null); }
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 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 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); }
private async Task DoReadAsync( ODataJsonLightReader jsonLightReader, Action <ODataResourceSet> verifyResourceSetAction = null, Action <ODataResource> verifyResourceAction = null) { while (await jsonLightReader.ReadAsync()) { switch (jsonLightReader.State) { case ODataReaderState.ResourceSetStart: break; case ODataReaderState.ResourceSetEnd: if (verifyResourceSetAction != null) { verifyResourceSetAction(jsonLightReader.Item as ODataResourceSet); } break; case ODataReaderState.ResourceStart: break; case ODataReaderState.ResourceEnd: if (verifyResourceAction != null) { verifyResourceAction(jsonLightReader.Item as ODataResource); } break; default: break; } } }
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 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); }