using System; using System.Collections; using System.Collections.Generic; using System.Globalization; using System.IO; using DPumpHydr.WinFrmUI.WenSkin.Json.Utilities; namespace DPumpHydr.WinFrmUI.WenSkin.Json.Linq { /// /// Represents a JSON array. /// /// /// /// public class JArray : JContainer, IList, ICollection, IEnumerable, IEnumerable { private readonly List _values = new List(); /// /// Gets the container's children tokens. /// /// The container's children tokens. protected override IList ChildrenTokens => _values; /// /// Gets the node type for this . /// /// The type. public override JTokenType Type => JTokenType.Array; /// /// Gets the with the specified key. /// /// The with the specified key. public override JToken this[object key] { get { ValidationUtils.ArgumentNotNull(key, "key"); if (!(key is int)) { throw new ArgumentException("Accessed JArray values with invalid key value: {0}. Int32 array index expected.".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.ToString(key))); } return GetItem((int)key); } set { ValidationUtils.ArgumentNotNull(key, "key"); if (!(key is int)) { throw new ArgumentException("Set JArray values with invalid key value: {0}. Int32 array index expected.".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.ToString(key))); } SetItem((int)key, value); } } /// /// Gets or sets the at the specified index. /// /// public JToken this[int index] { get { return GetItem(index); } set { SetItem(index, value); } } /// /// Gets a value indicating whether the is read-only. /// /// true if the is read-only; otherwise, false. public bool IsReadOnly => false; /// /// Initializes a new instance of the class. /// public JArray() { } /// /// Initializes a new instance of the class from another object. /// /// A object to copy from. public JArray(JArray other) : base(other) { } /// /// Initializes a new instance of the class with the specified content. /// /// The contents of the array. public JArray(params object[] content) : this((object)content) { } /// /// Initializes a new instance of the class with the specified content. /// /// The contents of the array. public JArray(object content) { Add(content); } internal override bool DeepEquals(JToken node) { JArray jArray = node as JArray; if (jArray != null) { return ContentsEqual(jArray); } return false; } internal override JToken CloneToken() { return new JArray(this); } /// /// Loads an from a . /// /// A that will be read for the content of the . /// A that contains the JSON that was read from the specified . public new static JArray Load(JsonReader reader) { return Load(reader, null); } /// /// Loads an from a . /// /// A that will be read for the content of the . /// The used to load the JSON. /// If this is null, default load settings will be used. /// A that contains the JSON that was read from the specified . public new static JArray Load(JsonReader reader, JsonLoadSettings settings) { if (reader.TokenType == JsonToken.None && !reader.Read()) { throw JsonReaderException.Create(reader, "Error reading JArray from JsonReader."); } reader.MoveToContent(); if (reader.TokenType != JsonToken.StartArray) { throw JsonReaderException.Create(reader, "Error reading JArray from JsonReader. Current JsonReader item is not an array: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType)); } JArray jArray = new JArray(); jArray.SetLineInfo(reader as IJsonLineInfo, settings); jArray.ReadTokenFrom(reader, settings); return jArray; } /// /// Load a from a string that contains JSON. /// /// A that contains JSON. /// A populated from the string that contains JSON. /// /// /// public new static JArray Parse(string json) { return Parse(json, null); } /// /// Load a from a string that contains JSON. /// /// A that contains JSON. /// The used to load the JSON. /// If this is null, default load settings will be used. /// A populated from the string that contains JSON. /// /// /// public new static JArray Parse(string json, JsonLoadSettings settings) { using JsonReader jsonReader = new JsonTextReader(new StringReader(json)); JArray result = Load(jsonReader, settings); if (jsonReader.Read() && jsonReader.TokenType != JsonToken.Comment) { throw JsonReaderException.Create(jsonReader, "Additional text found in JSON string after parsing content."); } return result; } /// /// Creates a from an object. /// /// The object that will be used to create . /// A with the values of the specified object public new static JArray FromObject(object o) { return FromObject(o, JsonSerializer.CreateDefault()); } /// /// Creates a from an object. /// /// The object that will be used to create . /// The that will be used to read the object. /// A with the values of the specified object public new static JArray FromObject(object o, JsonSerializer jsonSerializer) { JToken jToken = JToken.FromObjectInternal(o, jsonSerializer); if (jToken.Type != JTokenType.Array) { throw new ArgumentException("Object serialized to {0}. JArray instance expected.".FormatWith(CultureInfo.InvariantCulture, jToken.Type)); } return (JArray)jToken; } /// /// Writes this token to a . /// /// A into which this method will write. /// A collection of which will be used when writing the token. public override void WriteTo(JsonWriter writer, params JsonConverter[] converters) { writer.WriteStartArray(); for (int i = 0; i < _values.Count; i++) { _values[i].WriteTo(writer, converters); } writer.WriteEndArray(); } internal override int IndexOfItem(JToken item) { return _values.IndexOfReference(item); } internal override void MergeItem(object content, JsonMergeSettings settings) { IEnumerable enumerable = ((IsMultiContent(content) || content is JArray) ? ((IEnumerable)content) : null); if (enumerable != null) { JContainer.MergeEnumerableContent(this, enumerable, settings); } } /// /// Determines the index of a specific item in the . /// /// The object to locate in the . /// /// The index of if found in the list; otherwise, -1. /// public int IndexOf(JToken item) { return IndexOfItem(item); } /// /// Inserts an item to the at the specified index. /// /// The zero-based index at which should be inserted. /// The object to insert into the . /// /// is not a valid index in the . /// The is read-only. public void Insert(int index, JToken item) { InsertItem(index, item, skipParentCheck: false); } /// /// Removes the item at the specified index. /// /// The zero-based index of the item to remove. /// /// is not a valid index in the . /// The is read-only. public void RemoveAt(int index) { RemoveItemAt(index); } /// /// Returns an enumerator that iterates through the collection. /// /// /// A that can be used to iterate through the collection. /// public IEnumerator GetEnumerator() { return Children().GetEnumerator(); } /// /// Adds an item to the . /// /// The object to add to the . /// The is read-only. public void Add(JToken item) { Add((object)item); } /// /// Removes all items from the . /// /// The is read-only. public void Clear() { ClearItems(); } /// /// Determines whether the contains a specific value. /// /// The object to locate in the . /// /// true if is found in the ; otherwise, false. /// public bool Contains(JToken item) { return ContainsItem(item); } /// /// Copies to. /// /// The array. /// Index of the array. public void CopyTo(JToken[] array, int arrayIndex) { CopyItemsTo(array, arrayIndex); } /// /// Removes the first occurrence of a specific object from the . /// /// The object to remove from the . /// /// true if was successfully removed from the ; otherwise, false. This method also returns false if is not found in the original . /// /// The is read-only. public bool Remove(JToken item) { return RemoveItem(item); } internal override int GetDeepHashCode() { return ContentsHashCode(); } } }