Пример #1
0
        /// <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.
        /// </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);
        }
        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 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>
 /// <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;
 }
Пример #6
0
 /// <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>
 /// <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>
 /// <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);
 }
Пример #10
0
 /// <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();
     }
 }