| | | 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 | | [DebuggerDisplay("{ToJsonString(),nq}")] |
| | | 10 | | [DebuggerTypeProxy(typeof(JsonValue<>.DebugView))] |
| | | 11 | | internal abstract class JsonValue<TValue> : JsonValue |
| | | 12 | | { |
| | | 13 | | internal readonly TValue Value; // keep as a field for direct access to avoid copies |
| | | 14 | | |
| | 0 | 15 | | protected JsonValue(TValue value, JsonNodeOptions? options) : base(options) |
| | 0 | 16 | | { |
| | 0 | 17 | | Debug.Assert(value != null); |
| | 0 | 18 | | Debug.Assert(value is not JsonElement or JsonElement { ValueKind: not JsonValueKind.Null }); |
| | 0 | 19 | | Debug.Assert(value is not JsonNode); |
| | 0 | 20 | | Value = value; |
| | 0 | 21 | | } |
| | | 22 | | |
| | | 23 | | public override T GetValue<T>() |
| | 0 | 24 | | { |
| | | 25 | | // If no conversion is needed, just return the raw value. |
| | 0 | 26 | | if (Value is T returnValue) |
| | 0 | 27 | | { |
| | 0 | 28 | | return returnValue; |
| | | 29 | | } |
| | | 30 | | |
| | | 31 | | // Currently we do not support other conversions. |
| | | 32 | | // Generics (and also boxing) do not support standard cast operators say from 'long' to 'int', |
| | | 33 | | // so attempting to cast here would throw InvalidCastException. |
| | 0 | 34 | | ThrowHelper.ThrowInvalidOperationException_NodeUnableToConvert(typeof(TValue), typeof(T)); |
| | | 35 | | return default!; |
| | 0 | 36 | | } |
| | | 37 | | |
| | | 38 | | public override bool TryGetValue<T>([NotNullWhen(true)] out T value) |
| | 0 | 39 | | { |
| | | 40 | | // If no conversion is needed, just return the raw value. |
| | 0 | 41 | | if (Value is T returnValue) |
| | 0 | 42 | | { |
| | 0 | 43 | | value = returnValue; |
| | 0 | 44 | | return true; |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | // Currently we do not support other conversions. |
| | | 48 | | // Generics (and also boxing) do not support standard cast operators say from 'long' to 'int', |
| | | 49 | | // so attempting to cast here would throw InvalidCastException. |
| | 0 | 50 | | value = default!; |
| | 0 | 51 | | return false; |
| | 0 | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Whether <typeparamref name="TValue"/> is a built-in type that admits primitive JsonValue representation. |
| | | 56 | | /// </summary> |
| | 0 | 57 | | internal static bool TypeIsSupportedPrimitive => s_valueKind.HasValue; |
| | 0 | 58 | | private static readonly JsonValueKind? s_valueKind = DetermineValueKindForType(typeof(TValue)); |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Determines the JsonValueKind for the value of a built-in type. |
| | | 62 | | /// </summary> |
| | | 63 | | private protected static JsonValueKind DetermineValueKind(TValue value) |
| | 0 | 64 | | { |
| | 0 | 65 | | Debug.Assert(s_valueKind is not null, "Should only be invoked for types that are supported primitives."); |
| | | 66 | | |
| | 0 | 67 | | if (value is bool boolean) |
| | 0 | 68 | | { |
| | | 69 | | // Boolean requires special handling since kind varies by value. |
| | 0 | 70 | | return boolean ? JsonValueKind.True : JsonValueKind.False; |
| | | 71 | | } |
| | | 72 | | |
| | 0 | 73 | | return s_valueKind.Value; |
| | 0 | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Precomputes the JsonValueKind for a given built-in type where possible. |
| | | 78 | | /// </summary> |
| | | 79 | | private static JsonValueKind? DetermineValueKindForType(Type type) |
| | 0 | 80 | | { |
| | 0 | 81 | | if (type.IsEnum) |
| | 0 | 82 | | { |
| | 0 | 83 | | return null; // Can vary depending on converter configuration and value. |
| | | 84 | | } |
| | | 85 | | |
| | 0 | 86 | | if (Nullable.GetUnderlyingType(type) is Type underlyingType) |
| | 0 | 87 | | { |
| | | 88 | | // Because JsonNode excludes null values, we can identify with the value kind of the underlying type. |
| | 0 | 89 | | return DetermineValueKindForType(underlyingType); |
| | | 90 | | } |
| | | 91 | | |
| | 0 | 92 | | if (type == typeof(DateTime) || type == typeof(DateTimeOffset) || type == typeof(TimeSpan) || |
| | 0 | 93 | | #if NET |
| | 0 | 94 | | type == typeof(DateOnly) || type == typeof(TimeOnly) || |
| | 0 | 95 | | #endif |
| | 0 | 96 | | type == typeof(Guid) || type == typeof(Uri) || type == typeof(Version)) |
| | 0 | 97 | | { |
| | 0 | 98 | | return JsonValueKind.String; |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | #if NET |
| | 0 | 102 | | if (type == typeof(Half) || type == typeof(UInt128) || type == typeof(Int128)) |
| | 0 | 103 | | { |
| | 0 | 104 | | return JsonValueKind.Number; |
| | | 105 | | } |
| | | 106 | | #endif |
| | 0 | 107 | | return Type.GetTypeCode(type) switch |
| | 0 | 108 | | { |
| | 0 | 109 | | TypeCode.Boolean => JsonValueKind.Undefined, // Can vary dependending on value. |
| | 0 | 110 | | TypeCode.SByte => JsonValueKind.Number, |
| | 0 | 111 | | TypeCode.Byte => JsonValueKind.Number, |
| | 0 | 112 | | TypeCode.Int16 => JsonValueKind.Number, |
| | 0 | 113 | | TypeCode.UInt16 => JsonValueKind.Number, |
| | 0 | 114 | | TypeCode.Int32 => JsonValueKind.Number, |
| | 0 | 115 | | TypeCode.UInt32 => JsonValueKind.Number, |
| | 0 | 116 | | TypeCode.Int64 => JsonValueKind.Number, |
| | 0 | 117 | | TypeCode.UInt64 => JsonValueKind.Number, |
| | 0 | 118 | | TypeCode.Single => JsonValueKind.Number, |
| | 0 | 119 | | TypeCode.Double => JsonValueKind.Number, |
| | 0 | 120 | | TypeCode.Decimal => JsonValueKind.Number, |
| | 0 | 121 | | TypeCode.String => JsonValueKind.String, |
| | 0 | 122 | | TypeCode.Char => JsonValueKind.String, |
| | 0 | 123 | | _ => null, |
| | 0 | 124 | | }; |
| | 0 | 125 | | } |
| | | 126 | | |
| | | 127 | | [ExcludeFromCodeCoverage] // Justification = "Design-time" |
| | | 128 | | [DebuggerDisplay("{Json,nq}")] |
| | | 129 | | private sealed class DebugView |
| | | 130 | | { |
| | | 131 | | [DebuggerBrowsable(DebuggerBrowsableState.Never)] |
| | | 132 | | public JsonValue<TValue> _node; |
| | | 133 | | |
| | | 134 | | public DebugView(JsonValue<TValue> node) |
| | | 135 | | { |
| | | 136 | | _node = node; |
| | | 137 | | } |
| | | 138 | | |
| | | 139 | | public string Json => _node.ToJsonString(); |
| | | 140 | | public string Path => _node.GetPath(); |
| | | 141 | | public TValue? Value => _node.Value; |
| | | 142 | | } |
| | | 143 | | } |
| | | 144 | | } |