| | | 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.Diagnostics.CodeAnalysis; |
| | | 6 | | |
| | | 7 | | namespace System.Text.Json.Nodes |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Defines a primitive JSON value that is wrapping a <see cref="JsonElement"/>. |
| | | 11 | | /// </summary> |
| | | 12 | | internal sealed class JsonValueOfElement : JsonValue<JsonElement> |
| | | 13 | | { |
| | 0 | 14 | | public JsonValueOfElement(JsonElement value, JsonNodeOptions? options) : base(value, options) |
| | 0 | 15 | | { |
| | 0 | 16 | | Debug.Assert(value.ValueKind is JsonValueKind.False or JsonValueKind.True or JsonValueKind.Number or JsonVal |
| | 0 | 17 | | } |
| | | 18 | | |
| | 0 | 19 | | internal override JsonElement? UnderlyingElement => Value; |
| | 0 | 20 | | internal override JsonNode DeepCloneCore() => new JsonValueOfElement(Value.Clone(), Options); |
| | 0 | 21 | | private protected override JsonValueKind GetValueKindCore() => Value.ValueKind; |
| | | 22 | | |
| | | 23 | | internal override bool DeepEqualsCore(JsonNode otherNode) |
| | 0 | 24 | | { |
| | 0 | 25 | | if (otherNode.UnderlyingElement is JsonElement otherElement) |
| | 0 | 26 | | { |
| | 0 | 27 | | return JsonElement.DeepEquals(Value, otherElement); |
| | | 28 | | } |
| | | 29 | | |
| | 0 | 30 | | if (otherNode is JsonValue) |
| | 0 | 31 | | { |
| | | 32 | | // Dispatch to the other value in case it knows |
| | | 33 | | // how to convert JsonElement to its own type. |
| | 0 | 34 | | return otherNode.DeepEqualsCore(this); |
| | | 35 | | } |
| | | 36 | | |
| | 0 | 37 | | return base.DeepEqualsCore(otherNode); |
| | 0 | 38 | | } |
| | | 39 | | |
| | | 40 | | public override TypeToConvert GetValue<TypeToConvert>() |
| | 0 | 41 | | { |
| | 0 | 42 | | if (!TryGetValue(out TypeToConvert? value)) |
| | 0 | 43 | | { |
| | 0 | 44 | | ThrowHelper.ThrowInvalidOperationException_NodeUnableToConvertElement(Value.ValueKind, typeof(TypeToConv |
| | | 45 | | } |
| | | 46 | | |
| | 0 | 47 | | return value; |
| | 0 | 48 | | } |
| | | 49 | | |
| | | 50 | | public override bool TryGetValue<TypeToConvert>([NotNullWhen(true)] out TypeToConvert value) |
| | 0 | 51 | | { |
| | | 52 | | bool success; |
| | | 53 | | |
| | 0 | 54 | | if (Value is TypeToConvert element) |
| | 0 | 55 | | { |
| | 0 | 56 | | value = element; |
| | 0 | 57 | | return true; |
| | | 58 | | } |
| | | 59 | | |
| | 0 | 60 | | switch (Value.ValueKind) |
| | | 61 | | { |
| | | 62 | | case JsonValueKind.Number: |
| | 0 | 63 | | if (typeof(TypeToConvert) == typeof(int) || typeof(TypeToConvert) == typeof(int?)) |
| | 0 | 64 | | { |
| | 0 | 65 | | success = Value.TryGetInt32(out int result); |
| | 0 | 66 | | value = (TypeToConvert)(object)result; |
| | 0 | 67 | | return success; |
| | | 68 | | } |
| | | 69 | | |
| | 0 | 70 | | if (typeof(TypeToConvert) == typeof(long) || typeof(TypeToConvert) == typeof(long?)) |
| | 0 | 71 | | { |
| | 0 | 72 | | success = Value.TryGetInt64(out long result); |
| | 0 | 73 | | value = (TypeToConvert)(object)result; |
| | 0 | 74 | | return success; |
| | | 75 | | } |
| | | 76 | | |
| | 0 | 77 | | if (typeof(TypeToConvert) == typeof(double) || typeof(TypeToConvert) == typeof(double?)) |
| | 0 | 78 | | { |
| | 0 | 79 | | success = Value.TryGetDouble(out double result); |
| | 0 | 80 | | value = (TypeToConvert)(object)result; |
| | 0 | 81 | | return success; |
| | | 82 | | } |
| | | 83 | | |
| | 0 | 84 | | if (typeof(TypeToConvert) == typeof(short) || typeof(TypeToConvert) == typeof(short?)) |
| | 0 | 85 | | { |
| | 0 | 86 | | success = Value.TryGetInt16(out short result); |
| | 0 | 87 | | value = (TypeToConvert)(object)result; |
| | 0 | 88 | | return success; |
| | | 89 | | } |
| | | 90 | | |
| | 0 | 91 | | if (typeof(TypeToConvert) == typeof(decimal) || typeof(TypeToConvert) == typeof(decimal?)) |
| | 0 | 92 | | { |
| | 0 | 93 | | success = Value.TryGetDecimal(out decimal result); |
| | 0 | 94 | | value = (TypeToConvert)(object)result; |
| | 0 | 95 | | return success; |
| | | 96 | | } |
| | | 97 | | |
| | 0 | 98 | | if (typeof(TypeToConvert) == typeof(byte) || typeof(TypeToConvert) == typeof(byte?)) |
| | 0 | 99 | | { |
| | 0 | 100 | | success = Value.TryGetByte(out byte result); |
| | 0 | 101 | | value = (TypeToConvert)(object)result; |
| | 0 | 102 | | return success; |
| | | 103 | | } |
| | | 104 | | |
| | 0 | 105 | | if (typeof(TypeToConvert) == typeof(float) || typeof(TypeToConvert) == typeof(float?)) |
| | 0 | 106 | | { |
| | 0 | 107 | | success = Value.TryGetSingle(out float result); |
| | 0 | 108 | | value = (TypeToConvert)(object)result; |
| | 0 | 109 | | return success; |
| | | 110 | | } |
| | | 111 | | |
| | 0 | 112 | | if (typeof(TypeToConvert) == typeof(uint) || typeof(TypeToConvert) == typeof(uint?)) |
| | 0 | 113 | | { |
| | 0 | 114 | | success = Value.TryGetUInt32(out uint result); |
| | 0 | 115 | | value = (TypeToConvert)(object)result; |
| | 0 | 116 | | return success; |
| | | 117 | | } |
| | | 118 | | |
| | 0 | 119 | | if (typeof(TypeToConvert) == typeof(ushort) || typeof(TypeToConvert) == typeof(ushort?)) |
| | 0 | 120 | | { |
| | 0 | 121 | | success = Value.TryGetUInt16(out ushort result); |
| | 0 | 122 | | value = (TypeToConvert)(object)result; |
| | 0 | 123 | | return success; |
| | | 124 | | } |
| | | 125 | | |
| | 0 | 126 | | if (typeof(TypeToConvert) == typeof(ulong) || typeof(TypeToConvert) == typeof(ulong?)) |
| | 0 | 127 | | { |
| | 0 | 128 | | success = Value.TryGetUInt64(out ulong result); |
| | 0 | 129 | | value = (TypeToConvert)(object)result; |
| | 0 | 130 | | return success; |
| | | 131 | | } |
| | | 132 | | |
| | 0 | 133 | | if (typeof(TypeToConvert) == typeof(sbyte) || typeof(TypeToConvert) == typeof(sbyte?)) |
| | 0 | 134 | | { |
| | 0 | 135 | | success = Value.TryGetSByte(out sbyte result); |
| | 0 | 136 | | value = (TypeToConvert)(object)result; |
| | 0 | 137 | | return success; |
| | | 138 | | } |
| | 0 | 139 | | break; |
| | | 140 | | |
| | | 141 | | case JsonValueKind.String: |
| | 0 | 142 | | if (typeof(TypeToConvert) == typeof(string)) |
| | 0 | 143 | | { |
| | 0 | 144 | | string? result = Value.GetString(); |
| | 0 | 145 | | Debug.Assert(result != null); |
| | 0 | 146 | | value = (TypeToConvert)(object)result; |
| | 0 | 147 | | return true; |
| | | 148 | | } |
| | | 149 | | |
| | 0 | 150 | | if (typeof(TypeToConvert) == typeof(DateTime) || typeof(TypeToConvert) == typeof(DateTime?)) |
| | 0 | 151 | | { |
| | 0 | 152 | | success = Value.TryGetDateTime(out DateTime result); |
| | 0 | 153 | | value = (TypeToConvert)(object)result; |
| | 0 | 154 | | return success; |
| | | 155 | | } |
| | | 156 | | |
| | 0 | 157 | | if (typeof(TypeToConvert) == typeof(DateTimeOffset) || typeof(TypeToConvert) == typeof(DateTimeOffse |
| | 0 | 158 | | { |
| | 0 | 159 | | success = Value.TryGetDateTimeOffset(out DateTimeOffset result); |
| | 0 | 160 | | value = (TypeToConvert)(object)result; |
| | 0 | 161 | | return success; |
| | | 162 | | } |
| | | 163 | | |
| | 0 | 164 | | if (typeof(TypeToConvert) == typeof(Guid) || typeof(TypeToConvert) == typeof(Guid?)) |
| | 0 | 165 | | { |
| | 0 | 166 | | success = Value.TryGetGuid(out Guid result); |
| | 0 | 167 | | value = (TypeToConvert)(object)result; |
| | 0 | 168 | | return success; |
| | | 169 | | } |
| | | 170 | | |
| | 0 | 171 | | if (typeof(TypeToConvert) == typeof(char) || typeof(TypeToConvert) == typeof(char?)) |
| | 0 | 172 | | { |
| | 0 | 173 | | string? result = Value.GetString(); |
| | 0 | 174 | | Debug.Assert(result != null); |
| | 0 | 175 | | if (result.Length == 1) |
| | 0 | 176 | | { |
| | 0 | 177 | | value = (TypeToConvert)(object)result[0]; |
| | 0 | 178 | | return true; |
| | | 179 | | } |
| | 0 | 180 | | } |
| | 0 | 181 | | break; |
| | | 182 | | |
| | | 183 | | case JsonValueKind.True: |
| | | 184 | | case JsonValueKind.False: |
| | 0 | 185 | | if (typeof(TypeToConvert) == typeof(bool) || typeof(TypeToConvert) == typeof(bool?)) |
| | 0 | 186 | | { |
| | 0 | 187 | | value = (TypeToConvert)(object)Value.GetBoolean(); |
| | 0 | 188 | | return true; |
| | | 189 | | } |
| | 0 | 190 | | break; |
| | | 191 | | } |
| | | 192 | | |
| | 0 | 193 | | value = default!; |
| | 0 | 194 | | return false; |
| | 0 | 195 | | } |
| | | 196 | | |
| | | 197 | | public override void WriteTo(Utf8JsonWriter writer, JsonSerializerOptions? options = null) |
| | 0 | 198 | | { |
| | 0 | 199 | | ArgumentNullException.ThrowIfNull(writer); |
| | | 200 | | |
| | 0 | 201 | | Value.WriteTo(writer); |
| | 0 | 202 | | } |
| | | 203 | | } |
| | | 204 | | } |