/// <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)); // NOTE: Ideally, we should instantiate a JsonLightODataAnnotationWriter that supports EITHER synchronous OR asynchronous writing. // Based on the value of `jsonLightOutputContext.Synchronous` // However, some higher level classes expose asynchronous wrappers for synchronous methods. Asynchronous wrappers for synchronous methods // that depend on the instance of JsonLightODataAnnotationWriter that supports synchronous writing would break hence the reason to maintain // the two separate instances when asynchronous API implementation is in progress this.odataAnnotationWriter = new SimpleLazy <JsonLightODataAnnotationWriter>( () => new JsonLightODataAnnotationWriter( jsonLightOutputContext.JsonWriter, this.JsonLightOutputContext.OmitODataPrefix, this.MessageWriterSettings.Version)); this.asynchronousODataAnnotationWriter = new SimpleLazy <JsonLightODataAnnotationWriter>( () => new JsonLightODataAnnotationWriter( jsonLightOutputContext.AsynchronousJsonWriter, this.JsonLightOutputContext.OmitODataPrefix, this.MessageWriterSettings.Version)); if (initContextUriBuilder) { // DEVNOTE: grab this early so that any validation errors are thrown at creation time rather than when Write___ is called. this.ContextUriBuilder = ODataContextUriBuilder.Create( this.jsonLightOutputContext.MessageWriterSettings.MetadataDocumentUri, this.jsonLightOutputContext.WritingResponse && !(this.jsonLightOutputContext.MetadataLevel is JsonNoMetadataLevel)); } }
private void InitalizeBuilder() { this.metadataDocumentBaseUri = new Uri(MetadataDocumentUriString); this.responseContextUriBuilder = ODataContextUriBuilder.Create(this.metadataDocumentBaseUri, true); this.requestContextUriBuilder = ODataContextUriBuilder.Create(this.metadataDocumentBaseUri, false); this.builderWithNoMetadataDocumentUri = ODataContextUriBuilder.Create(null, false); }
/// <summary> /// Constructor. /// </summary> /// <param name="atomOutputContext">The output context to write to.</param> internal ODataAtomServiceDocumentSerializer(ODataAtomOutputContext atomOutputContext) : base(atomOutputContext) { this.atomServiceDocumentMetadataSerializer = new ODataAtomServiceDocumentMetadataSerializer(atomOutputContext); // DEVNOTE: grab this early so that any validation errors are thrown at creation time rather than when Write___ is called. this.contextUriBuilder = atomOutputContext.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="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> /// Writes the property start element. /// </summary> /// <param name="beforePropertyCallback">Action called before anything else is written (if it's not null).</param> /// <param name="propertyName">The name of the property to write.</param> /// <param name="value">The odata value to write.</param> /// <param name="isWritingCollection">true if we are writing a collection instead of an entry.</param> /// <param name="isTopLevel">true if writing a top-level property payload; otherwise false.</param> private void WritePropertyStart(Action beforePropertyCallback, string propertyName, ODataValue value, bool isWritingCollection, bool isTopLevel) { if (beforePropertyCallback != null) { beforePropertyCallback(); } if (!isTopLevel) { // <d:propertyname> this.XmlWriter.WriteStartElement( isWritingCollection ? string.Empty : AtomConstants.ODataNamespacePrefix, propertyName, AtomConstants.ODataNamespace); } else { // <m:value> this.XmlWriter.WriteStartElement( AtomConstants.ODataMetadataNamespacePrefix, AtomConstants.ODataValueElementName, AtomConstants.ODataMetadataNamespace); // COMPAT 24: Use standard ('d' and 'm') namespace prefixes for top-level property payloads // Top-level collection payloads don't write namespace declarations on the root element (except for the d namespace) // We decided to use the same set of default namespaces on entries, feeds, collections and top-level properties DefaultNamespaceFlags namespaces = DefaultNamespaceFlags.Gml | DefaultNamespaceFlags.GeoRss; if (!this.MessageWriterSettings.AlwaysUseDefaultXmlNamespaceForRootElement) { // DEVNOTE: no need to include the OData namespace here, because we already defined it above. // However, the order will change if we leave it to XmlWriter to add it. So, if the knob hasn't been flipped, // manually add it. namespaces |= DefaultNamespaceFlags.OData; } this.WriteDefaultNamespaceAttributes(namespaces); ODataContextUriBuilder contextUriBuilder = this.AtomOutputContext.CreateContextUriBuilder(); ODataPayloadKind kind = this.AtomOutputContext.MessageWriterSettings.IsIndividualProperty ? ODataPayloadKind.IndividualProperty : ODataPayloadKind.Property; ODataContextUrlInfo contextInfo = ODataContextUrlInfo.Create(value, this.AtomOutputContext.MessageWriterSettings.ODataUri); this.WriteContextUriProperty(contextUriBuilder.BuildContextUri(kind, contextInfo)); } }
/// <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)); this.odataAnnotationWriter = new SimpleLazy <JsonLightODataAnnotationWriter>(() => new JsonLightODataAnnotationWriter(jsonLightOutputContext.JsonWriter, this.JsonLightOutputContext.ODataSimplifiedOptions.EnableWritingODataAnnotationWithoutPrefix, this.MessageWriterSettings.Version)); if (initContextUriBuilder) { // DEVNOTE: grab this early so that any validation errors are thrown at creation time rather than when Write___ is called. this.ContextUriBuilder = ODataContextUriBuilder.Create( this.jsonLightOutputContext.MessageWriterSettings.MetadataDocumentUri, this.jsonLightOutputContext.WritingResponse && !(this.jsonLightOutputContext.MetadataLevel is JsonNoMetadataLevel)); } }
public void ShouldNotRequireContextUriInResponsesForNoMetadata() { ODataContextUriBuilder.Create(null, false).Should().NotBeNull(); }
public void ShouldRequireMetadataDocumentUriInResponses() { Action action = () => ODataContextUriBuilder.Create(null, true); action.ShouldThrow <ODataException>().WithMessage(Microsoft.OData.Core.Strings.ODataOutputContext_MetadataDocumentUriMissing); }
/// <summary> /// Constructor. /// </summary> /// <param name="atomOutputContext">The output context to write to.</param> internal ODataAtomEntityReferenceLinkSerializer(ODataAtomOutputContext atomOutputContext) : base(atomOutputContext) { // DEVNOTE: grab this early so that any validation errors are thrown at creation time rather than when Write___ is called. this.contextUriBuilder = atomOutputContext.CreateContextUriBuilder(); }