KGy SOFT

JsonObject Class

KGy SOFT JSON Libraries Help
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.
See the Remarks section for details.
Inheritance Hierarchy

SystemObject
  KGySoft.JsonJsonObject

Namespace:  KGySoft.Json
Assembly:  KGySoft.Json (in KGySoft.Json.dll) Version: 2.0.0
Syntax

[SerializableAttribute]
public sealed class JsonObject : IList<JsonProperty>, 
	ICollection<JsonProperty>, IEnumerable<JsonProperty>, IEnumerable, 
	IDictionary<string, JsonValue>, ICollection<KeyValuePair<string, JsonValue>>, 
	IEnumerable<KeyValuePair<string, JsonValue>>

The JsonObject type exposes the following members.

Constructors

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

  NameDescription
Public propertyCount
Gets the number of properties contained in the JsonObject, including possible duplicates and properties with Undefined value.
Public propertyEntries
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.
Public propertyItemInt32
Gets or sets the property at the specified index.
Public propertyItemString
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.
Public propertyKeys
Gets a collection of the property names in this JsonObject. This property returns distinct property names even if there are duplicate keys.
Public propertyValues
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.
Top
Methods

  NameDescription
Public methodAdd(JsonProperty)
Adds an item to this JsonObject.
Public methodAdd(String, JsonValue)
Adds a pair of name and value to this JsonObject.
Public methodClear
Removes all properties from the JsonObject.
Public methodContains
Determines whether the JsonObject contains a property with the specified propertyName.
Public methodCopyTo
Copies the properties of the JsonObject to an Array of JsonProperty elements, starting at the specified arrayIndex.
Public methodEnsureUniqueKeys
Removes possible duplicate keys from the JsonObject, keeping only the last occurrence of each key.
Public methodEquals
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).)
Public methodGetEnumerator
Returns an enumerator that iterates through the JsonObject.
Public methodGetHashCode
Returns a hash code for this JsonObject instance.
(Overrides ObjectGetHashCode.)
Public methodIndexOf
Determines the index of a specific property in the JsonObject.
Public methodInsert
Inserts an item to the JsonObject at the specified index.
Public methodStatic memberParse(String)
Reads a JsonObject from a string that contains JSON object.
Public methodStatic memberParse(TextReader)
Reads a JsonObject from a TextReader that contains a JSON object.
Public methodStatic memberParse(Stream, Encoding)
Reads a JsonObject from a Stream that contains JSON object.
Public methodRemove
Removes one occurrence of the properties with the specific propertyName from the JsonObject.
Public methodRemoveAt
Removes the property from the JsonObject at the specified index.
Public methodToString
Returns a minimized JSON string that represents this JsonObject.
(Overrides ObjectToString.)
Public methodToString(String)
Returns a JSON string that represents this JsonObject.
Public methodTryGetValue
Tries to get the value associated with the specified propertyName from the JsonObject.
Public methodStatic memberTryParse(String, JsonObject)
Tries to read a JsonObject from a string that contains JSON object.
Public methodStatic memberTryParse(TextReader, JsonObject)
Tries to read a JsonObject from a TextReader that contains JSON object.
Public methodStatic memberTryParse(Stream, JsonObject, Encoding)
Tries to read a JsonObject from a Stream that contains JSON object.
Public methodWriteTo(StringBuilder, String)
Writes this JsonObject instance into a JsonObject.
Public methodWriteTo(TextWriter, String)
Writes this JsonObject instance into a TextReader.
Public methodWriteTo(Stream, Encoding, String)
Writes this JsonObject instance into a Stream using the specified encoding.
Top
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 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.

Tip Tip
Populating the JsonObject only by the dictionary constructor or the string indexer ensures that no duplicate property names are added.

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 Tip
See the Remarks section of the JsonValue type for more details and examples.
See Also

Reference

KGySoft.JsonJsonObject