| | | 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.Collections.Generic; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.Diagnostics.CodeAnalysis; |
| | | 7 | | using System.Text.Json.Serialization.Converters; |
| | | 8 | | using System.Threading; |
| | | 9 | | |
| | | 10 | | namespace System.Text.Json.Nodes |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Represents a mutable JSON array. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <remarks> |
| | | 16 | | /// It is safe to perform multiple concurrent read operations on a <see cref="JsonArray"/>, |
| | | 17 | | /// but issues can occur if the collection is modified while it's being read. |
| | | 18 | | /// </remarks> |
| | | 19 | | [DebuggerDisplay("JsonArray[{List.Count}]")] |
| | | 20 | | [DebuggerTypeProxy(typeof(DebugView))] |
| | | 21 | | public sealed partial class JsonArray : JsonNode |
| | | 22 | | { |
| | | 23 | | private JsonElement? _jsonElement; |
| | | 24 | | private List<JsonNode?>? _list; |
| | | 25 | | |
| | 0 | 26 | | internal override JsonElement? UnderlyingElement => _jsonElement; |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Initializes a new instance of the <see cref="JsonArray"/> class that is empty. |
| | | 30 | | /// </summary> |
| | | 31 | | /// <param name="options">Options to control the behavior.</param> |
| | 0 | 32 | | public JsonArray(JsonNodeOptions? options = null) : base(options) { } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Initializes a new instance of the <see cref="JsonArray"/> class that contains items from the specified par |
| | | 36 | | /// </summary> |
| | | 37 | | /// <param name="options">Options to control the behavior.</param> |
| | | 38 | | /// <param name="items">The items to add to the new <see cref="JsonArray"/>.</param> |
| | 0 | 39 | | public JsonArray(JsonNodeOptions options, params JsonNode?[] items) : base(options) |
| | 0 | 40 | | { |
| | 0 | 41 | | InitializeFromArray(items); |
| | 0 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Initializes a new instance of the <see cref="JsonArray"/> class that contains items from the specified par |
| | | 46 | | /// </summary> |
| | | 47 | | /// <param name="options">Options to control the behavior.</param> |
| | | 48 | | /// <param name="items">The items to add to the new <see cref="JsonArray"/>.</param> |
| | 0 | 49 | | public JsonArray(JsonNodeOptions options, params ReadOnlySpan<JsonNode?> items) : base(options) |
| | 0 | 50 | | { |
| | 0 | 51 | | InitializeFromSpan(items); |
| | 0 | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Initializes a new instance of the <see cref="JsonArray"/> class that contains items from the specified arr |
| | | 56 | | /// </summary> |
| | | 57 | | /// <param name="items">The items to add to the new <see cref="JsonArray"/>.</param> |
| | 0 | 58 | | public JsonArray(params JsonNode?[] items) : base() |
| | 0 | 59 | | { |
| | 0 | 60 | | InitializeFromArray(items); |
| | 0 | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Initializes a new instance of the <see cref="JsonArray"/> class that contains items from the specified spa |
| | | 65 | | /// </summary> |
| | | 66 | | /// <param name="items">The items to add to the new <see cref="JsonArray"/>.</param> |
| | 0 | 67 | | public JsonArray(params ReadOnlySpan<JsonNode?> items) : base() |
| | 0 | 68 | | { |
| | 0 | 69 | | InitializeFromSpan(items); |
| | 0 | 70 | | } |
| | | 71 | | |
| | 0 | 72 | | private protected override JsonValueKind GetValueKindCore() => JsonValueKind.Array; |
| | | 73 | | |
| | | 74 | | internal override JsonNode DeepCloneCore() |
| | 0 | 75 | | { |
| | 0 | 76 | | GetUnderlyingRepresentation(out List<JsonNode?>? list, out JsonElement? jsonElement); |
| | | 77 | | |
| | 0 | 78 | | if (list is null) |
| | 0 | 79 | | { |
| | 0 | 80 | | return jsonElement.HasValue |
| | 0 | 81 | | ? new JsonArray(jsonElement.Value.Clone(), Options) |
| | 0 | 82 | | : new JsonArray(Options); |
| | | 83 | | } |
| | | 84 | | |
| | 0 | 85 | | var jsonArray = new JsonArray(Options) |
| | 0 | 86 | | { |
| | 0 | 87 | | _list = new List<JsonNode?>(list.Count) |
| | 0 | 88 | | }; |
| | | 89 | | |
| | 0 | 90 | | for (int i = 0; i < list.Count; i++) |
| | 0 | 91 | | { |
| | 0 | 92 | | jsonArray.Add(list[i]?.DeepCloneCore()); |
| | 0 | 93 | | } |
| | | 94 | | |
| | 0 | 95 | | return jsonArray; |
| | 0 | 96 | | } |
| | | 97 | | |
| | | 98 | | internal override bool DeepEqualsCore(JsonNode node) |
| | 0 | 99 | | { |
| | 0 | 100 | | switch (node) |
| | | 101 | | { |
| | | 102 | | case JsonObject: |
| | 0 | 103 | | return false; |
| | | 104 | | case JsonValue value: |
| | | 105 | | // JsonValue instances have special comparison semantics, dispatch to their implementation. |
| | 0 | 106 | | return value.DeepEqualsCore(this); |
| | | 107 | | case JsonArray array: |
| | 0 | 108 | | List<JsonNode?> currentList = List; |
| | 0 | 109 | | List<JsonNode?> otherList = array.List; |
| | | 110 | | |
| | 0 | 111 | | if (currentList.Count != otherList.Count) |
| | 0 | 112 | | { |
| | 0 | 113 | | return false; |
| | | 114 | | } |
| | | 115 | | |
| | 0 | 116 | | for (int i = 0; i < currentList.Count; i++) |
| | 0 | 117 | | { |
| | 0 | 118 | | if (!DeepEquals(currentList[i], otherList[i])) |
| | 0 | 119 | | { |
| | 0 | 120 | | return false; |
| | | 121 | | } |
| | 0 | 122 | | } |
| | | 123 | | |
| | 0 | 124 | | return true; |
| | | 125 | | default: |
| | 0 | 126 | | Debug.Fail("Impossible case"); |
| | | 127 | | return false; |
| | | 128 | | } |
| | 0 | 129 | | } |
| | | 130 | | |
| | | 131 | | internal int GetElementIndex(JsonNode? node) |
| | 0 | 132 | | { |
| | 0 | 133 | | return List.IndexOf(node); |
| | 0 | 134 | | } |
| | | 135 | | |
| | | 136 | | /// <summary> |
| | | 137 | | /// Returns an enumerable that wraps calls to <see cref="JsonNode.GetValue{T}"/>. |
| | | 138 | | /// </summary> |
| | | 139 | | /// <typeparam name="T">The type of the value to obtain from the <see cref="JsonValue"/>.</typeparam> |
| | | 140 | | /// <returns>An enumerable iterating over values of the array.</returns> |
| | | 141 | | public IEnumerable<T> GetValues<T>() |
| | 0 | 142 | | { |
| | 0 | 143 | | foreach (JsonNode? item in List) |
| | 0 | 144 | | { |
| | 0 | 145 | | yield return item is null ? (T)(object?)null! : item.GetValue<T>(); |
| | 0 | 146 | | } |
| | 0 | 147 | | } |
| | | 148 | | |
| | | 149 | | private void InitializeFromArray(JsonNode?[] items) |
| | 0 | 150 | | { |
| | 0 | 151 | | var list = new List<JsonNode?>(items); |
| | | 152 | | |
| | 0 | 153 | | for (int i = 0; i < list.Count; i++) |
| | 0 | 154 | | { |
| | 0 | 155 | | list[i]?.AssignParent(this); |
| | 0 | 156 | | } |
| | | 157 | | |
| | 0 | 158 | | _list = list; |
| | 0 | 159 | | } |
| | | 160 | | |
| | | 161 | | private void InitializeFromSpan(ReadOnlySpan<JsonNode?> items) |
| | 0 | 162 | | { |
| | 0 | 163 | | List<JsonNode?> list = new(items.Length); |
| | | 164 | | |
| | | 165 | | #if NET |
| | 0 | 166 | | list.AddRange(items); |
| | | 167 | | #else |
| | | 168 | | foreach (JsonNode? item in items) |
| | | 169 | | { |
| | | 170 | | list.Add(item); |
| | | 171 | | } |
| | | 172 | | #endif |
| | | 173 | | |
| | 0 | 174 | | for (int i = 0; i < list.Count; i++) |
| | 0 | 175 | | { |
| | 0 | 176 | | list[i]?.AssignParent(this); |
| | 0 | 177 | | } |
| | | 178 | | |
| | 0 | 179 | | _list = list; |
| | 0 | 180 | | } |
| | | 181 | | |
| | | 182 | | /// <summary> |
| | | 183 | | /// Initializes a new instance of the <see cref="JsonArray"/> class that contains items from the specified <se |
| | | 184 | | /// </summary> |
| | | 185 | | /// <returns> |
| | | 186 | | /// The new instance of the <see cref="JsonArray"/> class that contains items from the specified <see cref="Js |
| | | 187 | | /// </returns> |
| | | 188 | | /// <param name="element">The <see cref="JsonElement"/>.</param> |
| | | 189 | | /// <param name="options">Options to control the behavior.</param> |
| | | 190 | | /// <exception cref="InvalidOperationException"> |
| | | 191 | | /// The <paramref name="element"/> is not a <see cref="JsonValueKind.Array"/>. |
| | | 192 | | /// </exception> |
| | | 193 | | public static JsonArray? Create(JsonElement element, JsonNodeOptions? options = null) |
| | 0 | 194 | | { |
| | 0 | 195 | | return element.ValueKind switch |
| | 0 | 196 | | { |
| | 0 | 197 | | JsonValueKind.Null => null, |
| | 0 | 198 | | JsonValueKind.Array => new JsonArray(element, options), |
| | 0 | 199 | | _ => throw new InvalidOperationException(SR.Format(SR.NodeElementWrongType, nameof(JsonValueKind.Array)) |
| | 0 | 200 | | }; |
| | 0 | 201 | | } |
| | | 202 | | |
| | 44 | 203 | | internal JsonArray(JsonElement element, JsonNodeOptions? options = null) : base(options) |
| | 44 | 204 | | { |
| | 44 | 205 | | Debug.Assert(element.ValueKind == JsonValueKind.Array); |
| | 44 | 206 | | _jsonElement = element; |
| | 44 | 207 | | } |
| | | 208 | | |
| | | 209 | | /// <summary> |
| | | 210 | | /// Adds an object to the end of the <see cref="JsonArray"/>. |
| | | 211 | | /// </summary> |
| | | 212 | | /// <typeparam name="T">The type of object to be added.</typeparam> |
| | | 213 | | /// <param name="value"> |
| | | 214 | | /// The object to be added to the end of the <see cref="JsonArray"/>. |
| | | 215 | | /// </param> |
| | | 216 | | [RequiresUnreferencedCode(JsonValue.CreateUnreferencedCodeMessage)] |
| | | 217 | | [RequiresDynamicCode(JsonValue.CreateDynamicCodeMessage)] |
| | | 218 | | public void Add<T>(T? value) |
| | 0 | 219 | | { |
| | 0 | 220 | | JsonNode? nodeToAdd = ConvertFromValue(value, Options); |
| | 0 | 221 | | Add(nodeToAdd); |
| | 0 | 222 | | } |
| | | 223 | | |
| | | 224 | | /// <summary> |
| | | 225 | | /// Gets or creates the underlying list containing the element nodes of the array. |
| | | 226 | | /// </summary> |
| | 0 | 227 | | private List<JsonNode?> List => _list ?? InitializeList(); |
| | | 228 | | |
| | | 229 | | private protected override JsonNode? GetItem(int index) |
| | 0 | 230 | | { |
| | 0 | 231 | | return List[index]; |
| | 0 | 232 | | } |
| | | 233 | | |
| | | 234 | | private protected override void SetItem(int index, JsonNode? value) |
| | 0 | 235 | | { |
| | 0 | 236 | | value?.AssignParent(this); |
| | 0 | 237 | | DetachParent(List[index]); |
| | 0 | 238 | | List[index] = value; |
| | 0 | 239 | | } |
| | | 240 | | |
| | | 241 | | internal override void GetPath(ref ValueStringBuilder path, JsonNode? child) |
| | 0 | 242 | | { |
| | 0 | 243 | | Parent?.GetPath(ref path, this); |
| | | 244 | | |
| | 0 | 245 | | if (child != null) |
| | 0 | 246 | | { |
| | 0 | 247 | | int index = List.IndexOf(child); |
| | 0 | 248 | | Debug.Assert(index >= 0); |
| | | 249 | | |
| | 0 | 250 | | path.Append('['); |
| | | 251 | | #if NET |
| | 0 | 252 | | Span<char> chars = stackalloc char[JsonConstants.MaximumFormatUInt32Length]; |
| | 0 | 253 | | bool formatted = ((uint)index).TryFormat(chars, out int charsWritten); |
| | 0 | 254 | | Debug.Assert(formatted); |
| | 0 | 255 | | path.Append(chars.Slice(0, charsWritten)); |
| | | 256 | | #else |
| | | 257 | | path.Append(index.ToString()); |
| | | 258 | | #endif |
| | 0 | 259 | | path.Append(']'); |
| | 0 | 260 | | } |
| | 0 | 261 | | } |
| | | 262 | | |
| | | 263 | | /// <inheritdoc/> |
| | | 264 | | public override void WriteTo(Utf8JsonWriter writer, JsonSerializerOptions? options = null) |
| | 0 | 265 | | { |
| | 0 | 266 | | ArgumentNullException.ThrowIfNull(writer); |
| | | 267 | | |
| | 0 | 268 | | GetUnderlyingRepresentation(out List<JsonNode?>? list, out JsonElement? jsonElement); |
| | | 269 | | |
| | 0 | 270 | | if (list is null && jsonElement.HasValue) |
| | 0 | 271 | | { |
| | 0 | 272 | | jsonElement.Value.WriteTo(writer); |
| | 0 | 273 | | } |
| | | 274 | | else |
| | 0 | 275 | | { |
| | 0 | 276 | | writer.WriteStartArray(); |
| | | 277 | | |
| | 0 | 278 | | foreach (JsonNode? element in List) |
| | 0 | 279 | | { |
| | 0 | 280 | | if (element is null) |
| | 0 | 281 | | { |
| | 0 | 282 | | writer.WriteNullValue(); |
| | 0 | 283 | | } |
| | | 284 | | else |
| | 0 | 285 | | { |
| | 0 | 286 | | element.WriteTo(writer, options); |
| | 0 | 287 | | } |
| | 0 | 288 | | } |
| | | 289 | | |
| | 0 | 290 | | writer.WriteEndArray(); |
| | 0 | 291 | | } |
| | 0 | 292 | | } |
| | | 293 | | |
| | | 294 | | private List<JsonNode?> InitializeList() |
| | 0 | 295 | | { |
| | 0 | 296 | | GetUnderlyingRepresentation(out List<JsonNode?>? list, out JsonElement? jsonElement); |
| | | 297 | | |
| | 0 | 298 | | if (list is null) |
| | 0 | 299 | | { |
| | 0 | 300 | | if (jsonElement.HasValue) |
| | 0 | 301 | | { |
| | 0 | 302 | | JsonElement jElement = jsonElement.Value; |
| | 0 | 303 | | Debug.Assert(jElement.ValueKind == JsonValueKind.Array); |
| | | 304 | | |
| | 0 | 305 | | list = new List<JsonNode?>(jElement.GetArrayLength()); |
| | | 306 | | |
| | 0 | 307 | | foreach (JsonElement element in jElement.EnumerateArray()) |
| | 0 | 308 | | { |
| | 0 | 309 | | JsonNode? node = JsonNodeConverter.Create(element, Options); |
| | 0 | 310 | | node?.AssignParent(this); |
| | 0 | 311 | | list.Add(node); |
| | 0 | 312 | | } |
| | 0 | 313 | | } |
| | | 314 | | else |
| | 0 | 315 | | { |
| | 0 | 316 | | list = new(); |
| | 0 | 317 | | } |
| | | 318 | | |
| | | 319 | | // Ensure _jsonElement is written to after _list |
| | 0 | 320 | | _list = list; |
| | 0 | 321 | | Interlocked.MemoryBarrier(); |
| | 0 | 322 | | _jsonElement = null; |
| | 0 | 323 | | } |
| | | 324 | | |
| | 0 | 325 | | return list; |
| | 0 | 326 | | } |
| | | 327 | | |
| | | 328 | | /// <summary> |
| | | 329 | | /// Provides a coherent view of the underlying representation of the current node. |
| | | 330 | | /// The jsonElement value should be consumed if and only if the list value is null. |
| | | 331 | | /// </summary> |
| | | 332 | | private void GetUnderlyingRepresentation(out List<JsonNode?>? list, out JsonElement? jsonElement) |
| | 0 | 333 | | { |
| | | 334 | | // Because JsonElement cannot be read atomically there might be torn reads, |
| | | 335 | | // however the order of read/write operations guarantees that that's only |
| | | 336 | | // possible if the value of _list is non-null. |
| | 0 | 337 | | jsonElement = _jsonElement; |
| | 0 | 338 | | Interlocked.MemoryBarrier(); |
| | 0 | 339 | | list = _list; |
| | 0 | 340 | | } |
| | | 341 | | |
| | | 342 | | [ExcludeFromCodeCoverage] // Justification = "Design-time" |
| | | 343 | | private sealed class DebugView |
| | | 344 | | { |
| | | 345 | | [DebuggerBrowsable(DebuggerBrowsableState.Never)] |
| | | 346 | | private readonly JsonArray _node; |
| | | 347 | | |
| | | 348 | | public DebugView(JsonArray node) |
| | | 349 | | { |
| | | 350 | | _node = node; |
| | | 351 | | } |
| | | 352 | | |
| | | 353 | | public string Json => _node.ToJsonString(); |
| | | 354 | | public string Path => _node.GetPath(); |
| | | 355 | | |
| | | 356 | | [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] |
| | | 357 | | private DebugViewItem[] Items |
| | | 358 | | { |
| | | 359 | | get |
| | | 360 | | { |
| | | 361 | | DebugViewItem[] properties = new DebugViewItem[_node.List.Count]; |
| | | 362 | | |
| | | 363 | | for (int i = 0; i < _node.List.Count; i++) |
| | | 364 | | { |
| | | 365 | | properties[i].Value = _node.List[i]; |
| | | 366 | | } |
| | | 367 | | |
| | | 368 | | return properties; |
| | | 369 | | } |
| | | 370 | | } |
| | | 371 | | |
| | | 372 | | [DebuggerDisplay("{Display,nq}")] |
| | | 373 | | private struct DebugViewItem |
| | | 374 | | { |
| | | 375 | | [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] |
| | | 376 | | public JsonNode? Value; |
| | | 377 | | |
| | | 378 | | [DebuggerBrowsable(DebuggerBrowsableState.Never)] |
| | | 379 | | public string Display |
| | | 380 | | { |
| | | 381 | | get |
| | | 382 | | { |
| | | 383 | | if (Value == null) |
| | | 384 | | { |
| | | 385 | | return $"null"; |
| | | 386 | | } |
| | | 387 | | |
| | | 388 | | if (Value is JsonValue) |
| | | 389 | | { |
| | | 390 | | return Value.ToJsonString(); |
| | | 391 | | } |
| | | 392 | | |
| | | 393 | | if (Value is JsonObject jsonObject) |
| | | 394 | | { |
| | | 395 | | return $"JsonObject[{jsonObject.Count}]"; |
| | | 396 | | } |
| | | 397 | | |
| | | 398 | | JsonArray jsonArray = (JsonArray)Value; |
| | | 399 | | return $"JsonArray[{jsonArray.List.Count}]"; |
| | | 400 | | } |
| | | 401 | | } |
| | | 402 | | } |
| | | 403 | | } |
| | | 404 | | } |
| | | 405 | | } |