using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Dynamic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using DPumpHydr.WinFrmUI.WenSkin.Json.Utilities;
namespace DPumpHydr.WinFrmUI.WenSkin.Json.Linq
{
///
/// Represents a JSON object.
///
///
///
///
public class JObject : JContainer, IDictionary, ICollection>, IEnumerable>, IEnumerable, INotifyPropertyChanged, ICustomTypeDescriptor, INotifyPropertyChanging
{
private class JObjectDynamicProxy : DynamicProxy
{
public override bool TryGetMember(JObject instance, GetMemberBinder binder, out object result)
{
result = instance[binder.Name];
return true;
}
public override bool TrySetMember(JObject instance, SetMemberBinder binder, object value)
{
JToken jToken = value as JToken;
if (jToken == null)
{
jToken = new JValue(value);
}
instance[binder.Name] = jToken;
return true;
}
public override IEnumerable GetDynamicMemberNames(JObject instance)
{
return from p in instance.Properties()
select p.Name;
}
}
private readonly JPropertyKeyedCollection _properties = new JPropertyKeyedCollection();
///
/// Gets the container's children tokens.
///
/// The container's children tokens.
protected override IList ChildrenTokens => _properties;
///
/// Gets the node type for this .
///
/// The type.
public override JTokenType Type => JTokenType.Object;
///
/// Gets the with the specified key.
///
/// The with the specified key.
public override JToken this[object key]
{
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];
}
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;
}
}
///
/// Gets or sets the with the specified property name.
///
///
public JToken this[string propertyName]
{
get
{
ValidationUtils.ArgumentNotNull(propertyName, "propertyName");
return Property(propertyName)?.Value;
}
set
{
JProperty jProperty = Property(propertyName);
if (jProperty != null)
{
jProperty.Value = value;
return;
}
OnPropertyChanging(propertyName);
Add(new JProperty(propertyName, value));
OnPropertyChanged(propertyName);
}
}
ICollection IDictionary.Keys => _properties.Keys;
ICollection IDictionary.Values
{
get
{
throw new NotImplementedException();
}
}
bool ICollection>.IsReadOnly => false;
///
/// Occurs when a property value changes.
///
public event PropertyChangedEventHandler PropertyChanged;
///
/// Occurs when a property value is changing.
///
public event PropertyChangingEventHandler PropertyChanging;
///
/// Initializes a new instance of the class.
///
public JObject()
{
}
///
/// Initializes a new instance of the class from another object.
///
/// A object to copy from.
public JObject(JObject other)
: base(other)
{
}
///
/// Initializes a new instance of the class with the specified content.
///
/// The contents of the object.
public JObject(params object[] content)
: this((object)content)
{
}
///
/// Initializes a new instance of the class with the specified content.
///
/// The contents of the object.
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);
}
internal override int IndexOfItem(JToken item)
{
return _properties.IndexOfReference(item);
}
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, 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, JsonMergeSettings settings)
{
JObject jObject = content as JObject;
if (jObject == null)
{
return;
}
foreach (KeyValuePair item in jObject)
{
JProperty jProperty = Property(item.Key);
if (jProperty == null)
{
Add(item.Key, item.Value);
}
else
{
if (item.Value == null)
{
continue;
}
JContainer jContainer = jProperty.Value as JContainer;
if (jContainer == null)
{
if (item.Value.Type != JTokenType.Null || (settings != null && settings.MergeNullValueHandling == MergeNullValueHandling.Merge))
{
jProperty.Value = item.Value;
}
}
else if (jContainer.Type != item.Value.Type)
{
jProperty.Value = item.Value;
}
else
{
jContainer.Merge(item.Value, settings);
}
}
}
}
internal void InternalPropertyChanged(JProperty childProperty)
{
OnPropertyChanged(childProperty.Name);
if (_listChanged != null)
{
OnListChanged(new ListChangedEventArgs(ListChangedType.ItemChanged, IndexOfItem(childProperty)));
}
if (_collectionChanged != null)
{
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, childProperty, childProperty, IndexOfItem(childProperty)));
}
}
internal void InternalPropertyChanging(JProperty childProperty)
{
OnPropertyChanging(childProperty.Name);
}
internal override JToken CloneToken()
{
return new JObject(this);
}
///
/// Gets an of this object's properties.
///
/// An of this object's properties.
public IEnumerable Properties()
{
return _properties.Cast();
}
///
/// Gets a the specified name.
///
/// The property name.
/// A with the specified name or null.
public JProperty Property(string name)
{
if (name == null)
{
return null;
}
_properties.TryGetValue(name, out var value);
return (JProperty)value;
}
///
/// Gets an of this object's property values.
///
/// An of this object's property values.
public JEnumerable PropertyValues()
{
return new JEnumerable(from p in Properties()
select p.Value);
}
///
/// 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 JObject 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 JObject Load(JsonReader reader, 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;
}
///
/// Load a from a string that contains JSON.
///
/// A that contains JSON.
/// A populated from the string that contains JSON.
///
///
///
public new static JObject 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 JObject Parse(string json, JsonLoadSettings settings)
{
using JsonReader jsonReader = new JsonTextReader(new StringReader(json));
JObject 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 JObject 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 JObject FromObject(object o, JsonSerializer jsonSerializer)
{
JToken jToken = JToken.FromObjectInternal(o, jsonSerializer);
if (jToken != null && jToken.Type != JTokenType.Object)
{
throw new ArgumentException("Object serialized to {0}. JObject instance expected.".FormatWith(CultureInfo.InvariantCulture, jToken.Type));
}
return (JObject)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.WriteStartObject();
for (int i = 0; i < _properties.Count; i++)
{
_properties[i].WriteTo(writer, converters);
}
writer.WriteEndObject();
}
///
/// Gets the with the specified property name.
///
/// Name of the property.
/// The with the specified property name.
public JToken GetValue(string propertyName)
{
return GetValue(propertyName, StringComparison.Ordinal);
}
///
/// Gets the with the specified property name.
/// The exact property name will be searched for first and if no matching property is found then
/// the will be used to match a property.
///
/// Name of the property.
/// One of the enumeration values that specifies how the strings will be compared.
/// The with the specified property name.
public JToken GetValue(string propertyName, StringComparison comparison)
{
if (propertyName == null)
{
return null;
}
JProperty jProperty = Property(propertyName);
if (jProperty != null)
{
return jProperty.Value;
}
if (comparison != StringComparison.Ordinal)
{
foreach (JProperty property in _properties)
{
if (string.Equals(property.Name, propertyName, comparison))
{
return property.Value;
}
}
}
return null;
}
///
/// Tries to get the with the specified property name.
/// The exact property name will be searched for first and if no matching property is found then
/// the will be used to match a property.
///
/// Name of the property.
/// The value.
/// One of the enumeration values that specifies how the strings will be compared.
/// true if a value was successfully retrieved; otherwise, false.
public bool TryGetValue(string propertyName, StringComparison comparison, out JToken value)
{
value = GetValue(propertyName, comparison);
return value != null;
}
///
/// Adds the specified property name.
///
/// Name of the property.
/// The value.
public void Add(string propertyName, JToken value)
{
Add(new JProperty(propertyName, value));
}
bool IDictionary.ContainsKey(string key)
{
return _properties.Contains(key);
}
///
/// Removes the property with the specified name.
///
/// Name of the property.
/// true if item was successfully removed; otherwise, false.
public bool Remove(string propertyName)
{
JProperty jProperty = Property(propertyName);
if (jProperty == null)
{
return false;
}
jProperty.Remove();
return true;
}
///
/// Tries the get value.
///
/// Name of the property.
/// The value.
/// true if a value was successfully retrieved; otherwise, false.
public bool TryGetValue(string propertyName, out JToken value)
{
JProperty jProperty = Property(propertyName);
if (jProperty == null)
{
value = null;
return false;
}
value = jProperty.Value;
return true;
}
void ICollection>.Add(KeyValuePair item)
{
Add(new JProperty(item.Key, item.Value));
}
void ICollection>.Clear()
{
RemoveAll();
}
bool ICollection>.Contains(KeyValuePair item)
{
JProperty jProperty = Property(item.Key);
if (jProperty == null)
{
return false;
}
return jProperty.Value == item.Value;
}
void ICollection>.CopyTo(KeyValuePair[] 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(property.Name, property.Value);
num++;
}
}
bool ICollection>.Remove(KeyValuePair item)
{
if (!((ICollection>)this).Contains(item))
{
return false;
}
((IDictionary)this).Remove(item.Key);
return true;
}
internal override int GetDeepHashCode()
{
return ContentsHashCode();
}
///
/// Returns an enumerator that iterates through the collection.
///
///
/// A that can be used to iterate through the collection.
///
public IEnumerator> GetEnumerator()
{
foreach (JProperty property in _properties)
{
yield return new KeyValuePair(property.Name, property.Value);
}
}
///
/// Raises the event with the provided arguments.
///
/// Name of the property.
protected virtual void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
///
/// Raises the event with the provided arguments.
///
/// Name of the property.
protected virtual void OnPropertyChanging(string propertyName)
{
if (this.PropertyChanging != null)
{
this.PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
}
}
///
/// Returns the properties for this instance of a component.
///
///
/// A that represents the properties for this component instance.
///
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
{
return ((ICustomTypeDescriptor)this).GetProperties((Attribute[])null);
}
///
/// Returns the properties for this instance of a component using the attribute array as a filter.
///
/// An array of type that is used as a filter.
///
/// A that represents the filtered properties for this component instance.
///
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
{
PropertyDescriptorCollection propertyDescriptorCollection = new PropertyDescriptorCollection(null);
using IEnumerator> enumerator = GetEnumerator();
while (enumerator.MoveNext())
{
propertyDescriptorCollection.Add(new JPropertyDescriptor(enumerator.Current.Key));
}
return propertyDescriptorCollection;
}
///
/// Returns a collection of custom attributes for this instance of a component.
///
///
/// An containing the attributes for this object.
///
AttributeCollection ICustomTypeDescriptor.GetAttributes()
{
return AttributeCollection.Empty;
}
///
/// Returns the class name of this instance of a component.
///
///
/// The class name of the object, or null if the class does not have a name.
///
string ICustomTypeDescriptor.GetClassName()
{
return null;
}
///
/// Returns the name of this instance of a component.
///
///
/// The name of the object, or null if the object does not have a name.
///
string ICustomTypeDescriptor.GetComponentName()
{
return null;
}
///
/// Returns a type converter for this instance of a component.
///
///
/// A that is the converter for this object, or null if there is no for this object.
///
TypeConverter ICustomTypeDescriptor.GetConverter()
{
return new TypeConverter();
}
///
/// Returns the default event for this instance of a component.
///
///
/// An that represents the default event for this object, or null if this object does not have events.
///
EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()
{
return null;
}
///
/// Returns the default property for this instance of a component.
///
///
/// A that represents the default property for this object, or null if this object does not have properties.
///
PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()
{
return null;
}
///
/// Returns an editor of the specified type for this instance of a component.
///
/// A that represents the editor for this object.
///
/// An of the specified type that is the editor for this object, or null if the editor cannot be found.
///
object ICustomTypeDescriptor.GetEditor(Type editorBaseType)
{
return null;
}
///
/// Returns the events for this instance of a component using the specified attribute array as a filter.
///
/// An array of type that is used as a filter.
///
/// An that represents the filtered events for this component instance.
///
EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes)
{
return EventDescriptorCollection.Empty;
}
///
/// Returns the events for this instance of a component.
///
///
/// An that represents the events for this component instance.
///
EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
{
return EventDescriptorCollection.Empty;
}
///
/// Returns an object that contains the property described by the specified property descriptor.
///
/// A that represents the property whose owner is to be found.
///
/// An that represents the owner of the specified property.
///
object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
{
return null;
}
///
/// Returns the responsible for binding operations performed on this object.
///
/// The expression tree representation of the runtime value.
///
/// The to bind this object.
///
protected override DynamicMetaObject GetMetaObject(Expression parameter)
{
return new DynamicProxyMetaObject(parameter, this, new JObjectDynamicProxy(), dontFallbackFirst: true);
}
}
}