/// <summary> /// Constructor for creating a collection writer to use when writing operation result payloads. /// </summary> /// <param name="jsonLightOutputContext">The output context to write to.</param> /// <param name="itemTypeReference">The item type of the collection being written or null if no metadata is available.</param> internal ODataJsonLightCollectionWriter(ODataJsonLightOutputContext jsonLightOutputContext, IEdmTypeReference itemTypeReference) : base(jsonLightOutputContext, itemTypeReference) { Debug.Assert(jsonLightOutputContext != null, "jsonLightOutputContext != null"); this.jsonLightOutputContext = jsonLightOutputContext; this.jsonLightCollectionSerializer = new ODataJsonLightCollectionSerializer(this.jsonLightOutputContext, /*writingTopLevelCollection*/true); }
/// <summary> /// Constructor. /// </summary> /// <param name="jsonLightOutputContext">The output context to write to.</param> /// <param name="operation">The operation import whose parameters will be written.</param> internal ODataJsonLightParameterWriter(ODataJsonLightOutputContext jsonLightOutputContext, IEdmOperation operation) : base(jsonLightOutputContext, operation) { Debug.Assert(jsonLightOutputContext != null, "jsonLightOutputContext != null"); this.jsonLightOutputContext = jsonLightOutputContext; this.jsonLightValueSerializer = new ODataJsonLightValueSerializer(this.jsonLightOutputContext); }
/// <summary> /// Constructor for creating a collection writer to use when writing parameter payloads. /// </summary> /// <param name="jsonLightOutputContext">The output context to write to.</param> /// <param name="expectedItemType">The type reference of the expected item type or null if no expected item type exists.</param> /// <param name="listener">If not null, the writer will notify the implementer of the interface of relevant state changes in the writer.</param> internal ODataJsonLightCollectionWriter(ODataJsonLightOutputContext jsonLightOutputContext, IEdmTypeReference expectedItemType, IODataReaderWriterListener listener) : base(jsonLightOutputContext, expectedItemType, listener) { Debug.Assert(jsonLightOutputContext != null, "jsonLightOutputContext != null"); Debug.Assert(!jsonLightOutputContext.WritingResponse, "The collection writer constructor for parameter payloads must only be used for writing requests."); this.jsonLightOutputContext = jsonLightOutputContext; this.jsonLightCollectionSerializer = new ODataJsonLightCollectionSerializer(this.jsonLightOutputContext, /*writingTopLevelCollection*/false); }
/// <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 feed to be written (or null if the entity set base type should be used).</param> /// <param name="writingFeed">true if the writer is created for writing a feed; false when it is created for writing an entry.</param> internal ODataJsonLightWriter( ODataJsonLightOutputContext jsonLightOutputContext, IEdmNavigationSource navigationSource, IEdmEntityType entityType, bool writingFeed) : base(jsonLightOutputContext, navigationSource, entityType, writingFeed) { Debug.Assert(jsonLightOutputContext != null, "jsonLightOutputContext != null"); this.jsonLightOutputContext = jsonLightOutputContext; this.jsonLightEntryAndFeedSerializer = new ODataJsonLightEntryAndFeedSerializer(this.jsonLightOutputContext); }
/// <summary> /// Constructor. /// </summary> /// <param name="jsonLightOutputContext">The output context to write to.</param> /// <param name="initContextUriBuilder">Whether contextUriBuilder should be initialized.</param> internal ODataJsonLightSerializer(ODataJsonLightOutputContext jsonLightOutputContext, bool initContextUriBuilder = false) : base(jsonLightOutputContext) { Debug.Assert(jsonLightOutputContext != null, "jsonLightOutputContext != null"); this.jsonLightOutputContext = jsonLightOutputContext; this.instanceAnnotationWriter = new SimpleLazy<JsonLightInstanceAnnotationWriter>(() => new JsonLightInstanceAnnotationWriter(new ODataJsonLightValueSerializer(jsonLightOutputContext), jsonLightOutputContext.TypeNameOracle)); if (initContextUriBuilder) { // DEVNOTE: grab this early so that any validation errors are thrown at creation time rather than when Write___ is called. this.ContextUriBuilder = jsonLightOutputContext.CreateContextUriBuilder(); } }
/// <summary> /// Constructor. /// </summary> /// <param name="jsonLightOutputContext">The output context to write to.</param> /// <param name="initContextUriBuilder">Whether contextUriBuilder should be initialized.</param> internal ODataJsonLightSerializer(ODataJsonLightOutputContext jsonLightOutputContext, bool initContextUriBuilder = false) : base(jsonLightOutputContext) { Debug.Assert(jsonLightOutputContext != null, "jsonLightOutputContext != null"); this.jsonLightOutputContext = jsonLightOutputContext; this.instanceAnnotationWriter = new SimpleLazy <JsonLightInstanceAnnotationWriter>(() => new JsonLightInstanceAnnotationWriter(new ODataJsonLightValueSerializer(jsonLightOutputContext), jsonLightOutputContext.TypeNameOracle)); if (initContextUriBuilder) { // DEVNOTE: grab this early so that any validation errors are thrown at creation time rather than when Write___ is called. this.ContextUriBuilder = jsonLightOutputContext.CreateContextUriBuilder(); } }
/// <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 feed to be written (or null if the entity set base type should be used).</param> /// <param name="writingFeed">true if the writer is created for writing a feed; false when it is created for writing an entry.</param> /// <param name="writingParameter">true if the writer is created for writing a parameter; false otherwise.</param> /// <param name="listener">If not null, the writer will notify the implementer of the interface of relevant state changes in the writer.</param> internal ODataJsonLightWriter( ODataJsonLightOutputContext jsonLightOutputContext, IEdmNavigationSource navigationSource, IEdmEntityType entityType, bool writingFeed, bool writingParameter = false, IODataReaderWriterListener listener = null) : base(jsonLightOutputContext, navigationSource, entityType, writingFeed, listener) { Debug.Assert(jsonLightOutputContext != null, "jsonLightOutputContext != null"); this.jsonLightOutputContext = jsonLightOutputContext; this.jsonLightEntryAndFeedSerializer = new ODataJsonLightEntryAndFeedSerializer(this.jsonLightOutputContext); this.writingParameter = writingParameter; }
/// <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 feed to be written (or null if the entity set base type should be used).</param> /// <param name="writingFeed">true if the writer is created for writing a feed; false when it is created for writing an entry.</param> /// <param name="writingParameter">true if the writer is created for writing a parameter; false otherwise.</param> /// <param name="listener">If not null, the writer will notify the implementer of the interface of relevant state changes in the writer.</param> internal ODataJsonLightWriter( ODataJsonLightOutputContext jsonLightOutputContext, IEdmNavigationSource navigationSource, IEdmEntityType entityType, bool writingFeed, bool writingParameter = false, IODataReaderWriterListener listener = null) : base(jsonLightOutputContext, navigationSource, entityType, writingFeed, listener) { Debug.Assert(jsonLightOutputContext != null, "jsonLightOutputContext != null"); this.jsonLightOutputContext = jsonLightOutputContext; this.jsonLightEntryAndFeedSerializer = new ODataJsonLightEntryAndFeedSerializer(this.jsonLightOutputContext); this.writingParameter = writingParameter; }
/// <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 feed to be written (or null if the entity set base type should be used).</param> /// <param name="writingFeed">true if the writer is created for writing a feed; false when it is created for writing an entry.</param> /// <param name="writingParameter">true if the writer is created for writing a parameter; false otherwise.</param> /// <param name="listener">If not null, the writer will notify the implementer of the interface of relevant state changes in the writer.</param> internal ODataJsonLightWriter( ODataJsonLightOutputContext jsonLightOutputContext, IEdmNavigationSource navigationSource, IEdmEntityType entityType, bool writingFeed, bool writingParameter = false, IODataReaderWriterListener listener = null) : base(jsonLightOutputContext, navigationSource, entityType, writingFeed, listener) { Debug.Assert(jsonLightOutputContext != null, "jsonLightOutputContext != null"); this.jsonLightOutputContext = jsonLightOutputContext; this.jsonLightEntryAndFeedSerializer = new ODataJsonLightEntryAndFeedSerializer(this.jsonLightOutputContext); this.writingParameter = writingParameter; this.jsonWriter = this.jsonLightOutputContext.JsonWriter; this.odataAnnotationWriter = new JsonLightODataAnnotationWriter(this.jsonWriter, jsonLightOutputContext.MessageWriterSettings.ODataSimplified); }
/// <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 feed to be written (or null if the entity set base type should be used).</param> /// <param name="writingFeed">true if the writer is created for writing a feed; false when it is created for writing an entry.</param> /// <param name="writingParameter">true if the writer is created for writing a parameter; false otherwise.</param> /// <param name="listener">If not null, the writer will notify the implementer of the interface of relevant state changes in the writer.</param> internal ODataJsonLightWriter( ODataJsonLightOutputContext jsonLightOutputContext, IEdmNavigationSource navigationSource, IEdmEntityType entityType, bool writingFeed, bool writingParameter = false, IODataReaderWriterListener listener = null) : base(jsonLightOutputContext, navigationSource, entityType, writingFeed, listener) { Debug.Assert(jsonLightOutputContext != null, "jsonLightOutputContext != null"); this.jsonLightOutputContext = jsonLightOutputContext; this.jsonLightEntryAndFeedSerializer = new ODataJsonLightEntryAndFeedSerializer(this.jsonLightOutputContext); this.writingParameter = writingParameter; this.jsonWriter = this.jsonLightOutputContext.JsonWriter; this.odataAnnotationWriter = new JsonLightODataAnnotationWriter(this.jsonWriter, jsonLightOutputContext.MessageWriterSettings.ODataSimplified); }
/// <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 feed to be written (or null if the entity set base type should be used).</param> internal 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.jsonLightOutputContext = jsonLightOutputContext; this.jsonLightEntryAndFeedSerializer = new ODataJsonLightEntryAndFeedSerializer(this.jsonLightOutputContext); this.NavigationSource = navigationSource; this.EntityType = entityType; if (navigationSource != null && entityType == null) { entityType = this.jsonLightOutputContext.EdmTypeResolver.GetElementType(navigationSource); } ODataUri odataUri = this.jsonLightOutputContext.MessageWriterSettings.ODataUri.Clone(); this.scopes.Push(new Scope(WriterState.Start, /*item*/null, navigationSource, entityType, this.jsonLightOutputContext.MessageWriterSettings.SelectedProperties, odataUri)); }
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> /// Initializes a new instance of the <see cref="ODataJsonLightValueSerializer"/> class. /// </summary> /// <param name="outputContext">The output context to use.</param> /// <param name="initContextUriBuilder">Whether contextUriBuilder should be initialized.</param> internal ODataJsonLightValueSerializer(ODataJsonLightOutputContext outputContext, bool initContextUriBuilder = false) : base(outputContext, initContextUriBuilder) { }
/// <summary> /// Constructor. /// </summary> /// <param name="jsonLightOutputContext">The output context to write to.</param> internal ODataJsonLightEntryAndFeedSerializer(ODataJsonLightOutputContext jsonLightOutputContext) : base(jsonLightOutputContext, /*initContextUriBuilder*/ true) { }
/// <summary> /// Constructor. /// </summary> /// <param name="jsonLightOutputContext">The output context to write to.</param> /// <param name="initContextUriBuilder">Whether contextUriBuilder should be initialized.</param> internal ODataJsonLightPropertySerializer(ODataJsonLightOutputContext jsonLightOutputContext, bool initContextUriBuilder = false) : base(jsonLightOutputContext, initContextUriBuilder) { this.jsonLightValueSerializer = new ODataJsonLightValueSerializer(this, initContextUriBuilder); }
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(); }
/// <summary> /// Constructor. /// </summary> /// <param name="jsonLightOutputContext">The output context to write to.</param> internal ODataJsonLightEntryAndFeedSerializer(ODataJsonLightOutputContext jsonLightOutputContext) : base(jsonLightOutputContext, /*initContextUriBuilder*/ true) { }
private static ODataJsonLightServiceDocumentSerializer CreateODataJsonLightServiceDocumentSerializer(MemoryStream memoryStream, IODataUrlResolver urlResolver = null) { var model = new EdmModel(); var messageWriterSettings = new ODataMessageWriterSettings(); IEdmModel mainModel = TestUtils.WrapReferencedModelsToMainModel(model); var jsonLightOutputContext = new ODataJsonLightOutputContext(ODataFormat.Json, memoryStream, new ODataMediaType("application", "json") /*mediaType*/, Encoding.UTF8, messageWriterSettings, false /*WritingResponse*/, true /*sync*/, mainModel, urlResolver); return new ODataJsonLightServiceDocumentSerializer(jsonLightOutputContext); }
/// <summary> /// Constructor. /// </summary> /// <param name="jsonLightOutputContext">The output context to write to.</param> /// <param name="initContextUriBuilder">Whether contextUriBuilder should be initialized.</param> internal ODataJsonLightPropertySerializer(ODataJsonLightOutputContext jsonLightOutputContext, bool initContextUriBuilder = false) : base(jsonLightOutputContext, initContextUriBuilder) { this.jsonLightValueSerializer = new ODataJsonLightValueSerializer(this, initContextUriBuilder); this.bypassValidation = !jsonLightOutputContext.MessageWriterSettings.EnableFullValidation; }
/// <summary> /// Constructor. /// </summary> /// <param name="jsonLightOutputContext">The output context to write to.</param> internal ODataJsonLightEntityReferenceLinkSerializer(ODataJsonLightOutputContext jsonLightOutputContext) : base(jsonLightOutputContext, /*initContextUriBuilder*/ true) { }
/// <summary> /// Constructor. /// </summary> /// <param name="jsonLightOutputContext">The output context to write to.</param> internal ODataJsonLightServiceDocumentSerializer(ODataJsonLightOutputContext jsonLightOutputContext) : base(jsonLightOutputContext, /*initContextUriBuilder*/ true) { }
/// <summary> /// Constructor to create an <see cref="JsonLightExpandedNavigationPropertyWriter"/>. /// </summary> /// <param name="navigationSource">The navigation source of the parent delta entry.</param> /// <param name="entityType">The entity type of the parent delta entry.</param> /// <param name="parentDeltaEntry">The parent delta entry.</param> /// <param name="jsonLightOutputContext">The output context for Json.</param> public JsonLightExpandedNavigationPropertyWriter(IEdmNavigationSource navigationSource, IEdmEntityType entityType, ODataEntry parentDeltaEntry, ODataJsonLightOutputContext jsonLightOutputContext) { this.parentDeltaEntry = parentDeltaEntry; this.entryWriter = new ODataJsonLightWriter(jsonLightOutputContext, navigationSource, entityType, /*writingFeed*/ false, writingDelta: true); }
/// <summary> /// Constructor to create a new expanded navigation property scope. /// </summary> /// <param name="navigationLink">The navigation link for the feed.</param> /// <param name="navigationSource">The navigation source of the parent delta entry.</param> /// <param name="entityType">The entity type of the parent delta entry.</param> /// <param name="selectedProperties">The selected properties of this scope.</param> /// <param name="odataUri">The ODataUri info of this scope.</param> /// <param name="parentDeltaEntry">The parent delta entry.</param> /// <param name="jsonLightOutputContext">The output context for Json.</param> public JsonLightExpandedNavigationPropertyScope(ODataItem navigationLink, IEdmNavigationSource navigationSource, IEdmEntityType entityType, SelectedPropertiesNode selectedProperties, ODataUri odataUri, ODataEntry parentDeltaEntry, ODataJsonLightOutputContext jsonLightOutputContext) : base(navigationLink, navigationSource, entityType, selectedProperties, odataUri) { this.jsonLightExpandedNavigationPropertyWriter = new JsonLightExpandedNavigationPropertyWriter(navigationSource, entityType, parentDeltaEntry, jsonLightOutputContext); }
/// <summary> /// Constructor. /// </summary> /// <param name="jsonLightOutputContext">The output context to write to.</param> /// <param name="writingTopLevelCollection">true when writing a top-level collection that requires the 'value' wrapper object; otherwise false.</param> internal ODataJsonLightCollectionSerializer(ODataJsonLightOutputContext jsonLightOutputContext, bool writingTopLevelCollection) : base(jsonLightOutputContext, /*initContextUriBuilder*/true) { this.writingTopLevelCollection = writingTopLevelCollection; }
/// <summary> /// Constructor. /// </summary> /// <param name="jsonLightOutputContext">The output context to write to.</param> /// <param name="initContextUriBuilder">Whether contextUriBuilder should be initialized.</param> internal ODataJsonLightPropertySerializer(ODataJsonLightOutputContext jsonLightOutputContext, bool initContextUriBuilder = false) : base(jsonLightOutputContext, initContextUriBuilder) { this.jsonLightValueSerializer = new ODataJsonLightValueSerializer(this, initContextUriBuilder); }
private void TestInit(IEdmModel userModel = null, bool fullMetadata = false) { this.stream = new MemoryStream(); this.outputContext = CreateJsonLightOutputContext(this.stream, userModel, fullMetadata); }
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; }
/// <summary> /// Constructor. /// </summary> /// <param name="jsonLightOutputContext">The output context to write to.</param> internal ODataJsonLightEntityReferenceLinkSerializer(ODataJsonLightOutputContext jsonLightOutputContext) : base(jsonLightOutputContext, /*initContextUriBuilder*/ true) { }
/// <summary> /// Constructor. /// </summary> /// <param name="jsonLightOutputContext">The output context to write to.</param> /// <param name="writingTopLevelCollection">true when writing a top-level collection that requires the 'value' wrapper object; otherwise false.</param> internal ODataJsonLightCollectionSerializer(ODataJsonLightOutputContext jsonLightOutputContext, bool writingTopLevelCollection) : base(jsonLightOutputContext, /*initContextUriBuilder*/ true) { this.writingTopLevelCollection = writingTopLevelCollection; }
/// <summary> /// Constructor. /// </summary> /// <param name="jsonLightOutputContext">The output context to write to.</param> /// <param name="initContextUriBuilder">Whether contextUriBuilder should be initialized.</param> internal ODataJsonLightPropertySerializer(ODataJsonLightOutputContext jsonLightOutputContext, bool initContextUriBuilder = false) : base(jsonLightOutputContext, initContextUriBuilder) { this.jsonLightValueSerializer = new ODataJsonLightValueSerializer(this, initContextUriBuilder); this.bypassValidation = !jsonLightOutputContext.MessageWriterSettings.EnableFullValidation; }
/// <summary> /// Convert to a literal value in JSON Light format. /// </summary> /// <param name="model">EDM Model to use for validation and type lookups.</param> /// <param name="writeAction">Delegate to use to actually write the value.</param> /// <returns>The literal value string.</returns> private static string ConverToJsonLightLiteral(IEdmModel model, Action<ODataOutputContext> writeAction) { using (MemoryStream stream = new MemoryStream()) { ODataMessageWriterSettings messageWriterSettings = new ODataMessageWriterSettings() { Version = ODataVersion.V4, Indent = false }; ODataMediaType mediaType = new ODataMediaType(MimeConstants.MimeApplicationType, MimeConstants.MimeJsonSubType); using (ODataJsonLightOutputContext jsonOutputContext = new ODataJsonLightOutputContext(ODataFormat.Json, stream, mediaType, Encoding.UTF8, messageWriterSettings, false, true, model, null)) { writeAction(jsonOutputContext); stream.Position = 0; return new StreamReader(stream).ReadToEnd(); } } }
/// <summary> /// Initializes a new instance of the <see cref="ODataJsonLightValueSerializer"/> class. /// </summary> /// <param name="outputContext">The output context to use.</param> /// <param name="initContextUriBuilder">Whether contextUriBuilder should be initialized.</param> internal ODataJsonLightValueSerializer(ODataJsonLightOutputContext outputContext, bool initContextUriBuilder = false) : base(outputContext, initContextUriBuilder) { }
private void TestInit(IEdmModel userModel = null) { this.stream = new MemoryStream(); this.outputContext = CreateJsonLightOutputContext(this.stream, userModel); }
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 static ODataJsonLightSerializer GetSerializer(Stream stream, string jsonpFunctionName = null, bool nometadata = false, bool setMetadataDocumentUri = true) { var model = new EdmModel(); var complexType = new EdmComplexType("ns", "ErrorDetails"); //var collectionType = new EdmCollectionType(new EdmComplexTypeReference(complexType, false)); model.AddElement(complexType); var settings = new ODataMessageWriterSettings { JsonPCallback = jsonpFunctionName, DisableMessageStreamDisposal = true, Version = ODataVersion.V4 }; if (setMetadataDocumentUri) { settings.SetServiceDocumentUri(new Uri("http://example.com")); } ODataMediaType mediaType = nometadata ? new ODataMediaType("application", "json", new KeyValuePair<string, string>("odata", "none")) : new ODataMediaType("application", "json"); IEdmModel mainModel = TestUtils.WrapReferencedModelsToMainModel(model); var context = new ODataJsonLightOutputContext(ODataFormat.Json, stream, mediaType, Encoding.Default, settings, true, true, mainModel, null); return new ODataJsonLightSerializer(context, setMetadataDocumentUri); }
/// <summary> /// Constructor. /// </summary> /// <param name="jsonLightOutputContext">The output context to write to.</param> internal ODataJsonLightServiceDocumentSerializer(ODataJsonLightOutputContext jsonLightOutputContext) : base(jsonLightOutputContext, /*initContextUriBuilder*/ true) { }
/// <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(); } }