JsonValue
Represents a mutable JSON value.
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
namespace System.Text.Json.Nodes
{
[System.Runtime.CompilerServices.NullableContext(2)]
[System.Runtime.CompilerServices.Nullable(0)]
public abstract class JsonValue : JsonNode
{
internal const string CreateUnreferencedCodeMessage = "Creating JsonValue instances with non-primitive types is not compatible with trimming. It can result in non-primitive types being serialized, which may have their members trimmed.";
internal const string CreateDynamicCodeMessage = "Creating JsonValue instances with non-primitive types requires generating code at runtime.";
[System.Runtime.CompilerServices.NullableContext(1)]
public static JsonValue Create(bool value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
return new JsonValuePrimitive<bool>(value, JsonMetadataServices.BooleanConverter, options);
}
public static JsonValue Create(bool? value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
if (!value.HasValue)
return null;
return new JsonValuePrimitive<bool>(value.Value, JsonMetadataServices.BooleanConverter, options);
}
[System.Runtime.CompilerServices.NullableContext(1)]
public static JsonValue Create(byte value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
return new JsonValuePrimitive<byte>(value, JsonMetadataServices.ByteConverter, options);
}
public static JsonValue Create(byte? value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
if (!value.HasValue)
return null;
return new JsonValuePrimitive<byte>(value.Value, JsonMetadataServices.ByteConverter, options);
}
[System.Runtime.CompilerServices.NullableContext(1)]
public static JsonValue Create(char value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
return new JsonValuePrimitive<char>(value, JsonMetadataServices.CharConverter, options);
}
public static JsonValue Create(char? value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
if (!value.HasValue)
return null;
return new JsonValuePrimitive<char>(value.Value, JsonMetadataServices.CharConverter, options);
}
[System.Runtime.CompilerServices.NullableContext(1)]
public static JsonValue Create(DateTime value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
return new JsonValuePrimitive<DateTime>(value, JsonMetadataServices.DateTimeConverter, options);
}
public static JsonValue Create(DateTime? value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
if (!value.HasValue)
return null;
return new JsonValuePrimitive<DateTime>(value.Value, JsonMetadataServices.DateTimeConverter, options);
}
[System.Runtime.CompilerServices.NullableContext(1)]
public static JsonValue Create(DateTimeOffset value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
return new JsonValuePrimitive<DateTimeOffset>(value, JsonMetadataServices.DateTimeOffsetConverter, options);
}
public static JsonValue Create(DateTimeOffset? value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
if (!value.HasValue)
return null;
return new JsonValuePrimitive<DateTimeOffset>(value.Value, JsonMetadataServices.DateTimeOffsetConverter, options);
}
[System.Runtime.CompilerServices.NullableContext(1)]
public static JsonValue Create(decimal value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
return new JsonValuePrimitive<decimal>(value, JsonMetadataServices.DecimalConverter, options);
}
public static JsonValue Create(decimal? value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
if (!value.HasValue)
return null;
return new JsonValuePrimitive<decimal>(value.Value, JsonMetadataServices.DecimalConverter, options);
}
[System.Runtime.CompilerServices.NullableContext(1)]
public static JsonValue Create(double value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
return new JsonValuePrimitive<double>(value, JsonMetadataServices.DoubleConverter, options);
}
public static JsonValue Create(double? value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
if (!value.HasValue)
return null;
return new JsonValuePrimitive<double>(value.Value, JsonMetadataServices.DoubleConverter, options);
}
[System.Runtime.CompilerServices.NullableContext(1)]
public static JsonValue Create(Guid value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
return new JsonValuePrimitive<Guid>(value, JsonMetadataServices.GuidConverter, options);
}
public static JsonValue Create(Guid? value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
if (!value.HasValue)
return null;
return new JsonValuePrimitive<Guid>(value.Value, JsonMetadataServices.GuidConverter, options);
}
[System.Runtime.CompilerServices.NullableContext(1)]
public static JsonValue Create(short value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
return new JsonValuePrimitive<short>(value, JsonMetadataServices.Int16Converter, options);
}
public static JsonValue Create(short? value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
if (!value.HasValue)
return null;
return new JsonValuePrimitive<short>(value.Value, JsonMetadataServices.Int16Converter, options);
}
[System.Runtime.CompilerServices.NullableContext(1)]
public static JsonValue Create(int value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
return new JsonValuePrimitive<int>(value, JsonMetadataServices.Int32Converter, options);
}
public static JsonValue Create(int? value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
if (!value.HasValue)
return null;
return new JsonValuePrimitive<int>(value.Value, JsonMetadataServices.Int32Converter, options);
}
[System.Runtime.CompilerServices.NullableContext(1)]
public static JsonValue Create(long value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
return new JsonValuePrimitive<long>(value, JsonMetadataServices.Int64Converter, options);
}
public static JsonValue Create(long? value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
if (!value.HasValue)
return null;
return new JsonValuePrimitive<long>(value.Value, JsonMetadataServices.Int64Converter, options);
}
[System.Runtime.CompilerServices.NullableContext(1)]
[CLSCompliant(false)]
public static JsonValue Create(sbyte value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
return new JsonValuePrimitive<sbyte>(value, JsonMetadataServices.SByteConverter, options);
}
[CLSCompliant(false)]
public static JsonValue Create(sbyte? value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
if (!value.HasValue)
return null;
return new JsonValuePrimitive<sbyte>(value.Value, JsonMetadataServices.SByteConverter, options);
}
[System.Runtime.CompilerServices.NullableContext(1)]
public static JsonValue Create(float value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
return new JsonValuePrimitive<float>(value, JsonMetadataServices.SingleConverter, options);
}
public static JsonValue Create(float? value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
if (!value.HasValue)
return null;
return new JsonValuePrimitive<float>(value.Value, JsonMetadataServices.SingleConverter, options);
}
[return: System.Diagnostics.CodeAnalysis.NotNullIfNotNull("value")]
public static JsonValue Create(string value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
if (value == null)
return null;
return new JsonValuePrimitive<string>(value, JsonMetadataServices.StringConverter, options);
}
[System.Runtime.CompilerServices.NullableContext(1)]
[CLSCompliant(false)]
public static JsonValue Create(ushort value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
return new JsonValuePrimitive<ushort>(value, JsonMetadataServices.UInt16Converter, options);
}
[CLSCompliant(false)]
public static JsonValue Create(ushort? value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
if (!value.HasValue)
return null;
return new JsonValuePrimitive<ushort>(value.Value, JsonMetadataServices.UInt16Converter, options);
}
[System.Runtime.CompilerServices.NullableContext(1)]
[CLSCompliant(false)]
public static JsonValue Create(uint value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
return new JsonValuePrimitive<uint>(value, JsonMetadataServices.UInt32Converter, options);
}
[CLSCompliant(false)]
public static JsonValue Create(uint? value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
if (!value.HasValue)
return null;
return new JsonValuePrimitive<uint>(value.Value, JsonMetadataServices.UInt32Converter, options);
}
[System.Runtime.CompilerServices.NullableContext(1)]
[CLSCompliant(false)]
public static JsonValue Create(ulong value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
return new JsonValuePrimitive<ulong>(value, JsonMetadataServices.UInt64Converter, options);
}
[CLSCompliant(false)]
public static JsonValue Create(ulong? value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
if (!value.HasValue)
return null;
return new JsonValuePrimitive<ulong>(value.Value, JsonMetadataServices.UInt64Converter, options);
}
public static JsonValue Create(JsonElement value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
return CreateFromElement(ref value, options);
}
public static JsonValue Create(JsonElement? value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
if (!value.HasValue)
return null;
JsonElement element = value.GetValueOrDefault();
return CreateFromElement(ref element, options);
}
private protected JsonValue(JsonNodeOptions? options)
: base(options)
{
}
public abstract bool TryGetValue<T>([System.Diagnostics.CodeAnalysis.NotNullWhen(true)] out T value);
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Creating JsonValue instances with non-primitive types is not compatible with trimming. It can result in non-primitive types being serialized, which may have their members trimmed. Use the overload that takes a JsonTypeInfo, or make sure all of the required types are preserved.")]
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Creating JsonValue instances with non-primitive types requires generating code at runtime.")]
public static JsonValue Create<T>(T value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
if (value == null)
return null;
if (((object)value) is JsonNode)
ThrowHelper.ThrowArgumentException_NodeValueNotAllowed("value");
if (((object)value) is JsonElement) {
JsonElement element = value as JsonElement;
return CreateFromElement(ref element, options);
}
JsonTypeInfo<T> jsonTypeInfo = (JsonTypeInfo<T>)JsonSerializerOptions.Default.GetTypeInfo(typeof(T));
return CreateFromTypeInfo(value, jsonTypeInfo, options);
}
public static JsonValue Create<T>(T value, [System.Runtime.CompilerServices.Nullable(1)] JsonTypeInfo<T> jsonTypeInfo, JsonNodeOptions? options = default(JsonNodeOptions?))
{
if (jsonTypeInfo == null)
ThrowHelper.ThrowArgumentNullException("jsonTypeInfo");
if (value == null)
return null;
if (((object)value) is JsonNode)
ThrowHelper.ThrowArgumentException_NodeValueNotAllowed("value");
jsonTypeInfo.EnsureConfigured();
if (((object)value) is JsonElement) {
JsonElement element = value as JsonElement;
if (jsonTypeInfo.EffectiveConverter.IsInternalConverter)
return CreateFromElement(ref element, options);
}
return CreateFromTypeInfo(value, jsonTypeInfo, options);
}
internal override bool DeepEqualsCore(JsonNode otherNode)
{
if (GetValueKind() == otherNode.GetValueKind()) {
JsonDocument backingDocument;
JsonElement element = <DeepEqualsCore>g__ToJsonElement|41_0(this, out backingDocument);
JsonDocument backingDocument2;
JsonElement element2 = <DeepEqualsCore>g__ToJsonElement|41_0(otherNode, out backingDocument2);
try {
return JsonElement.DeepEquals(element, element2);
} finally {
backingDocument?.Dispose();
backingDocument2?.Dispose();
}
}
return false;
}
internal sealed override void GetPath(ref System.Text.ValueStringBuilder path, JsonNode child)
{
base.Parent?.GetPath(ref path, this);
}
internal static JsonValue CreateFromTypeInfo<T>(T value, JsonTypeInfo<T> jsonTypeInfo, JsonNodeOptions? options = default(JsonNodeOptions?))
{
if (JsonValue<T>.TypeIsSupportedPrimitive && jsonTypeInfo != null) {
JsonConverter<T> effectiveConverter = jsonTypeInfo.EffectiveConverter;
if (effectiveConverter != null && effectiveConverter.IsInternalConverter && (jsonTypeInfo.EffectiveNumberHandling & JsonNumberHandling.WriteAsString) == JsonNumberHandling.Strict)
return new JsonValuePrimitive<T>(value, jsonTypeInfo.EffectiveConverter, options);
}
return new JsonValueCustomized<T>(value, jsonTypeInfo, options);
}
internal static JsonValue CreateFromElement([In] [System.Runtime.CompilerServices.RequiresLocation] ref JsonElement element, JsonNodeOptions? options = default(JsonNodeOptions?))
{
JsonValueKind valueKind = element.ValueKind;
if (valueKind - 1 <= JsonValueKind.Object) {
ThrowHelper.ThrowInvalidOperationException_NodeElementCannotBeObjectOrArray();
return null;
}
if (valueKind == JsonValueKind.Null)
return null;
return new JsonValueOfElement(element, options);
}
}
}