Beispiel #1
0
 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="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));
            }
        }
        /// <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));
            }
        }
Beispiel #4
0
 public void ShouldNotRequireContextUriInResponsesForNoMetadata()
 {
     ODataContextUriBuilder.Create(null, false).Should().NotBeNull();
 }
Beispiel #5
0
        public void ShouldRequireMetadataDocumentUriInResponses()
        {
            Action action = () => ODataContextUriBuilder.Create(null, true);

            action.ShouldThrow <ODataException>().WithMessage(Microsoft.OData.Core.Strings.ODataOutputContext_MetadataDocumentUriMissing);
        }