JObject
public class JObject : JContainer, IDictionary<string, JToken>, ICollection<KeyValuePair<string, JToken>>, IEnumerable<KeyValuePair<string, JToken>>, IEnumerable, INotifyPropertyChanged, ICustomTypeDescriptor
Represents a JSON object.
using Newtonsoft.Json.Utilities;
using Newtonsoft.Json.Utilities.LinqBridge;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Runtime.CompilerServices;
namespace Newtonsoft.Json.Linq
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public class JObject : JContainer, IDictionary<string, JToken>, ICollection<KeyValuePair<string, JToken>>, IEnumerable<KeyValuePair<string, JToken>>, IEnumerable, INotifyPropertyChanged, ICustomTypeDescriptor
{
private readonly JPropertyKeyedCollection _properties = new JPropertyKeyedCollection();
protected override IList<JToken> ChildrenTokens => _properties;
public override JTokenType Type => JTokenType.Object;
[System.Runtime.CompilerServices.Nullable(2)]
public override JToken this[object key] {
[return: System.Runtime.CompilerServices.Nullable(2)]
get {
ValidationUtils.ArgumentNotNull(key, "key");
string text = key as string;
if (text == null)
throw new ArgumentException("Accessed JObject values with invalid key value: {0}. Object property name expected.".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.ToString(key)));
return this[text];
}
[param: System.Runtime.CompilerServices.Nullable(2)]
set {
ValidationUtils.ArgumentNotNull(key, "key");
string text = key as string;
if (text == null)
throw new ArgumentException("Set JObject values with invalid key value: {0}. Object property name expected.".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.ToString(key)));
this[text] = value;
}
}
[System.Runtime.CompilerServices.Nullable(2)]
public JToken this[string propertyName] {
[return: System.Runtime.CompilerServices.Nullable(2)]
get {
ValidationUtils.ArgumentNotNull(propertyName, "propertyName");
return Property(propertyName, StringComparison.Ordinal)?.Value;
}
[param: System.Runtime.CompilerServices.Nullable(2)]
set {
JProperty jProperty = Property(propertyName, StringComparison.Ordinal);
if (jProperty != null)
jProperty.Value = value;
else {
Add(propertyName, value);
OnPropertyChanged(propertyName);
}
}
}
ICollection<string> IDictionary<string, JToken>.Keys {
get {
return _properties.Keys;
}
}
[System.Runtime.CompilerServices.Nullable(new byte[] {
1,
2
})]
ICollection<JToken> IDictionary<string, JToken>.Values {
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
2
})]
get {
throw new NotImplementedException();
}
}
bool ICollection<KeyValuePair<string, JToken>>.IsReadOnly {
get {
return false;
}
}
[System.Runtime.CompilerServices.Nullable(2)]
[method: System.Runtime.CompilerServices.NullableContext(2)]
[field: System.Runtime.CompilerServices.Nullable(2)]
public event PropertyChangedEventHandler PropertyChanged;
public JObject()
{
}
public JObject(JObject other)
: base(other)
{
}
public JObject(params object[] content)
: this((object)content)
{
}
public JObject(object content)
{
Add(content);
}
internal override bool DeepEquals(JToken node)
{
JObject jObject = node as JObject;
if (jObject == null)
return false;
return _properties.Compare(jObject._properties);
}
[System.Runtime.CompilerServices.NullableContext(2)]
internal override int IndexOfItem(JToken item)
{
if (item == null)
return -1;
return _properties.IndexOfReference(item);
}
[System.Runtime.CompilerServices.NullableContext(2)]
internal override void InsertItem(int index, JToken item, bool skipParentCheck)
{
if (item == null || item.Type != JTokenType.Comment)
base.InsertItem(index, item, skipParentCheck);
}
internal override void ValidateToken(JToken o, [System.Runtime.CompilerServices.Nullable(2)] JToken existing)
{
ValidationUtils.ArgumentNotNull(o, "o");
if (o.Type != JTokenType.Property)
throw new ArgumentException("Can not add {0} to {1}.".FormatWith(CultureInfo.InvariantCulture, o.GetType(), GetType()));
JProperty jProperty = (JProperty)o;
if (existing != null) {
JProperty jProperty2 = (JProperty)existing;
if (jProperty.Name == jProperty2.Name)
return;
}
if (_properties.TryGetValue(jProperty.Name, out existing))
throw new ArgumentException("Can not add property {0} to {1}. Property with the same name already exists on object.".FormatWith(CultureInfo.InvariantCulture, jProperty.Name, GetType()));
}
internal override void MergeItem(object content, [System.Runtime.CompilerServices.Nullable(2)] JsonMergeSettings settings)
{
JObject jObject = content as JObject;
if (jObject != null) {
foreach (KeyValuePair<string, JToken> item in jObject) {
JProperty jProperty = Property(item.Key, settings?.PropertyNameComparison ?? StringComparison.Ordinal);
if (jProperty == null)
Add(item.Key, item.Value);
else if (item.Value != null) {
JContainer jContainer = jProperty.Value as JContainer;
if (jContainer == null || jContainer.Type != item.Value.Type) {
if (!IsNull(item.Value) || (settings != null && settings.MergeNullValueHandling == MergeNullValueHandling.Merge))
jProperty.Value = item.Value;
} else
jContainer.Merge(item.Value, settings);
}
}
}
}
private static bool IsNull(JToken token)
{
if (token.Type == JTokenType.Null)
return true;
JValue jValue = token as JValue;
if (jValue != null && jValue.Value == null)
return true;
return false;
}
internal void InternalPropertyChanged(JProperty childProperty)
{
OnPropertyChanged(childProperty.Name);
if (_listChanged != null)
OnListChanged(new ListChangedEventArgs(ListChangedType.ItemChanged, IndexOfItem(childProperty)));
}
internal void InternalPropertyChanging(JProperty childProperty)
{
}
internal override JToken CloneToken()
{
return new JObject(this);
}
public IEnumerable<JProperty> Properties()
{
return _properties.Cast<JProperty>();
}
[return: System.Runtime.CompilerServices.Nullable(2)]
public JProperty Property(string name)
{
return Property(name, StringComparison.Ordinal);
}
[return: System.Runtime.CompilerServices.Nullable(2)]
public JProperty Property(string name, StringComparison comparison)
{
if (name == null)
return null;
if (_properties.TryGetValue(name, out JToken value))
return (JProperty)value;
if (comparison != StringComparison.Ordinal) {
for (int i = 0; i < _properties.Count; i++) {
JProperty jProperty = (JProperty)_properties[i];
if (string.Equals(jProperty.Name, name, comparison))
return jProperty;
}
}
return null;
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})]
public JEnumerable<JToken> PropertyValues()
{
return new JEnumerable<JToken>(from p in Properties()
select p.Value);
}
public new static JObject Load(JsonReader reader)
{
return Load(reader, null);
}
public new static JObject Load(JsonReader reader, [System.Runtime.CompilerServices.Nullable(2)] JsonLoadSettings settings)
{
ValidationUtils.ArgumentNotNull(reader, "reader");
if (reader.TokenType == JsonToken.None && !reader.Read())
throw JsonReaderException.Create(reader, "Error reading JObject from JsonReader.");
reader.MoveToContent();
if (reader.TokenType != JsonToken.StartObject)
throw JsonReaderException.Create(reader, "Error reading JObject from JsonReader. Current JsonReader item is not an object: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
JObject jObject = new JObject();
jObject.SetLineInfo(reader as IJsonLineInfo, settings);
jObject.ReadTokenFrom(reader, settings);
return jObject;
}
public new static JObject Parse(string json)
{
return Parse(json, null);
}
public new static JObject Parse(string json, [System.Runtime.CompilerServices.Nullable(2)] JsonLoadSettings settings)
{
using (JsonReader jsonReader = new JsonTextReader(new StringReader(json))) {
JObject result = Load(jsonReader, settings);
while (jsonReader.Read()) {
}
return result;
}
}
public new static JObject FromObject(object o)
{
return FromObject(o, JsonSerializer.CreateDefault());
}
public new static JObject FromObject(object o, JsonSerializer jsonSerializer)
{
JToken jToken = JToken.FromObjectInternal(o, jsonSerializer);
if (jToken.Type != JTokenType.Object)
throw new ArgumentException("Object serialized to {0}. JObject instance expected.".FormatWith(CultureInfo.InvariantCulture, jToken.Type));
return (JObject)jToken;
}
public override void WriteTo(JsonWriter writer, params JsonConverter[] converters)
{
writer.WriteStartObject();
for (int i = 0; i < _properties.Count; i++) {
_properties[i].WriteTo(writer, converters);
}
writer.WriteEndObject();
}
[System.Runtime.CompilerServices.NullableContext(2)]
public JToken GetValue(string propertyName)
{
return GetValue(propertyName, StringComparison.Ordinal);
}
[System.Runtime.CompilerServices.NullableContext(2)]
public JToken GetValue(string propertyName, StringComparison comparison)
{
if (propertyName == null)
return null;
return Property(propertyName, comparison)?.Value;
}
public bool TryGetValue(string propertyName, StringComparison comparison, [System.Runtime.CompilerServices.Nullable(2)] [System.Diagnostics.CodeAnalysis.NotNullWhen(true)] out JToken value)
{
value = GetValue(propertyName, comparison);
return value != null;
}
public void Add(string propertyName, [System.Runtime.CompilerServices.Nullable(2)] JToken value)
{
Add(new JProperty(propertyName, value));
}
public bool ContainsKey(string propertyName)
{
ValidationUtils.ArgumentNotNull(propertyName, "propertyName");
return _properties.Contains(propertyName);
}
public bool Remove(string propertyName)
{
JProperty jProperty = Property(propertyName, StringComparison.Ordinal);
if (jProperty == null)
return false;
jProperty.Remove();
return true;
}
public bool TryGetValue(string propertyName, [System.Runtime.CompilerServices.Nullable(2)] [System.Diagnostics.CodeAnalysis.NotNullWhen(true)] out JToken value)
{
JProperty jProperty = Property(propertyName, StringComparison.Ordinal);
if (jProperty == null) {
value = null;
return false;
}
value = jProperty.Value;
return true;
}
void ICollection<KeyValuePair<string, JToken>>.Add([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
2
})] KeyValuePair<string, JToken> item)
{
Add(new JProperty(item.Key, item.Value));
}
void ICollection<KeyValuePair<string, JToken>>.Clear()
{
RemoveAll();
}
bool ICollection<KeyValuePair<string, JToken>>.Contains([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
2
})] KeyValuePair<string, JToken> item)
{
JProperty jProperty = Property(item.Key, StringComparison.Ordinal);
if (jProperty == null)
return false;
return jProperty.Value == item.Value;
}
void ICollection<KeyValuePair<string, JToken>>.CopyTo([System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0,
1,
2
})] KeyValuePair<string, JToken>[] array, int arrayIndex)
{
if (array == null)
throw new ArgumentNullException("array");
if (arrayIndex < 0)
throw new ArgumentOutOfRangeException("arrayIndex", "arrayIndex is less than 0.");
if (arrayIndex >= array.Length && arrayIndex != 0)
throw new ArgumentException("arrayIndex is equal to or greater than the length of array.");
if (base.Count > array.Length - arrayIndex)
throw new ArgumentException("The number of elements in the source JObject is greater than the available space from arrayIndex to the end of the destination array.");
int num = 0;
foreach (JProperty property in _properties) {
array[arrayIndex + num] = new KeyValuePair<string, JToken>(property.Name, property.Value);
num++;
}
}
bool ICollection<KeyValuePair<string, JToken>>.Remove([System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1,
2
})] KeyValuePair<string, JToken> item)
{
if (!((ICollection<KeyValuePair<string, JToken>>)this).Contains(item))
return false;
((IDictionary<string, JToken>)this).Remove(item.Key);
return true;
}
internal override int GetDeepHashCode()
{
return ContentsHashCode();
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0,
1,
2
})]
public IEnumerator<KeyValuePair<string, JToken>> GetEnumerator()
{
foreach (JProperty property in _properties) {
yield return new KeyValuePair<string, JToken>(property.Name, property.Value);
}
}
protected virtual void OnPropertyChanged(string propertyName)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
{
return ((ICustomTypeDescriptor)this).GetProperties((Attribute[])null);
}
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
{
PropertyDescriptor[] array = new PropertyDescriptor[base.Count];
int num = 0;
using (IEnumerator<KeyValuePair<string, JToken>> enumerator = GetEnumerator()) {
while (enumerator.MoveNext()) {
array[num] = new JPropertyDescriptor(enumerator.Current.Key);
num++;
}
}
return new PropertyDescriptorCollection(array);
}
AttributeCollection ICustomTypeDescriptor.GetAttributes()
{
return AttributeCollection.Empty;
}
[System.Runtime.CompilerServices.NullableContext(2)]
string ICustomTypeDescriptor.GetClassName()
{
return null;
}
[System.Runtime.CompilerServices.NullableContext(2)]
string ICustomTypeDescriptor.GetComponentName()
{
return null;
}
TypeConverter ICustomTypeDescriptor.GetConverter()
{
return new TypeConverter();
}
[System.Runtime.CompilerServices.NullableContext(2)]
EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()
{
return null;
}
[System.Runtime.CompilerServices.NullableContext(2)]
PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()
{
return null;
}
[return: System.Runtime.CompilerServices.Nullable(2)]
object ICustomTypeDescriptor.GetEditor(Type editorBaseType)
{
return null;
}
EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes)
{
return EventDescriptorCollection.Empty;
}
EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
{
return EventDescriptorCollection.Empty;
}
[return: System.Runtime.CompilerServices.Nullable(2)]
object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
{
if (pd is JPropertyDescriptor)
return this;
return null;
}
}
}