Esempio n. 1
0
        public static JsonNode?Create(JsonElement element, JsonNodeOptions?options)
        {
            JsonNode?node;

            switch (element.ValueKind)
            {
            case JsonValueKind.Null:
                node = null;
                break;

            case JsonValueKind.Object:
                node = new JsonObject(element, options);
                break;

            case JsonValueKind.Array:
                node = new JsonArray(element, options);
                break;

            default:
                node = new JsonValueTrimmable <JsonElement>(element, JsonMetadataServices.JsonElementConverter, options);
                break;
            }

            return(node);
        }
Esempio n. 2
0
        public JsonObject ReadObject(ref Utf8JsonReader reader, JsonNodeOptions?options)
        {
            JsonElement jElement = JsonElement.ParseValue(ref reader);
            JsonObject  jObject  = new JsonObject(jElement, options);

            return(jObject);
        }
Esempio n. 3
0
        /// <summary>
        ///   Parses one JSON value (including objects or arrays) from the provided reader.
        /// </summary>
        /// <param name="reader">The reader to read.</param>
        /// <param name="nodeOptions">Options to control the behavior.</param>
        /// <returns>
        ///   The <see cref="JsonNode"/> from the reader.
        /// </returns>
        /// <remarks>
        ///   <para>
        ///     If the <see cref="Utf8JsonReader.TokenType"/> property of <paramref name="reader"/>
        ///     is <see cref="JsonTokenType.PropertyName"/> or <see cref="JsonTokenType.None"/>, the
        ///     reader will be advanced by one call to <see cref="Utf8JsonReader.Read"/> to determine
        ///     the start of the value.
        ///   </para>
        ///   <para>
        ///     Upon completion of this method <paramref name="reader"/> will be positioned at the
        ///     final token in the JSON value.  If an exception is thrown, the reader is reset to the state it was in when the method was called.
        ///   </para>
        ///   <para>
        ///     This method makes a copy of the data the reader acted on, so there is no caller
        ///     requirement to maintain data integrity beyond the return of this method.
        ///   </para>
        /// </remarks>
        /// <exception cref="ArgumentException">
        ///   <paramref name="reader"/> is using unsupported options.
        /// </exception>
        /// <exception cref="ArgumentException">
        ///   The current <paramref name="reader"/> token does not start or represent a value.
        /// </exception>
        /// <exception cref="JsonException">
        ///   A value could not be read from the reader.
        /// </exception>
        public static JsonNode?Parse(
            ref Utf8JsonReader reader,
            JsonNodeOptions?nodeOptions = null)
        {
            JsonElement element = JsonElement.ParseValue(ref reader);

            return(JsonNodeConverter.Create(element, nodeOptions));
        }
Esempio n. 4
0
        /// <summary>
        ///   Parse text representing a single JSON value.
        /// </summary>
        /// <param name="utf8Json">JSON text to parse.</param>
        /// <param name="nodeOptions">Options to control the node behavior after parsing.</param>
        /// <param name="documentOptions">Options to control the document behavior during parsing.</param>
        /// <returns>
        ///   A <see cref="JsonNode"/> representation of the JSON value.
        /// </returns>
        /// <exception cref="JsonException">
        ///   <paramref name="utf8Json"/> does not represent a valid single JSON value.
        /// </exception>
        public static JsonNode?Parse(
            ReadOnlySpan <byte> utf8Json,
            JsonNodeOptions?nodeOptions         = null,
            JsonDocumentOptions documentOptions = default(JsonDocumentOptions))
        {
            JsonElement element = JsonElement.ParseValue(utf8Json, documentOptions);

            return(JsonNodeConverter.Create(element, nodeOptions));
        }
Esempio n. 5
0
        /// <summary>
        ///   Initializes a new instance of the <see cref="JsonValue"/> class that contains the specified value.
        /// </summary>
        /// <param name="value">The underlying value of the new <see cref="JsonValue"/> instance.</param>
        /// <param name="options">Options to control the behavior.</param>
        /// <returns>The new instance of the <see cref="JsonValue"/> class that contains the specified value.</returns>
        public static JsonValue?Create(JsonElement value, JsonNodeOptions?options = null)
        {
            if (value.ValueKind == JsonValueKind.Null)
            {
                return(null);
            }

            VerifyJsonElementIsNotArrayOrObject(ref value);

            return(new JsonValueTrimmable <JsonElement>(value, JsonMetadataServices.JsonElementConverter));
        }
Esempio n. 6
0
        internal readonly TValue _value; // keep as a field for direct access to avoid copies

        public JsonValue(TValue value, JsonNodeOptions?options = null) : base(options)
        {
            Debug.Assert(value != null);
            Debug.Assert(!(value is JsonElement) || ((JsonElement)(object)value).ValueKind != JsonValueKind.Null);

            if (value is JsonNode)
            {
                ThrowHelper.ThrowArgumentException_NodeValueNotAllowed(nameof(value));
            }

            _value = value;
        }
Esempio n. 7
0
        /// <summary>
        ///   Initializes a new instance of the <see cref="JsonObject"/> class that contains properties from the specified <see cref="JsonElement"/>.
        /// </summary>
        /// <returns>
        ///   The new instance of the <see cref="JsonObject"/> class that contains properties from the specified <see cref="JsonElement"/>.
        /// </returns>
        /// <param name="element">The <see cref="JsonElement"/>.</param>
        /// <param name="options">Options to control the behavior.</param>
        /// <returns>A <see cref="JsonObject"/>.</returns>
        public static JsonObject?Create(JsonElement element, JsonNodeOptions?options = null)
        {
            if (element.ValueKind == JsonValueKind.Null)
            {
                return(null);
            }

            if (element.ValueKind == JsonValueKind.Object)
            {
                return(new JsonObject(element, options));
            }

            throw new InvalidOperationException(SR.Format(SR.NodeElementWrongType, nameof(JsonValueKind.Object)));
        }
Esempio n. 8
0
        /// <summary>
        ///   Parses text representing a single JSON value.
        /// </summary>
        /// <param name="json">JSON text to parse.</param>
        /// <param name="nodeOptions">Options to control the node behavior after parsing.</param>
        /// <param name="documentOptions">Options to control the document behavior during parsing.</param>
        /// <returns>
        ///   A <see cref="JsonNode"/> representation of the JSON value.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        ///   <paramref name="json"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="JsonException">
        ///   <paramref name="json"/> does not represent a valid single JSON value.
        /// </exception>
        public static JsonNode?Parse(
            [StringSyntax(StringSyntaxAttribute.Json)] string json,
            JsonNodeOptions?nodeOptions         = null,
            JsonDocumentOptions documentOptions = default(JsonDocumentOptions))
        {
            if (json is null)
            {
                ThrowHelper.ThrowArgumentNullException(nameof(json));
            }

            JsonElement element = JsonElement.ParseValue(json, documentOptions);

            return(JsonNodeConverter.Create(element, nodeOptions));
        }
Esempio n. 9
0
        /// <summary>
        ///   Parse text representing a single JSON value.
        /// </summary>
        /// <param name="json">JSON text to parse.</param>
        /// <param name="nodeOptions">Options to control the node behavior after parsing.</param>
        /// <param name="documentOptions">Options to control the document behavior during parsing.</param>
        /// <returns>
        ///   A <see cref="JsonNode"/> representation of the JSON value.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        ///   <paramref name="json"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="JsonException">
        ///   <paramref name="json"/> does not represent a valid single JSON value.
        /// </exception>
        public static JsonNode?Parse(
            string json,
            JsonNodeOptions?nodeOptions         = null,
            JsonDocumentOptions documentOptions = default(JsonDocumentOptions))
        {
            if (json == null)
            {
                throw new ArgumentNullException(nameof(json));
            }

            JsonElement element = JsonElement.ParseValue(json, documentOptions);

            return(JsonNodeConverter.Create(element, nodeOptions));
        }
Esempio n. 10
0
        /// <summary>
        ///   Parse a <see cref="Stream"/> as UTF-8-encoded data representing a single JSON value into a
        ///   <see cref="JsonNode"/>.  The Stream will be read to completion.
        /// </summary>
        /// <param name="utf8Json">JSON text to parse.</param>
        /// <param name="nodeOptions">Options to control the node behavior after parsing.</param>
        /// <param name="documentOptions">Options to control the document behavior during parsing.</param>
        /// <returns>
        ///   A <see cref="JsonNode"/> representation of the JSON value.
        /// </returns>
        /// <exception cref="JsonException">
        ///   <paramref name="utf8Json"/> does not represent a valid single JSON value.
        /// </exception>
        public static JsonNode?Parse(
            Stream utf8Json,
            JsonNodeOptions?nodeOptions         = null,
            JsonDocumentOptions documentOptions = default)
        {
            if (utf8Json is null)
            {
                ThrowHelper.ThrowArgumentNullException(nameof(utf8Json));
            }

            JsonElement element = JsonElement.ParseValue(utf8Json, documentOptions);

            return(JsonNodeConverter.Create(element, nodeOptions));
        }
Esempio n. 11
0
        public static JsonValue?Create <T>(T?value, JsonNodeOptions?options = null)
        {
            if (value == null)
            {
                return(null);
            }

            if (value is JsonElement element)
            {
                if (element.ValueKind == JsonValueKind.Null)
                {
                    return(null);
                }

                VerifyJsonElementIsNotArrayOrObject(ref element);

                return(new JsonValueTrimmable <JsonElement>(element, JsonMetadataServices.JsonElementConverter, options));
            }

            return(new JsonValueNotTrimmable <T>(value, options));
        }
Esempio n. 12
0
        /// <summary>
        ///   Initializes a new instance of the <see cref="JsonValue"/> class that contains the specified value.
        /// </summary>
        /// <returns>
        ///   The new instance of the <see cref="JsonValue"/> class that contains the specified value.
        /// </returns>
        /// <typeparam name="T">The type of value to be added.</typeparam>
        /// <param name="value">The value to add.</param>
        /// <param name="options">Options to control the behavior.</param>
        /// <returns>The new instance of the <see cref="JsonValue"/> class that contains the specified value.</returns>
        public static JsonValue?Create <T>(T?value, JsonNodeOptions?options = null)
        {
            if (value == null)
            {
                return(null);
            }

            if (value is JsonElement element)
            {
                if (element.ValueKind == JsonValueKind.Null)
                {
                    return(null);
                }

                if (element.ValueKind == JsonValueKind.Object || element.ValueKind == JsonValueKind.Array)
                {
                    throw new InvalidOperationException(SR.NodeElementCannotBeObjectOrArray);
                }
            }

            return(new JsonValue <T>(value, options));
        }
Esempio n. 13
0
        /// <summary>
        ///   Initializes a new instance of the <see cref="JsonValue"/> class that contains the specified value.
        /// </summary>
        /// <returns>
        ///   The new instance of the <see cref="JsonValue"/> class that contains the specified value.
        /// </returns>
        /// <typeparam name="T">The type of value to create.</typeparam>
        /// <param name="value">The value to create.</param>
        /// <param name="jsonTypeInfo">The <see cref="JsonTypeInfo"/> that will be used to serialize the value.</param>
        /// <param name="options">Options to control the behavior.</param>
        /// <returns>The new instance of the <see cref="JsonValue"/> class that contains the specified value.</returns>
        public static JsonValue?Create <T>(T?value, JsonTypeInfo <T> jsonTypeInfo, JsonNodeOptions?options = null)
        {
            if (jsonTypeInfo is null)
            {
                ThrowHelper.ThrowArgumentNullException(nameof(jsonTypeInfo));
            }

            if (value == null)
            {
                return(null);
            }

            if (value is JsonElement element)
            {
                if (element.ValueKind == JsonValueKind.Null)
                {
                    return(null);
                }

                VerifyJsonElementIsNotArrayOrObject(ref element);
            }

            return(new JsonValueTrimmable <T>(value, jsonTypeInfo, options));
        }
Esempio n. 14
0
 /// <summary>
 ///   Initializes a new instance of the <see cref="JsonValue"/> class that contains the specified value.
 /// </summary>
 /// <param name="value">The underlying value of the new <see cref="JsonValue"/> instance.</param>
 /// <param name="options">Options to control the behavior.</param>
 /// <returns>The new instance of the <see cref="JsonValue"/> class that contains the specified value.</returns>
 public static JsonValue?Create(byte?value, JsonNodeOptions?options = null) => value.HasValue ? new JsonValueTrimmable <byte>(value.Value, JsonMetadataServices.ByteConverter) : null;
Esempio n. 15
0
 private protected JsonValue(JsonNodeOptions?options = null) : base(options)
 {
 }
Esempio n. 16
0
 public static JsonValue Create(ushort value, JsonNodeOptions?options = null) => new JsonValueTrimmable <ushort>(value, JsonMetadataServices.UInt16Converter);
Esempio n. 17
0
        public JsonArray ReadList(ref Utf8JsonReader reader, JsonNodeOptions?options = null)
        {
            JsonElement jElement = JsonElement.ParseValue(ref reader);

            return(new JsonArray(jElement, options));
        }
Esempio n. 18
0
 internal JsonNode(JsonNodeOptions?options = null)
 {
     _options = options;
 }
Esempio n. 19
0
 /// <summary>
 ///   Initializes a new instance of the <see cref="JsonValue"/> class that contains the specified value.
 /// </summary>
 /// <param name="value">The underlying value of the new <see cref="JsonValue"/> instance.</param>
 /// <param name="options">Options to control the behavior.</param>
 /// <returns>The new instance of the <see cref="JsonValue"/> class that contains the specified value.</returns>
 public static JsonValue Create(decimal value, JsonNodeOptions?options = null) => new JsonValueTrimmable <decimal>(value, JsonMetadataServices.DecimalConverter);
Esempio n. 20
0
 /// <summary>
 ///   Initializes a new instance of the <see cref="JsonValue"/> class that contains the specified value.
 /// </summary>
 /// <param name="value">The underlying value of the new <see cref="JsonValue"/> instance.</param>
 /// <param name="options">Options to control the behavior.</param>
 /// <returns>The new instance of the <see cref="JsonValue"/> class that contains the specified value.</returns>
 public static JsonValue?Create(DateTimeOffset?value, JsonNodeOptions?options = null) => value.HasValue ? new JsonValueTrimmable <DateTimeOffset>(value.Value, JsonMetadataServices.DateTimeOffsetConverter) : null;
Esempio n. 21
0
 public static JsonValue Create(ulong value, JsonNodeOptions?options = null) => new JsonValueTrimmable <ulong>(value, JsonMetadataServices.UInt64Converter);
Esempio n. 22
0
 public static JsonValue?Create(ulong?value, JsonNodeOptions?options = null) => value.HasValue ? new JsonValueTrimmable <ulong>(value.Value, JsonMetadataServices.UInt64Converter) : null;
Esempio n. 23
0
 /// <summary>
 ///   Initializes a new instance of the <see cref="JsonObject"/> class that is empty.
 /// </summary>
 /// <param name="options">Options to control the behavior.</param>
 public JsonObject(JsonNodeOptions?options = null) : base(options)
 {
 }
Esempio n. 24
0
 /// <summary>
 ///   Initializes a new instance of the <see cref="JsonValue"/> class that contains the specified value.
 /// </summary>
 /// <param name="value">The underlying value of the new <see cref="JsonValue"/> instance.</param>
 /// <param name="options">Options to control the behavior.</param>
 /// <returns>The new instance of the <see cref="JsonValue"/> class that contains the specified value.</returns>
 public static JsonValue Create(byte value, JsonNodeOptions?options = null) => new JsonValueTrimmable <byte>(value, JsonMetadataServices.ByteConverter);
Esempio n. 25
0
 public JsonValueNotTrimmable(TValue value, JsonNodeOptions?options = null) : base(value, options)
 {
 }
Esempio n. 26
0
 /// <summary>
 ///   Initializes a new instance of the <see cref="JsonValue"/> class that contains the specified value.
 /// </summary>
 /// <param name="value">The underlying value of the new <see cref="JsonValue"/> instance.</param>
 /// <param name="options">Options to control the behavior.</param>
 /// <returns>The new instance of the <see cref="JsonValue"/> class that contains the specified value.</returns>
 public static JsonValue Create(char value, JsonNodeOptions?options = null) => new JsonValueTrimmable <char>(value, JsonMetadataServices.CharConverter);
Esempio n. 27
0
 /// <summary>
 ///   Initializes a new instance of the <see cref="JsonObject"/> class that contains the specified <paramref name="properties"/>.
 /// </summary>
 /// <param name="properties">The properties to be added.</param>
 /// <param name="options">Options to control the behavior.</param>
 public JsonObject(IEnumerable <KeyValuePair <string, JsonNode?> > properties, JsonNodeOptions?options = null)
 {
     foreach (KeyValuePair <string, JsonNode?> node in properties)
     {
         Add(node.Key, node.Value);
     }
 }
Esempio n. 28
0
 /// <summary>
 ///   Initializes a new instance of the <see cref="JsonValue"/> class that contains the specified value.
 /// </summary>
 /// <param name="value">The underlying value of the new <see cref="JsonValue"/> instance.</param>
 /// <param name="options">Options to control the behavior.</param>
 /// <returns>The new instance of the <see cref="JsonValue"/> class that contains the specified value.</returns>
 public static JsonValue?Create(char?value, JsonNodeOptions?options = null) => value.HasValue ? new JsonValueTrimmable <char>(value.Value, JsonMetadataServices.CharConverter) : null;
Esempio n. 29
0
 internal JsonObject(JsonElement element, JsonNodeOptions?options = null) : base(options)
 {
     Debug.Assert(element.ValueKind == JsonValueKind.Object);
     _jsonElement = element;
 }
Esempio n. 30
0
 /// <summary>
 ///   Initializes a new instance of the <see cref="JsonValue"/> class that contains the specified value.
 /// </summary>
 /// <param name="value">The underlying value of the new <see cref="JsonValue"/> instance.</param>
 /// <param name="options">Options to control the behavior.</param>
 /// <returns>The new instance of the <see cref="JsonValue"/> class that contains the specified value.</returns>
 public static JsonValue Create(DateTimeOffset value, JsonNodeOptions?options = null) => new JsonValueTrimmable <DateTimeOffset>(value, JsonMetadataServices.DateTimeOffsetConverter);