| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Diagnostics; |
| | | 5 | | using System.Text.Json.Nodes; |
| | | 6 | | using System.Text.Json.Schema; |
| | | 7 | | |
| | | 8 | | namespace System.Text.Json.Serialization.Converters |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Converter for JsonNode-derived types. The {T} value must be Object and not JsonNode |
| | | 12 | | /// since we allow Object-declared members\variables to deserialize as {JsonNode}. |
| | | 13 | | /// </summary> |
| | | 14 | | internal sealed class JsonNodeConverter : JsonConverter<JsonNode?> |
| | | 15 | | { |
| | 270 | 16 | | internal static JsonNodeConverter Instance { get; } = new JsonNodeConverter(); |
| | | 17 | | |
| | | 18 | | public override void Write(Utf8JsonWriter writer, JsonNode? value, JsonSerializerOptions options) |
| | 0 | 19 | | { |
| | 0 | 20 | | if (value is null) |
| | 0 | 21 | | { |
| | 0 | 22 | | writer.WriteNullValue(); |
| | 0 | 23 | | } |
| | | 24 | | else |
| | 0 | 25 | | { |
| | 0 | 26 | | value.WriteTo(writer, options); |
| | 0 | 27 | | } |
| | 0 | 28 | | } |
| | | 29 | | |
| | | 30 | | public override JsonNode? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| | 634 | 31 | | { |
| | 634 | 32 | | return options.AllowDuplicateProperties |
| | 634 | 33 | | ? ReadAsJsonElement(ref reader, options.GetNodeOptions()) |
| | 634 | 34 | | : ReadAsJsonNode(ref reader, options.GetNodeOptions()); |
| | 326 | 35 | | } |
| | | 36 | | |
| | | 37 | | internal static JsonNode? ReadAsJsonElement(ref Utf8JsonReader reader, JsonNodeOptions options) |
| | 634 | 38 | | { |
| | 634 | 39 | | switch (reader.TokenType) |
| | | 40 | | { |
| | | 41 | | case JsonTokenType.String: |
| | | 42 | | case JsonTokenType.False: |
| | | 43 | | case JsonTokenType.True: |
| | | 44 | | case JsonTokenType.Number: |
| | 406 | 45 | | return JsonValueConverter.ReadNonNullPrimitiveValue(ref reader, options); |
| | | 46 | | case JsonTokenType.StartObject: |
| | 48 | 47 | | return JsonObjectConverter.ReadAsJsonElement(ref reader, options); |
| | | 48 | | case JsonTokenType.StartArray: |
| | 180 | 49 | | return JsonArrayConverter.ReadAsJsonElement(ref reader, options); |
| | | 50 | | case JsonTokenType.Null: |
| | 0 | 51 | | return null; |
| | | 52 | | default: |
| | 0 | 53 | | Debug.Assert(false); |
| | 0 | 54 | | throw new JsonException(); |
| | | 55 | | } |
| | 326 | 56 | | } |
| | | 57 | | |
| | | 58 | | internal static JsonNode? ReadAsJsonNode(ref Utf8JsonReader reader, JsonNodeOptions options) |
| | 0 | 59 | | { |
| | 0 | 60 | | switch (reader.TokenType) |
| | | 61 | | { |
| | | 62 | | case JsonTokenType.String: |
| | | 63 | | case JsonTokenType.False: |
| | | 64 | | case JsonTokenType.True: |
| | | 65 | | case JsonTokenType.Number: |
| | 0 | 66 | | return JsonValueConverter.ReadNonNullPrimitiveValue(ref reader, options); |
| | | 67 | | case JsonTokenType.StartObject: |
| | 0 | 68 | | return JsonObjectConverter.ReadAsJsonNode(ref reader, options); |
| | | 69 | | case JsonTokenType.StartArray: |
| | 0 | 70 | | return JsonArrayConverter.ReadAsJsonNode(ref reader, options); |
| | | 71 | | case JsonTokenType.Null: |
| | 0 | 72 | | return null; |
| | | 73 | | default: |
| | 0 | 74 | | Debug.Assert(false); |
| | 0 | 75 | | throw new JsonException(); |
| | | 76 | | } |
| | 0 | 77 | | } |
| | | 78 | | |
| | | 79 | | public static JsonNode? Create(JsonElement element, JsonNodeOptions? options) |
| | 0 | 80 | | { |
| | | 81 | | JsonNode? node; |
| | | 82 | | |
| | 0 | 83 | | switch (element.ValueKind) |
| | | 84 | | { |
| | | 85 | | case JsonValueKind.Null: |
| | 0 | 86 | | node = null; |
| | 0 | 87 | | break; |
| | | 88 | | case JsonValueKind.Object: |
| | 0 | 89 | | node = new JsonObject(element, options); |
| | 0 | 90 | | break; |
| | | 91 | | case JsonValueKind.Array: |
| | 0 | 92 | | node = new JsonArray(element, options); |
| | 0 | 93 | | break; |
| | | 94 | | default: |
| | 0 | 95 | | node = new JsonValueOfElement(element, options); |
| | 0 | 96 | | break; |
| | | 97 | | } |
| | | 98 | | |
| | 0 | 99 | | return node; |
| | 0 | 100 | | } |
| | | 101 | | |
| | 0 | 102 | | internal override JsonSchema? GetSchema(JsonNumberHandling _) => JsonSchema.CreateTrueSchema(); |
| | | 103 | | } |
| | | 104 | | } |