/// <summary> /// Creates an <see cref="ODataWriter" /> to write a resource set. /// </summary> /// <param name="entitySet">The entity set we are going to write entities for.</param> /// <param name="resourceType">The structured type for the items in the resource set to be written (or null if the entity set base type should be used).</param> /// <param name="writingParameter">true means writing a resource set into a uri operation parameter, false writing a resource set in other payloads.</param> /// <param name="writingDelta">true means writing a delta resource set.</param> /// <returns>The created writer.</returns> private ODataWriter CreateODataResourceSetWriterImplementation(IEdmEntitySetBase entitySet, IEdmStructuredType resourceType, bool writingParameter, bool writingDelta) { ODataJsonLightWriter odataJsonWriter = new ODataJsonLightWriter(this, entitySet, resourceType, /*writingResourceSet*/ true, writingParameter, writingDelta); this.outputInStreamErrorListener = odataJsonWriter; return(odataJsonWriter); }
/// <summary> /// Creates an <see cref="ODataWriter" /> to write a resource. /// </summary> /// <param name="navigationSource">The navigation source we are going to write resource set for.</param> /// <param name="resourceType">The structured type for the items in the resource set to be written (or null if the entity set base type should be used).</param> /// <returns>The created writer.</returns> private ODataWriter CreateODataResourceWriterImplementation(IEdmNavigationSource navigationSource, IEdmStructuredType resourceType) { ODataJsonLightWriter odataJsonWriter = new ODataJsonLightWriter(this, navigationSource, resourceType, /*writingResourceSet*/ false); this.outputInStreamErrorListener = odataJsonWriter; return(odataJsonWriter); }
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 void WriteEntryAndValidatePayloadWithModel(ODataResource entry, string expectedPayload, bool writingResponse = true, bool setMetadataDocumentUri = true) { MemoryStream stream = new MemoryStream(); ODataJsonLightOutputContext outputContext = CreateJsonLightOutputContext(stream, writingResponse, this.userModel, setMetadataDocumentUri ? this.serviceDocumentUri : null); entry.SerializationInfo = null; ODataJsonLightWriter writer = new ODataJsonLightWriter(outputContext, /*navigationSource*/ singleton, /*entityType*/ webType, /*writingFeed*/ false); WriteEntryAndValidatePayload(entry, stream, writer, expectedPayload); }
/// <summary> /// Constructor. /// </summary> /// <param name="jsonLightOutputContext">The output context to write to.</param> /// <param name="navigationSource">The navigation source we are going to write entities for.</param> /// <param name="entityType">The entity type for the entries in the resource set to be written (or null if the entity set base type should be used).</param> public ODataJsonLightDeltaWriter(ODataJsonLightOutputContext jsonLightOutputContext, IEdmNavigationSource navigationSource, IEdmEntityType entityType) { Debug.Assert(jsonLightOutputContext != null, "jsonLightOutputContext != null"); this.navigationSource = navigationSource; this.entityType = entityType; this.jsonLightOutputContext = jsonLightOutputContext; this.resourceWriter = new ODataJsonLightWriter(jsonLightOutputContext, navigationSource, entityType, true, writingDelta: true); this.inStreamErrorListener = resourceWriter; }
/// <summary> /// Constructor. /// </summary> /// <param name="jsonLightOutputContext">The output context to write to.</param> /// <param name="navigationSource">The navigation source we are going to write entities for.</param> /// <param name="entityType">The entity type for the entries in the resource set to be written (or null if the entity set base type should be used).</param> public ODataJsonLightDeltaWriter(ODataJsonLightOutputContext jsonLightOutputContext, IEdmNavigationSource navigationSource, IEdmEntityType entityType) { Debug.Assert(jsonLightOutputContext != null, "jsonLightOutputContext != null"); // TODO: Replace the assertion with ODataException. Debug.Assert(jsonLightOutputContext.WritingResponse, "jsonLightOutputContext.WritingResponse is true"); this.navigationSource = navigationSource; this.entityType = entityType; this.jsonLightOutputContext = jsonLightOutputContext; this.resourceWriter = new ODataJsonLightWriter(jsonLightOutputContext, navigationSource, entityType, true, writingDelta: true); this.inStreamErrorListener = resourceWriter; }
private void WriteEntryAndValidatePayloadWithoutModel(ODataResource entry, string expectedPayload, bool writingResponse = true, bool setMetadataDocumentUri = true) { MemoryStream stream = new MemoryStream(); ODataJsonLightOutputContext outputContext = CreateJsonLightOutputContext(stream, writingResponse, this.userModel, setMetadataDocumentUri ? this.serviceDocumentUri : null); ODataResourceSerializationInfo serializationInfo = new ODataResourceSerializationInfo { NavigationSourceName = "MySingleton", NavigationSourceEntityTypeName = "NS.Web", NavigationSourceKind = EdmNavigationSourceKind.Singleton, }; entry.SerializationInfo = serializationInfo; ODataJsonLightWriter writer = new ODataJsonLightWriter(outputContext, /*navigationSource*/ null, /*entityType*/ null, /*writingFeed*/ false); WriteEntryAndValidatePayload(entry, stream, writer, expectedPayload); }
private static void WriteEntryAndValidatePayload(ODataResource entry, MemoryStream stream, ODataJsonLightWriter writer, string expectedPayload) { writer.WriteStart(entry); writer.WriteEnd(); writer.Flush(); stream.Seek(0, SeekOrigin.Begin); string payload = (new StreamReader(stream)).ReadToEnd(); payload.Should().Be(expectedPayload); }
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); }