JsonValue
Represents a mutable JSON value.
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
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.";
[System.Runtime.CompilerServices.NullableContext(1)]
public static JsonValue Create(bool value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
return new JsonValueTrimmable<bool>(value, JsonMetadataServices.BooleanConverter, null);
}
public static JsonValue Create(bool? value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
if (!value.HasValue)
return null;
return new JsonValueTrimmable<bool>(value.Value, JsonMetadataServices.BooleanConverter, null);
}
[System.Runtime.CompilerServices.NullableContext(1)]
public static JsonValue Create(byte value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
return new JsonValueTrimmable<byte>(value, JsonMetadataServices.ByteConverter, null);
}
public static JsonValue Create(byte? value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
if (!value.HasValue)
return null;
return new JsonValueTrimmable<byte>(value.Value, JsonMetadataServices.ByteConverter, null);
}
[System.Runtime.CompilerServices.NullableContext(1)]
public static JsonValue Create(char value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
return new JsonValueTrimmable<char>(value, JsonMetadataServices.CharConverter, null);
}
public static JsonValue Create(char? value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
if (!value.HasValue)
return null;
return new JsonValueTrimmable<char>(value.Value, JsonMetadataServices.CharConverter, null);
}
[System.Runtime.CompilerServices.NullableContext(1)]
public static JsonValue Create(DateTime value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
return new JsonValueTrimmable<DateTime>(value, JsonMetadataServices.DateTimeConverter, null);
}
public static JsonValue Create(DateTime? value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
if (!value.HasValue)
return null;
return new JsonValueTrimmable<DateTime>(value.Value, JsonMetadataServices.DateTimeConverter, null);
}
[System.Runtime.CompilerServices.NullableContext(1)]
public static JsonValue Create(DateTimeOffset value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
return new JsonValueTrimmable<DateTimeOffset>(value, JsonMetadataServices.DateTimeOffsetConverter, null);
}
public static JsonValue Create(DateTimeOffset? value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
if (!value.HasValue)
return null;
return new JsonValueTrimmable<DateTimeOffset>(value.Value, JsonMetadataServices.DateTimeOffsetConverter, null);
}
[System.Runtime.CompilerServices.NullableContext(1)]
public static JsonValue Create(decimal value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
return new JsonValueTrimmable<decimal>(value, JsonMetadataServices.DecimalConverter, null);
}
public static JsonValue Create(decimal? value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
if (!value.HasValue)
return null;
return new JsonValueTrimmable<decimal>(value.Value, JsonMetadataServices.DecimalConverter, null);
}
[System.Runtime.CompilerServices.NullableContext(1)]
public static JsonValue Create(double value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
return new JsonValueTrimmable<double>(value, JsonMetadataServices.DoubleConverter, null);
}
public static JsonValue Create(double? value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
if (!value.HasValue)
return null;
return new JsonValueTrimmable<double>(value.Value, JsonMetadataServices.DoubleConverter, null);
}
[System.Runtime.CompilerServices.NullableContext(1)]
public static JsonValue Create(Guid value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
return new JsonValueTrimmable<Guid>(value, JsonMetadataServices.GuidConverter, null);
}
public static JsonValue Create(Guid? value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
if (!value.HasValue)
return null;
return new JsonValueTrimmable<Guid>(value.Value, JsonMetadataServices.GuidConverter, null);
}
[System.Runtime.CompilerServices.NullableContext(1)]
public static JsonValue Create(short value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
return new JsonValueTrimmable<short>(value, JsonMetadataServices.Int16Converter, null);
}
public static JsonValue Create(short? value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
if (!value.HasValue)
return null;
return new JsonValueTrimmable<short>(value.Value, JsonMetadataServices.Int16Converter, null);
}
[System.Runtime.CompilerServices.NullableContext(1)]
public static JsonValue Create(int value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
return new JsonValueTrimmable<int>(value, JsonMetadataServices.Int32Converter, null);
}
public static JsonValue Create(int? value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
if (!value.HasValue)
return null;
return new JsonValueTrimmable<int>(value.Value, JsonMetadataServices.Int32Converter, null);
}
[System.Runtime.CompilerServices.NullableContext(1)]
public static JsonValue Create(long value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
return new JsonValueTrimmable<long>(value, JsonMetadataServices.Int64Converter, null);
}
public static JsonValue Create(long? value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
if (!value.HasValue)
return null;
return new JsonValueTrimmable<long>(value.Value, JsonMetadataServices.Int64Converter, null);
}
[System.Runtime.CompilerServices.NullableContext(1)]
[CLSCompliant(false)]
public static JsonValue Create(sbyte value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
return new JsonValueTrimmable<sbyte>(value, JsonMetadataServices.SByteConverter, null);
}
[CLSCompliant(false)]
public static JsonValue Create(sbyte? value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
if (!value.HasValue)
return null;
return new JsonValueTrimmable<sbyte>(value.Value, JsonMetadataServices.SByteConverter, null);
}
[System.Runtime.CompilerServices.NullableContext(1)]
public static JsonValue Create(float value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
return new JsonValueTrimmable<float>(value, JsonMetadataServices.SingleConverter, null);
}
public static JsonValue Create(float? value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
if (!value.HasValue)
return null;
return new JsonValueTrimmable<float>(value.Value, JsonMetadataServices.SingleConverter, null);
}
public static JsonValue Create(string value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
if (value == null)
return null;
return new JsonValueTrimmable<string>(value, JsonMetadataServices.StringConverter, null);
}
[System.Runtime.CompilerServices.NullableContext(1)]
[CLSCompliant(false)]
public static JsonValue Create(ushort value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
return new JsonValueTrimmable<ushort>(value, JsonMetadataServices.UInt16Converter, null);
}
[CLSCompliant(false)]
public static JsonValue Create(ushort? value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
if (!value.HasValue)
return null;
return new JsonValueTrimmable<ushort>(value.Value, JsonMetadataServices.UInt16Converter, null);
}
[System.Runtime.CompilerServices.NullableContext(1)]
[CLSCompliant(false)]
public static JsonValue Create(uint value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
return new JsonValueTrimmable<uint>(value, JsonMetadataServices.UInt32Converter, null);
}
[CLSCompliant(false)]
public static JsonValue Create(uint? value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
if (!value.HasValue)
return null;
return new JsonValueTrimmable<uint>(value.Value, JsonMetadataServices.UInt32Converter, null);
}
[System.Runtime.CompilerServices.NullableContext(1)]
[CLSCompliant(false)]
public static JsonValue Create(ulong value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
return new JsonValueTrimmable<ulong>(value, JsonMetadataServices.UInt64Converter, null);
}
[CLSCompliant(false)]
public static JsonValue Create(ulong? value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
if (!value.HasValue)
return null;
return new JsonValueTrimmable<ulong>(value.Value, JsonMetadataServices.UInt64Converter, null);
}
public static JsonValue Create(JsonElement value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
if (value.ValueKind == JsonValueKind.Null)
return null;
VerifyJsonElementIsNotArrayOrObject(ref value);
return new JsonValueTrimmable<JsonElement>(value, JsonMetadataServices.JsonElementConverter, null);
}
public static JsonValue Create(JsonElement? value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
if (!value.HasValue)
return null;
JsonElement element = value.Value;
if (element.ValueKind == JsonValueKind.Null)
return null;
VerifyJsonElementIsNotArrayOrObject(ref element);
return new JsonValueTrimmable<JsonElement>(element, JsonMetadataServices.JsonElementConverter, null);
}
private protected JsonValue(JsonNodeOptions? options = default(JsonNodeOptions?))
: base(options)
{
}
[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.")]
public static JsonValue Create<T>(T value, JsonNodeOptions? options = default(JsonNodeOptions?))
{
if (value == null)
return null;
if (((object)value) is JsonElement) {
JsonElement element = value as JsonElement;
if (element.ValueKind == JsonValueKind.Null)
return null;
VerifyJsonElementIsNotArrayOrObject(ref element);
return new JsonValueTrimmable<JsonElement>(element, JsonMetadataServices.JsonElementConverter, options);
}
return new JsonValueNotTrimmable<T>(value, options);
}
public static JsonValue Create<T>(T value, [System.Runtime.CompilerServices.Nullable(1)] JsonTypeInfo<T> jsonTypeInfo, JsonNodeOptions? options = default(JsonNodeOptions?))
{
if (jsonTypeInfo == null)
throw new ArgumentNullException("jsonTypeInfo");
if (value == null)
return null;
if (((object)value) is JsonElement) {
JsonElement element = value as JsonElement;
if (element.ValueKind == JsonValueKind.Null)
return null;
VerifyJsonElementIsNotArrayOrObject(ref element);
}
return new JsonValueTrimmable<T>(value, jsonTypeInfo, options);
}
internal override void GetPath(List<string> path, JsonNode child)
{
if (base.Parent != null)
base.Parent.GetPath(path, this);
}
public abstract bool TryGetValue<T>([System.Diagnostics.CodeAnalysis.NotNullWhen(true)] out T value);
private static void VerifyJsonElementIsNotArrayOrObject(ref JsonElement element)
{
if (element.ValueKind == JsonValueKind.Object || element.ValueKind == JsonValueKind.Array)
throw new InvalidOperationException(System.SR.NodeElementCannotBeObjectOrArray);
}
}
}