JsonObject Class

Represents a JSON object, interpreted as a string-JsonValue dictionary and also as a list of JsonProperty elements. Use the ToString or WriteTo methods to convert it to JSON.

Definition

Namespace: KGySoft.Json
Assembly: KGySoft.Json (in KGySoft.Json.dll) Version: 3.0.0
C#
[SerializableAttribute]
public sealed class JsonObject : IList<JsonProperty>, 
	ICollection<JsonProperty>, IEnumerable<JsonProperty>, IEnumerable, 
	IDictionary<string, JsonValue>, ICollection<KeyValuePair<string, JsonValue>>, 
	IEnumerable<KeyValuePair<string, JsonValue>>
Inheritance
Object    JsonObject
Implements
ICollectionJsonProperty, ICollectionKeyValuePairString, JsonValue, IDictionaryString, JsonValue, IEnumerableJsonProperty, IEnumerableKeyValuePairString, JsonValue, IListJsonProperty, IEnumerable

Remarks

Just like in JavaScript, the ToString (and WriteTo) methods filter out properties with Undefined values.

Obtaining a nonexistent property by the string indexer returns Undefined, which is also a JavaScript-compatible behavior.

  Note

Using LINQ extension methods on a JsonObject may cause ambiguity due to its list/dictionary duality. It is recommended to perform the LINQ operations on the Entries property so it is not needed to specify the type arguments of the LINQ extension methods.

Due to performance reasons JsonObject allows adding duplicate keys; however, getting the properties by the string indexer retrieves always the lastly set value, just like in JavaScript.

If the JsonObject contains duplicate property names, then the ToString and WriteTo methods dump all of them by default. It's not an issue for JavaScript, which allows parsing such a JSON string where the duplicate keys will have the lastly defined value. But you can explicitly call the EnsureUniqueKeys method to remove the duplicate keys (keeping the lastly defined values) before producing the JSON string.

  Tip

See the Remarks section of the JsonValue type for more details and examples.

Constructors

JsonObject Initializes a new instance of the JsonObject class.
JsonObject(IDictionaryString, JsonValue) Initializes a new instance of the JsonObject class from a dictionary.
JsonObject(IEnumerableJsonProperty, Boolean) Initializes a new instance of the JsonObject class from a collection of JsonProperty items.

Properties

Count Gets the number of properties contained in the JsonObject, including possible duplicates and properties with Undefined value.
Entries Gets the property entries of this JsonObject, including possible duplicates. This property simply returns the self reference. It can be useful to be able to use LINQ extension methods on a JsonObject without ambiguity.
ItemInt32 Gets or sets the property at the specified index.
ItemReadOnlySpanChar Gets the value of a property by name. Using a nonexistent propertyName returns Undefined, just like in JavaScript.
ItemString Gets or sets the value of a property by name. When the indexer is read, using a nonexistent propertyName returns Undefined, just like in JavaScript.
ItemStringSegment Gets the value of a property by name. Using a nonexistent propertyName returns Undefined, just like in JavaScript.
Keys Gets a collection of the property names in this JsonObject. This property returns distinct property names even if there are duplicate keys.
Values Gets a collection of the property values in this JsonObject. If there are duplicate property names, then this property may return more elements than the Keys property. To avoid that call the EnsureUniqueKeys method before getting this property.

Methods

Add(JsonProperty) Adds an item to this JsonObject.
Add(String, JsonValue) Adds a pair of name and value to this JsonObject.
Clear Removes all properties from the JsonObject.
Contains Determines whether the JsonObject contains a property with the specified propertyName.
CopyTo Copies the properties of the JsonObject to an Array of JsonProperty elements, starting at the specified arrayIndex.
EnsureUniqueKeys Removes possible duplicate keys from the JsonObject, keeping only the last occurrence of each key.
Equals Determines whether the specified Object is equal to this instance. This method performs a deep comparison. Allows comparing also to JsonValue instances with Object Type.
(Overrides ObjectEquals(Object))
GetEnumerator Returns an enumerator that iterates through the JsonObject.
GetHashCode Returns a hash code for this JsonObject instance.
(Overrides ObjectGetHashCode)
IndexOf Determines the index of a specific property in the JsonObject.
Insert Inserts an item to the JsonObject at the specified index.
Parse(String) Reads a JsonObject from a string that contains JSON object.
Parse(TextReader) Reads a JsonObject from a TextReader that contains a JSON object.
Parse(Stream, Encoding) Reads a JsonObject from a Stream that contains JSON object.
Remove Removes one occurrence of the properties with the specific propertyName from the JsonObject.
RemoveAt Removes the property from the JsonObject at the specified index.
ToString Returns a minimized JSON string that represents this JsonObject.
(Overrides ObjectToString)
ToString(String) Returns a JSON string that represents this JsonObject.
TryGetValue Tries to get the value associated with the specified propertyName from the JsonObject.
TryParse(String, JsonObject) Tries to read a JsonObject from a string that contains JSON object.
TryParse(TextReader, JsonObject) Tries to read a JsonObject from a TextReader that contains JSON object.
TryParse(Stream, JsonObject, Encoding) Tries to read a JsonObject from a Stream that contains JSON object.
WriteTo(StringBuilder, String) Writes this JsonObject instance into a JsonObject.
WriteTo(TextWriter, String) Writes this JsonObject instance into a TextReader.
WriteTo(Stream, Encoding, String) Writes this JsonObject instance into a Stream using the specified encoding.

See Also