| | | 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 | | internal sealed class JsonValueConverter : JsonConverter<JsonValue?> |
| | | 11 | | { |
| | | 12 | | public override void Write(Utf8JsonWriter writer, JsonValue? value, JsonSerializerOptions options) |
| | 0 | 13 | | { |
| | 0 | 14 | | if (value is null) |
| | 0 | 15 | | { |
| | 0 | 16 | | writer.WriteNullValue(); |
| | 0 | 17 | | return; |
| | | 18 | | } |
| | | 19 | | |
| | 0 | 20 | | value.WriteTo(writer, options); |
| | 0 | 21 | | } |
| | | 22 | | |
| | | 23 | | public override JsonValue? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| | 634 | 24 | | { |
| | 634 | 25 | | if (reader.TokenType is JsonTokenType.Null) |
| | 0 | 26 | | { |
| | 0 | 27 | | return null; |
| | | 28 | | } |
| | | 29 | | |
| | 634 | 30 | | switch (reader.TokenType) |
| | | 31 | | { |
| | | 32 | | case JsonTokenType.String: |
| | | 33 | | case JsonTokenType.False: |
| | | 34 | | case JsonTokenType.True: |
| | | 35 | | case JsonTokenType.Number: |
| | 406 | 36 | | return ReadNonNullPrimitiveValue(ref reader, options.GetNodeOptions()); |
| | | 37 | | default: |
| | 228 | 38 | | JsonElement element = JsonElement.ParseValue(ref reader, options.AllowDuplicateProperties); |
| | 22 | 39 | | return JsonValue.CreateFromElement(ref element, options.GetNodeOptions()); |
| | | 40 | | } |
| | 304 | 41 | | } |
| | | 42 | | |
| | | 43 | | internal static JsonValue ReadNonNullPrimitiveValue(ref Utf8JsonReader reader, JsonNodeOptions options) |
| | 812 | 44 | | { |
| | 812 | 45 | | Debug.Assert(reader.TokenType is JsonTokenType.String or JsonTokenType.False or JsonTokenType.True or JsonTo |
| | 812 | 46 | | return JsonValueOfJsonPrimitive.CreatePrimitiveValue(ref reader, options); |
| | 608 | 47 | | } |
| | | 48 | | |
| | 0 | 49 | | internal override JsonSchema? GetSchema(JsonNumberHandling _) => JsonSchema.CreateTrueSchema(); |
| | | 50 | | } |
| | | 51 | | } |