JConstructor
Represents a JSON constructor.
using Newtonsoft.Json.Utilities;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Runtime.CompilerServices;
namespace Newtonsoft.Json.Linq
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public class JConstructor : JContainer
{
[System.Runtime.CompilerServices.Nullable(2)]
private string _name;
private readonly List<JToken> _values = new List<JToken>();
protected override IList<JToken> ChildrenTokens => _values;
[System.Runtime.CompilerServices.Nullable(2)]
public string Name {
[System.Runtime.CompilerServices.NullableContext(2)]
get {
return _name;
}
[System.Runtime.CompilerServices.NullableContext(2)]
set {
_name = value;
}
}
public override JTokenType Type => JTokenType.Constructor;
[System.Runtime.CompilerServices.Nullable(2)]
public override JToken this[object key] {
[return: System.Runtime.CompilerServices.Nullable(2)]
get {
ValidationUtils.ArgumentNotNull(key, "key");
if (!(key is int))
throw new ArgumentException("Accessed JConstructor values with invalid key value: {0}. Argument position index expected.".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.ToString(key)));
int index = (int)key;
return GetItem(index);
}
[param: System.Runtime.CompilerServices.Nullable(2)]
set {
ValidationUtils.ArgumentNotNull(key, "key");
if (!(key is int))
throw new ArgumentException("Set JConstructor values with invalid key value: {0}. Argument position index expected.".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.ToString(key)));
int index = (int)key;
SetItem(index, value);
}
}
[System.Runtime.CompilerServices.NullableContext(2)]
internal override int IndexOfItem(JToken item)
{
if (item == null)
return -1;
return _values.IndexOfReference(item);
}
internal override void MergeItem(object content, [System.Runtime.CompilerServices.Nullable(2)] JsonMergeSettings settings)
{
JConstructor jConstructor = content as JConstructor;
if (jConstructor != null) {
if (jConstructor.Name != null)
Name = jConstructor.Name;
JContainer.MergeEnumerableContent(this, jConstructor, settings);
}
}
public JConstructor()
{
}
public JConstructor(JConstructor other)
: base(other)
{
_name = other.Name;
}
public JConstructor(string name, params object[] content)
: this(name, (object)content)
{
}
public JConstructor(string name, object content)
: this(name)
{
Add(content);
}
public JConstructor(string name)
{
if (name == null)
throw new ArgumentNullException("name");
if (name.Length == 0)
throw new ArgumentException("Constructor name cannot be empty.", "name");
_name = name;
}
internal override bool DeepEquals(JToken node)
{
JConstructor jConstructor = node as JConstructor;
if (jConstructor != null && _name == jConstructor.Name)
return ContentsEqual(jConstructor);
return false;
}
internal override JToken CloneToken()
{
return new JConstructor(this);
}
public override void WriteTo(JsonWriter writer, params JsonConverter[] converters)
{
writer.WriteStartConstructor(_name);
int count = _values.Count;
for (int i = 0; i < count; i++) {
_values[i].WriteTo(writer, converters);
}
writer.WriteEndConstructor();
}
internal override int GetDeepHashCode()
{
return (_name?.GetHashCode() ?? 0) ^ ContentsHashCode();
}
public new static JConstructor Load(JsonReader reader)
{
return Load(reader, null);
}
public new static JConstructor Load(JsonReader reader, [System.Runtime.CompilerServices.Nullable(2)] JsonLoadSettings settings)
{
if (reader.TokenType == JsonToken.None && !reader.Read())
throw JsonReaderException.Create(reader, "Error reading JConstructor from JsonReader.");
reader.MoveToContent();
if (reader.TokenType != JsonToken.StartConstructor)
throw JsonReaderException.Create(reader, "Error reading JConstructor from JsonReader. Current JsonReader item is not a constructor: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
JConstructor jConstructor = new JConstructor((string)reader.Value);
jConstructor.SetLineInfo(reader as IJsonLineInfo, settings);
jConstructor.ReadTokenFrom(reader, settings);
return jConstructor;
}
}
}