JsonValue<TValue>
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
namespace System.Text.Json.Nodes
{
[DebuggerDisplay("{ToJsonString(),nq}")]
[DebuggerTypeProxy(typeof(JsonValue<>.DebugView))]
internal abstract class JsonValue<TValue> : JsonValue
{
[ExcludeFromCodeCoverage]
[DebuggerDisplay("{Json,nq}")]
private sealed class DebugView
{
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public JsonValue<TValue> _node;
public string Json => _node.ToJsonString(null);
public string Path => _node.GetPath();
public TValue Value => _node.Value;
public DebugView(JsonValue<TValue> node)
{
_node = node;
}
}
internal readonly TValue Value;
private static readonly JsonValueKind? s_valueKind = DetermineValueKindForType(typeof(TValue));
internal static bool TypeIsSupportedPrimitive => s_valueKind.HasValue;
protected JsonValue(TValue value, JsonNodeOptions? options)
: base(options)
{
Value = value;
}
public override T GetValue<T>()
{
TValue value = this.Value;
if (((object)value) is T)
return value as T;
ThrowHelper.ThrowInvalidOperationException_NodeUnableToConvert(typeof(TValue), typeof(T));
return default(T);
}
public override bool TryGetValue<T>([System.Diagnostics.CodeAnalysis.NotNullWhen(true)] out T value)
{
TValue value2 = this.Value;
if (((object)value2) is T) {
T val = value = (value2 as T);
return true;
}
value = default(T);
return false;
}
private protected static JsonValueKind DetermineValueKind(TValue value)
{
if (((object)value) is bool) {
if (!(value as bool))
return JsonValueKind.False;
return JsonValueKind.True;
}
return s_valueKind.Value;
}
private static JsonValueKind? DetermineValueKindForType(Type type)
{
if (type.IsEnum)
return null;
Type underlyingType = Nullable.GetUnderlyingType(type);
if ((object)underlyingType != null)
return DetermineValueKindForType(underlyingType);
if (!(type == typeof(DateTime)) && !(type == typeof(DateTimeOffset)) && !(type == typeof(TimeSpan)) && !(type == typeof(Guid)) && !(type == typeof(Uri)) && !(type == typeof(Version))) {
switch (Type.GetTypeCode(type)) {
case TypeCode.Boolean:
return JsonValueKind.Undefined;
case TypeCode.SByte:
return JsonValueKind.Number;
case TypeCode.Byte:
return JsonValueKind.Number;
case TypeCode.Int16:
return JsonValueKind.Number;
case TypeCode.UInt16:
return JsonValueKind.Number;
case TypeCode.Int32:
return JsonValueKind.Number;
case TypeCode.UInt32:
return JsonValueKind.Number;
case TypeCode.Int64:
return JsonValueKind.Number;
case TypeCode.UInt64:
return JsonValueKind.Number;
case TypeCode.Single:
return JsonValueKind.Number;
case TypeCode.Double:
return JsonValueKind.Number;
case TypeCode.Decimal:
return JsonValueKind.Number;
case TypeCode.String:
return JsonValueKind.String;
case TypeCode.Char:
return JsonValueKind.String;
default:
return null;
}
}
return JsonValueKind.String;
}
}
}