<PackageReference Include="System.Text.Json" Version="10.0.0-preview.2.25163.2" />

JsonArray

Represents a mutable JSON array.
using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using System.Text.Json.Serialization.Converters; using System.Threading; namespace System.Text.Json.Nodes { [System.Runtime.CompilerServices.NullableContext(2)] [System.Runtime.CompilerServices.Nullable(0)] [DebuggerDisplay("JsonArray[{List.Count}]")] [DebuggerTypeProxy(typeof(DebugView))] public sealed class JsonArray : JsonNode, IList<JsonNode>, ICollection<JsonNode>, IEnumerable<JsonNode>, IEnumerable { [ExcludeFromCodeCoverage] private sealed class DebugView { [DebuggerDisplay("{Display,nq}")] private struct DebugViewItem { [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] public JsonNode Value; [DebuggerBrowsable(DebuggerBrowsableState.Never)] public string Display { get { if (Value == null) return "null"; if (Value is JsonValue) return Value.ToJsonString(null); JsonObject jsonObject = Value as JsonObject; if (jsonObject != null) return $"""{jsonObject.Count}"""; JsonArray jsonArray = (JsonArray)Value; return $"""{jsonArray.List.Count}"""; } } } [DebuggerBrowsable(DebuggerBrowsableState.Never)] private readonly JsonArray _node; public string Json => _node.ToJsonString(null); public string Path => _node.GetPath(); [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] private DebugViewItem[] Items { get { DebugViewItem[] array = new DebugViewItem[_node.List.Count]; for (int i = 0; i < _node.List.Count; i++) { array[i].Value = _node.List[i]; } return array; } } public DebugView(JsonArray node) { _node = node; } } private JsonElement? _jsonElement; private List<JsonNode> _list; internal override JsonElement? UnderlyingElement => _jsonElement; [System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] private List<JsonNode> List { get { return _list ?? InitializeList(); } } public int Count => List.Count; bool ICollection<JsonNode>.IsReadOnly { get { return false; } } public JsonArray(JsonNodeOptions? options = default(JsonNodeOptions?)) : base(options) { } public JsonArray(JsonNodeOptions options, [System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] params JsonNode[] items) : base(options) { InitializeFromArray(items); } public JsonArray(JsonNodeOptions options, [System.Runtime.CompilerServices.ParamCollection] [System.Runtime.CompilerServices.ScopedRef] [System.Runtime.CompilerServices.Nullable(new byte[] { 0, 2 })] ReadOnlySpan<JsonNode> items) : base(options) { InitializeFromSpan(items); } public JsonArray([System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] params JsonNode[] items) : base(null) { InitializeFromArray(items); } public JsonArray([System.Runtime.CompilerServices.ParamCollection] [System.Runtime.CompilerServices.ScopedRef] [System.Runtime.CompilerServices.Nullable(new byte[] { 0, 2 })] ReadOnlySpan<JsonNode> items) : base(null) { InitializeFromSpan(items); } private protected override JsonValueKind GetValueKindCore() { return JsonValueKind.Array; } internal override JsonNode DeepCloneCore() { GetUnderlyingRepresentation(out List<JsonNode> list, out JsonElement? jsonElement); if (list == null) { if (!jsonElement.HasValue) return new JsonArray(base.Options); return new JsonArray(jsonElement.Value.Clone(), base.Options); } JsonArray jsonArray = new JsonArray(base.Options) { _list = new List<JsonNode>(list.Count) }; for (int i = 0; i < list.Count; i++) { jsonArray.Add(list[i]?.DeepCloneCore()); } return jsonArray; } internal override bool DeepEqualsCore(JsonNode node) { if (node is JsonObject) return false; JsonValue jsonValue = node as JsonValue; if (jsonValue != null) return jsonValue.DeepEqualsCore(this); JsonArray jsonArray = node as JsonArray; if (jsonArray != null) { List<JsonNode> list = List; List<JsonNode> list2 = jsonArray.List; if (list.Count != list2.Count) return false; for (int i = 0; i < list.Count; i++) { if (!JsonNode.DeepEquals(list[i], list2[i])) return false; } return true; } return false; } internal int GetElementIndex(JsonNode node) { return List.IndexOf(node); } [System.Runtime.CompilerServices.NullableContext(1)] [IteratorStateMachine(typeof(<GetValues>d__13<>))] public IEnumerable<T> GetValues<[System.Runtime.CompilerServices.Nullable(2)] T>() { <GetValues>d__13<T> <GetValues>d__ = new <GetValues>d__13<T>(-2); <GetValues>d__.<>4__this = this; return <GetValues>d__; } private void InitializeFromArray(JsonNode[] items) { List<JsonNode> list = new List<JsonNode>(items); for (int i = 0; i < list.Count; i++) { list[i]?.AssignParent(this); } _list = list; } private void InitializeFromSpan(ReadOnlySpan<JsonNode> items) { List<JsonNode> list = new List<JsonNode>(items.Length); ReadOnlySpan<JsonNode> readOnlySpan = items; foreach (JsonNode item in readOnlySpan) { list.Add(item); } for (int j = 0; j < list.Count; j++) { list[j]?.AssignParent(this); } _list = list; } public static JsonArray Create(JsonElement element, JsonNodeOptions? options = default(JsonNodeOptions?)) { switch (element.ValueKind) { case JsonValueKind.Null: return null; case JsonValueKind.Array: return new JsonArray(element, options); default: throw new InvalidOperationException(System.SR.Format(System.SR.NodeElementWrongType, "Array")); } } internal JsonArray(JsonElement element, JsonNodeOptions? options = default(JsonNodeOptions?)) : base(options) { _jsonElement = element; } [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.")] [System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Creating JsonValue instances with non-primitive types requires generating code at runtime.")] public void Add<T>(T value) { JsonNode item = JsonNode.ConvertFromValue(value, base.Options); Add(item); } private protected override JsonNode GetItem(int index) { return List[index]; } private protected override void SetItem(int index, JsonNode value) { value?.AssignParent(this); DetachParent(List[index]); List[index] = value; } internal override void GetPath(ref System.Text.ValueStringBuilder path, JsonNode child) { base.Parent?.GetPath(ref path, this); if (child != null) { int num = List.IndexOf(child); path.Append('['); path.Append(num.ToString()); path.Append(']'); } } [System.Runtime.CompilerServices.NullableContext(1)] public override void WriteTo(Utf8JsonWriter writer, [System.Runtime.CompilerServices.Nullable(2)] JsonSerializerOptions options = null) { if (writer == null) ThrowHelper.ThrowArgumentNullException("writer"); GetUnderlyingRepresentation(out List<JsonNode> list, out JsonElement? jsonElement); if (list == null && jsonElement.HasValue) jsonElement.Value.WriteTo(writer); else { writer.WriteStartArray(); foreach (JsonNode item in List) { if (item == null) writer.WriteNullValue(); else item.WriteTo(writer, options); } writer.WriteEndArray(); } } private List<JsonNode> InitializeList() { GetUnderlyingRepresentation(out List<JsonNode> list, out JsonElement? jsonElement); if (list == null) { if (jsonElement.HasValue) { JsonElement value = jsonElement.Value; list = new List<JsonNode>(value.GetArrayLength()); foreach (JsonElement item in value.EnumerateArray()) { JsonNode jsonNode = JsonNodeConverter.Create(item, base.Options); jsonNode?.AssignParent(this); list.Add(jsonNode); } } else list = new List<JsonNode>(); _list = list; Interlocked.MemoryBarrier(); _jsonElement = null; } return list; } private void GetUnderlyingRepresentation(out List<JsonNode> list, out JsonElement? jsonElement) { jsonElement = _jsonElement; Interlocked.MemoryBarrier(); list = _list; } public void Add(JsonNode item) { item?.AssignParent(this); List.Add(item); } public void Clear() { List<JsonNode> list = _list; if (list == null) _jsonElement = null; else { for (int i = 0; i < list.Count; i++) { DetachParent(list[i]); } list.Clear(); } } public bool Contains(JsonNode item) { return List.Contains(item); } public int IndexOf(JsonNode item) { return List.IndexOf(item); } public void Insert(int index, JsonNode item) { item?.AssignParent(this); List.Insert(index, item); } public bool Remove(JsonNode item) { if (List.Remove(item)) { DetachParent(item); return true; } return false; } public void RemoveAt(int index) { JsonNode item = List[index]; List.RemoveAt(index); DetachParent(item); } public int RemoveAll([System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] Func<JsonNode, bool> match) { if (match == null) ThrowHelper.ThrowArgumentNullException("match"); return List.RemoveAll(delegate(JsonNode node) { if (match(node)) { DetachParent(node); return true; } return false; }); } public void RemoveRange(int index, int count) { if (index < 0) ThrowHelper.ThrowArgumentOutOfRangeException_NeedNonNegNum("index"); if (count < 0) ThrowHelper.ThrowArgumentOutOfRangeException_NeedNonNegNum("count"); List<JsonNode> list = List; if (list.Count - index < count) ThrowHelper.ThrowArgumentException_InvalidOffLen(); if (count > 0) { for (int i = 0; i < count; i++) { DetachParent(list[index + i]); } list.RemoveRange(index, count); } } void ICollection<JsonNode>.CopyTo(JsonNode[] array, int index) { List.CopyTo(array, index); } [return: System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] public IEnumerator<JsonNode> GetEnumerator() { return List.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return ((IEnumerable)List).GetEnumerator(); } private static void DetachParent(JsonNode item) { if (item != null) item.Parent = null; } } }