Exemple #1
0
 internal void WriteJson(object instance, JsonWriter jsonWriter, string typeName, ODataVersion odataVersion)
 {
     IPrimitiveTypeConverter converter;
     Type type = instance.GetType();
     this.TryGetConverter(type, out converter);
     converter.WriteJson(instance, jsonWriter, typeName, odataVersion);
 }
 internal static void WriteError(JsonWriter jsonWriter, ODataError error, bool includeDebugInformation, int maxInnerErrorDepth)
 {
     string str;
     string str2;
     string str3;
     ErrorUtils.GetErrorDetails(error, out str, out str2, out str3);
     ODataInnerError innerError = includeDebugInformation ? error.InnerError : null;
     WriteError(jsonWriter, str, str2, str3, innerError, maxInnerErrorDepth);
 }
        /// <summary>
        /// Write an error message.
        /// </summary>
        /// <param name="jsonWriter">The JSON writer to write the error to.</param>
        /// <param name="error">The error instance to write.</param>
        /// <param name="includeDebugInformation">A flag indicating whether error details should be written (in debug mode only) or not.</param>
        /// <param name="maxInnerErrorDepth">The maximumum number of nested inner errors to allow.</param>
        internal static void WriteError(JsonWriter jsonWriter, ODataError error, bool includeDebugInformation, int maxInnerErrorDepth)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(jsonWriter != null, "jsonWriter != null");
            Debug.Assert(error != null, "error != null");

            string code, message, messageLanguage;
            ErrorUtils.GetErrorDetails(error, out code, out message, out messageLanguage);

            ODataInnerError innerError = includeDebugInformation ? error.InnerError : null;
            ODataJsonWriterUtils.WriteError(jsonWriter, code, message, messageLanguage, innerError, maxInnerErrorDepth);
        }
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="format">The format for this output context.</param>
 /// <param name="jsonWriter">The JSON writer to write to.</param>
 /// <param name="messageWriterSettings">Configuration settings of the OData writer.</param>
 /// <param name="writingResponse">true if writing a response message; otherwise false.</param>
 /// <param name="synchronous">true if the output should be written synchronously; false if it should be written asynchronously.</param>
 /// <param name="model">The model to use.</param>
 /// <param name="urlResolver">The optional URL resolver to perform custom URL resolution for URLs written to the payload.</param>
 private ODataJsonOutputContext(
     ODataFormat format,
     JsonWriter jsonWriter,
     ODataMessageWriterSettings messageWriterSettings,
     bool writingResponse,
     bool synchronous,
     IEdmModel model,
     IODataUrlResolver urlResolver)
     : base(format, messageWriterSettings, writingResponse, synchronous, model, urlResolver)
 {
     this.jsonWriter = jsonWriter;
 }
 private static void WriteInnerError(JsonWriter jsonWriter, ODataInnerError innerError, string innerErrorPropertyName, int recursionDepth, int maxInnerErrorDepth)
 {
     ValidationUtils.IncreaseAndValidateRecursionDepth(ref recursionDepth, maxInnerErrorDepth);
     jsonWriter.WriteName(innerErrorPropertyName);
     jsonWriter.StartObjectScope();
     jsonWriter.WriteName("message");
     jsonWriter.WriteValue(innerError.Message ?? string.Empty);
     jsonWriter.WriteName("type");
     jsonWriter.WriteValue(innerError.TypeName ?? string.Empty);
     jsonWriter.WriteName("stacktrace");
     jsonWriter.WriteValue(innerError.StackTrace ?? string.Empty);
     if (innerError.InnerError != null)
     {
         WriteInnerError(jsonWriter, innerError.InnerError, "internalexception", recursionDepth, maxInnerErrorDepth);
     }
     jsonWriter.EndObjectScope();
 }
 private static void WriteError(JsonWriter jsonWriter, string code, string message, string messageLanguage, ODataInnerError innerError, int maxInnerErrorDepth)
 {
     jsonWriter.StartObjectScope();
     jsonWriter.WriteName("error");
     jsonWriter.StartObjectScope();
     jsonWriter.WriteName("code");
     jsonWriter.WriteValue(code);
     jsonWriter.WriteName("message");
     jsonWriter.StartObjectScope();
     jsonWriter.WriteName("lang");
     jsonWriter.WriteValue(messageLanguage);
     jsonWriter.WriteName("value");
     jsonWriter.WriteValue(message);
     jsonWriter.EndObjectScope();
     if (innerError != null)
     {
         WriteInnerError(jsonWriter, innerError, "innererror", 0, maxInnerErrorDepth);
     }
     jsonWriter.EndObjectScope();
     jsonWriter.EndObjectScope();
 }
        /// <summary>
        /// Write an error message.
        /// </summary>
        /// <param name="jsonWriter">The JSON writer to write the error to.</param>
        /// <param name="code">The code of the error.</param>
        /// <param name="message">The message of the error.</param>
        /// <param name="messageLanguage">The language of the message.</param>
        /// <param name="innerError">Inner error details that will be included in debug mode (if present).</param>
        /// <param name="maxInnerErrorDepth">The maximumum number of nested inner errors to allow.</param>
        private static void WriteError(JsonWriter jsonWriter, string code, string message, string messageLanguage, ODataInnerError innerError, int maxInnerErrorDepth)
        {
            Debug.Assert(jsonWriter != null, "jsonWriter != null");
            Debug.Assert(code != null, "code != null");
            Debug.Assert(message != null, "message != null");
            Debug.Assert(messageLanguage != null, "messageLanguage != null");

            // { "error": {
            jsonWriter.StartObjectScope();
            jsonWriter.WriteName(JsonConstants.ODataErrorName);
            jsonWriter.StartObjectScope();

            // "code": "<code>"
            jsonWriter.WriteName(JsonConstants.ODataErrorCodeName);
            jsonWriter.WriteValue(code);

            // "message": {
            jsonWriter.WriteName(JsonConstants.ODataErrorMessageName);
            jsonWriter.StartObjectScope();

            // "lang": "<messageLanguage>"
            jsonWriter.WriteName(JsonConstants.ODataErrorMessageLanguageName);
            jsonWriter.WriteValue(messageLanguage);

            // "value": "<message>"
            jsonWriter.WriteName(JsonConstants.ODataErrorMessageValueName);
            jsonWriter.WriteValue(message);

            // }
            jsonWriter.EndObjectScope();

            if (innerError != null)
            {
                ODataJsonWriterUtils.WriteInnerError(jsonWriter, innerError, JsonConstants.ODataErrorInnerErrorName, /* recursionDepth */ 0, maxInnerErrorDepth);
            }

            // } }
            jsonWriter.EndObjectScope();
            jsonWriter.EndObjectScope();
        }
 public void WriteJson(object instance, JsonWriter jsonWriter, string typeName, ODataVersion odataVersion)
 {
     IDictionary<string, object> jsonObjectValue = GeoJsonObjectFormatter.Create().Write((ISpatial) instance);
     jsonWriter.WriteJsonObjectValue(jsonObjectValue, typeName, odataVersion);
 }
        /// <summary>
        /// Try to write the JSON representation of <paramref name="instance"/> using a registered primitive type converter
        /// </summary>
        /// <param name="instance">Object to convert to JSON representation.</param>
        /// <param name="jsonWriter">JsonWriter instance to write to.</param>
        /// <param name="typeName">Type name of the instance. If the type name is null, the type name is not written.</param>
        /// <param name="odataVersion">The OData protocol version to be used for writing payloads.</param>
        internal void WriteJson(object instance, JsonWriter jsonWriter, string typeName, ODataVersion odataVersion)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(instance != null, "Expected a non-null instance to write.");

            Type instanceType = instance.GetType();

            IPrimitiveTypeConverter primitiveTypeConverter;
            this.TryGetConverter(instanceType, out primitiveTypeConverter);
            Debug.Assert(primitiveTypeConverter != null, "primitiveTypeConverter != null");
            primitiveTypeConverter.WriteJson(instance, jsonWriter, typeName, odataVersion);
        }
 internal static string ConvertToUriComplexLiteral(ODataComplexValue complexValue, IEdmModel model, ODataVersion version)
 {
     ExceptionUtils.CheckArgumentNotNull<ODataComplexValue>(complexValue, "complexValue");
     ExceptionUtils.CheckArgumentNotNull<IEdmModel>(model, "model");
     StringBuilder sb = new StringBuilder();
     using (TextWriter writer = new StringWriter(sb, CultureInfo.InvariantCulture))
     {
         JsonWriter jsonWriter = new JsonWriter(writer, false);
         bool writingResponse = false;
         ODataMessageWriterSettings messageWriterSettings = new ODataMessageWriterSettings {
             Version = new ODataVersion?(version)
         };
         using (ODataJsonOutputContext context = ODataJsonOutputContext.Create(ODataFormat.VerboseJson, jsonWriter, messageWriterSettings, writingResponse, model, null))
         {
             ODataJsonPropertyAndValueSerializer serializer = new ODataJsonPropertyAndValueSerializer(context);
             serializer.WriteComplexValue(complexValue, null, true, serializer.CreateDuplicatePropertyNamesChecker(), null);
         }
     }
     return sb.ToString();
 }
        /// <summary>
        /// Write an inner error property and message.
        /// </summary>
        /// <param name="jsonWriter">The JSON writer to write the error to.</param>
        /// <param name="innerError">Inner error details.</param>
        /// <param name="innerErrorPropertyName">The property name for the inner error property.</param>
        /// <param name="recursionDepth">The number of times this method has been called recursively.</param>
        /// <param name="maxInnerErrorDepth">The maximumum number of nested inner errors to allow.</param>
        private static void WriteInnerError(JsonWriter jsonWriter, ODataInnerError innerError, string innerErrorPropertyName, int recursionDepth, int maxInnerErrorDepth)
        {
            Debug.Assert(jsonWriter != null, "jsonWriter != null");
            Debug.Assert(innerErrorPropertyName != null, "innerErrorPropertyName != null");

            ValidationUtils.IncreaseAndValidateRecursionDepth(ref recursionDepth, maxInnerErrorDepth);

            // "innererror": {
            jsonWriter.WriteName(innerErrorPropertyName);
            jsonWriter.StartObjectScope();

            //// NOTE: we add empty elements if no information is provided for the message, error type and stack trace
            ////       to stay compatible with Astoria.

            // "message": "<message>"
            jsonWriter.WriteName(JsonConstants.ODataErrorInnerErrorMessageName);
            jsonWriter.WriteValue(innerError.Message ?? string.Empty);

            // "type": "<typename">
            jsonWriter.WriteName(JsonConstants.ODataErrorInnerErrorTypeNameName);
            jsonWriter.WriteValue(innerError.TypeName ?? string.Empty);

            // "stacktrace": "<stacktrace>"
            jsonWriter.WriteName(JsonConstants.ODataErrorInnerErrorStackTraceName);
            jsonWriter.WriteValue(innerError.StackTrace ?? string.Empty);

            if (innerError.InnerError != null)
            {
                // "internalexception": { <nested inner error> }
                ODataJsonWriterUtils.WriteInnerError(jsonWriter, innerError.InnerError, JsonConstants.ODataErrorInnerErrorInnerErrorName, recursionDepth, maxInnerErrorDepth);
            }

            // }
            jsonWriter.EndObjectScope();
        }
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="format">The format for this output context.</param>
        /// <param name="messageStream">The message stream to write the payload to.</param>
        /// <param name="encoding">The encoding to use for the payload.</param>
        /// <param name="messageWriterSettings">Configuration settings of the OData writer.</param>
        /// <param name="writingResponse">true if writing a response message; otherwise false.</param>
        /// <param name="synchronous">true if the output should be written synchronously; false if it should be written asynchronously.</param>
        /// <param name="model">The model to use.</param>
        /// <param name="urlResolver">The optional URL resolver to perform custom URL resolution for URLs written to the payload.</param>
        private ODataJsonOutputContext(
            ODataFormat format,
            Stream messageStream,
            Encoding encoding,
            ODataMessageWriterSettings messageWriterSettings,
            bool writingResponse,
            bool synchronous,
            IEdmModel model,
            IODataUrlResolver urlResolver)
            : base(format, messageWriterSettings, writingResponse, synchronous, model, urlResolver)
        {
            try
            {
                this.messageOutputStream = messageStream;

                Stream outputStream;
                if (synchronous)
                {
                    outputStream = messageStream;
                }
                else
                {
                    this.asynchronousOutputStream = new AsyncBufferedStream(messageStream);
                    outputStream = this.asynchronousOutputStream;
                }

                this.textWriter = new StreamWriter(outputStream, encoding);

                this.jsonWriter = new JsonWriter(this.textWriter, messageWriterSettings.Indent);
            }
            catch (Exception e)
            {
                // Dispose the message stream if we failed to create the input context.
                if (ExceptionUtils.IsCatchableExceptionType(e) && messageStream != null)
                {
                    messageStream.Dispose();
                }

                throw;
            }
        }
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            try
            {
                if (this.messageOutputStream != null)
                {
                    // JsonWriter.Flush will call the underlying TextWriter.Flush.
                    // The TextWriter.Flush (Which is in fact StreamWriter.Flush) will call the underlying Stream.Flush.
                    this.jsonWriter.Flush();

                    // In the async case the underlying stream is the async buffered stream, so we have to flush that explicitely.
                    if (this.asynchronousOutputStream != null)
                    {
                        this.asynchronousOutputStream.FlushSync();
                        this.asynchronousOutputStream.Dispose();
                    }

                    // Dipose the message stream (note that we OWN this stream, so we always dispose it).
                    this.messageOutputStream.Dispose();
                }
            }
            finally
            {
                this.messageOutputStream = null;
                this.asynchronousOutputStream = null;
                this.textWriter = null;
                this.jsonWriter = null;
            }
        }
        /// <summary>
        /// Create JSON output context.
        /// </summary>
        /// <param name="format">The format to create the output context for.</param>
        /// <param name="jsonWriter">The JSON writer to write to.</param>
        /// <param name="messageWriterSettings">Configuration settings of the OData writer.</param>
        /// <param name="writingResponse">true if writing a response message; otherwise false.</param>
        /// <param name="model">The model to use.</param>
        /// <param name="urlResolver">The optional URL resolver to perform custom URL resolution for URLs written to the payload.</param>
        /// <returns>The newly created output context.</returns>
        /// <remarks>This method is for writing the output directly to the JSON writer, when created with this method
        /// the output context doesn't support most of the methods on it. Note that Dispose will do nothing in this case.</remarks>
        internal static ODataJsonOutputContext Create(
            ODataFormat format,
            JsonWriter jsonWriter,
            ODataMessageWriterSettings messageWriterSettings,
            bool writingResponse,
            IEdmModel model,
            IODataUrlResolver urlResolver)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(format == ODataFormat.VerboseJson, "This method only supports the verbose JSON format.");
            Debug.Assert(jsonWriter != null, "jsonWriter != null");
            Debug.Assert(messageWriterSettings != null, "messageWriterSettings != null");

            return new ODataJsonOutputContext(
                format,
                jsonWriter,
                messageWriterSettings,
                writingResponse,
                true,
                model,
                urlResolver);
        }
Exemple #15
0
 public Scope(JsonWriter.ScopeType type)
 {
     this.type = type;
 }