JSON
Derived from
JValue
Basic JSON object. Instance of this class contains key-value pairs, where key is always string and values can be any JValue objects. New values can be added, removed and replaced freely.
Constructors
Indexer
Properties
public int Count | Gets the count of key/value pairs in this JSON. | public string[] Keys | Gets all the keys in this JSON object. |
Methods
Static Methods
Constructors
public JSON () Creates new empty JSON object.
public JSON (IDictionary sourceKeysAndValues) Creates new JSON object from c# dictionary. Note that values in dictionary need to be basic c# objects like string, booleans, numbers, or another dictionaries or lists. To turn other classes to JSON object, or for more flexible options, consider using static JSON.Serialize() method. Parameters | sourceKeysAndValues | All the keys and values to be added to this JSON. Keys have to be strings and values have to be either JValue, or any basic c# object that can be changed to JValue. |
Exceptions | JArgumentException | If any exceptions occurs when trying to turn parameter dictionary to new JSON object. | | JArgumentNullException | If parameter is null. | | UnknownObjectTypeException | If any value in dictionary is unsupported type. |
Indexer
public JValue this [string key] Gets or sets the value specified by key. Setting value can do both adding new value or replacing old one with same key. Parameters | key | Key of the value to get or set, can not be null. |
Value | Any JValue object, can not be null but can be JNull. |
Properties
public int Count Gets the count of key/value pairs in this JSON. Value | Count of key/value pairs. |
public string[] Keys Gets all the keys in this JSON object. You can use this to loop through all the keys in this json, for example:
foreach (string key in json.Keys) {...}
Array of returned string keys is copy of internal dictionary keys of this JSON object. This means you can add or remove values from this JSON while looping through the keys. For example:
foreach (string key in json.Keys) { if (ShouldRemove(key)) { json.Remove(key); } }
Value | All the keys in this JSON object. |
Methods
public void Add (string key, object value) Add new key/value pair to this JSON. This method will prevent accidental overwriting old values with same key. If there already is key/value pair with same key, this method will throw an exception. Parameters | key | Key of the value, can not be null. | | value | Value to add. Either any JValue, or any basic c# object that can be changed to JValue (string, bool, numbers and null). |
Exceptions | JArgumentException | If adding parameter value to this JSON would cause circular JSON, meaning this JSON or recursively any of its children would contain this JSON itself. | | JArgumentNullException | If parameter key is null. | | JSONKeyAlreadyExistsException | If parameter key already exists in this JSON. | | ProtectedException | If this JSON object is set protected (read only). | | UnknownObjectTypeException | If parameter value is unsupported type. |
public void AddOrReplace (string key, object value) Add or replace key/value pair in this JSON. Possible previous value with same key is replaced. Parameters | key | Key of the value, can not be null. | | value | Value to set. Either any JValue, or any basic c# object that can be changed to JValue (string, bool, numbers and null). |
Exceptions | JArgumentException | If adding parameter value to this JSON would cause circular JSON, meaning this JSON or recursively any of its children would contain this JSON itself. | | JArgumentNullException | If parameter key or value is null. | | ProtectedException | If this JSON object is set protected (read only). | | UnknownObjectTypeException | If parameter value is unsupported type. |
public Dictionary AsDictionary () Gets the whole JSON object as system dictionary. This is recursive, so if this JSON contains other JSON objects or lists, those will be also changed to system objects. Returns | Dictionary that doesn't contain any TotalJSON objects on any level. |
public void Clear () Remove all the key/value pairs from this JSON, leaving it empty. Exceptions | ProtectedException | If this JSON object is set protected (read only). |
public bool ContainsKey (string keyToSearch) Check whatever this JSON object contains key/value pair with specified key. Parameters | keyToSearch | Key to look for. |
Returns | true if key exists, false otherwise. |
Exceptions | JArgumentNullException | If parameter value is null. |
public string CreatePrettyString () Turns this JSON object to JSON formatted, but easily human readable string. This output is not as tightly packed as string returned by CreateString() but contains all the same data and is still completely valid JSON. This method is just a shortcut for doing:
CreateStringSettings createStringSettings = new CreateStringSettings(); createStringSettings.HumanReadable = true; string output = CreateString(createStringSettings);
Returns | This JSON object as JSON formatted string, containing only basic ascii characters between [32..126], line feeds and tabs. |
public override string CreateString () Turns this JSON object to single JSON formatted string that can be easily stored or sent to another system. String always starts with character '{' and ends to character '}' and contains no linefeeds.
Note that for large JSON objects output may be also very long. For debugging purposes, consider also . Returns | This JSON object as JSON formatted string, containing only basic ascii characters between [32..126] without line feeds. |
public override string CreateString (CreateStringSettings settings) Turns this JSON object to single JSON formatted string using specified settings. Returns | This JSON object as JSON formatted string. |
public void DebugInEditor (string debugName) Adds this JSON object to editor debug window so content of JSON object can be followed in Unity editor when application is running. Choose "Window" -> "Total JSON" -> "JSON Runtime Debug" from Unity editor menu top open this window.
Calling this method outside Unity editor doesn't do anything. Parameters | debugName | Name of this object in debug window. |
public T Deserialize<T> () Deserialize this JSON to object, using default settings.
Typically this is used as opposite operation for Serialize. After Serialize is used to turn object to JSON, this method can be used to turn JSON back to object of same type. Type parameters | T | Type of object that can hold all the content of this JSON. |
Returns | Object of wanted type. |
Exceptions | DeserializeException | If any exceptions occurs when trying to deserialize this JSON to object. |
public T Deserialize<T> (DeserializeSettings deserializeSettings) Deserialize this JSON to object, using specified settings.
Typically this is used as opposite operation for Serialize. After Serialize is used to turn object to JSON, this method can be used to turn JSON back to object of same type. Type parameters | T | Type of object that can hold all the content of this JSON. |
Parameters | deserializeSettings | Specified settings used for deserialization. |
Returns | Object of wanted type. |
Exceptions | DeserializeException | If any exceptions occurs when trying to deserialize this JSON to object. |
public override bool Equals (object anotherObject) Test if another object equals to this object. Always returns false if parameter object is null or it is not instance of JSON. Two JSON objects are equal if both contains same keys and every element mapped to each key equals to each other.
Note that string representation generated using CreateString() may not have identical output from two JSON objects that still equals. This is because key-value pairs may be in different order when they are written to string. Parameters | anotherObject | Another object that is compared to this one. |
Returns | True if objects are equal, false otherwise. |
public JValue Get (string key) Gets value specified by key. This method will cause exception if key/value pair doesn't exist. Returned value may be JSON, JString, JNumber, JBoolean, JArray or JNull. Parameters | key | Key of the value to get. |
Returns | Value mapped to key. Can't be null, but can be JNull. |
Exceptions | JSONKeyNotFoundException | If key doesn't exist in this JSON. |
public bool GetBool (string key) Gets c# bool value specified by key. This method will cause exception if key/value pair doesn't exist or value is not boolean. Parameters | key | Key of the bool value to get. |
Exceptions | JSONKeyNotFoundException | If key doesn't exist in this JSON. | | JValueTypeException | If value mapped to this key is not JBoolean. |
public float GetFloat (string key) Gets floating point number specified by key. This method will cause exception if key/value pair doesn't exist, value is not number or it is outside float range. This method is for convenience. It is equal to json.GetJNumber(key).AsFloat(); If there is need to get number value from JSON in some very specific format, use GetJNumber(key) and then methods provided by JNumber class, for example json.GetJNumber(key).AsDecimal();
Parameters | key | Key of the float value to get. |
Exceptions | JNumberOverflowException | If number stored to this JNumber doesn't fit in float. | | JSONKeyNotFoundException | If key doesn't exist in this JSON. | | JValueNullException | If value mapped to this key is null. | | JValueTypeException | If value mapped to this key is not JNumber. |
public int GetInt (string key) Gets integer number specified by key. This method will cause exception if key/value pair doesn't exist, value is not number or it is outside int range. This method is for convenience. It is equal to json.GetJNumber(key).AsInt(); If there is need to get number value from JSON in some very specific format, use GetJNumber(key) and then methods provided by JNumber class, for example json.GetJNumber(key).AsULong();
Parameters | key | Key of the int value to get. |
Exceptions | JNumberFormatException | If number stored to this JNumber is floating point number. | | JNumberOverflowException | If number stored to this JNumber doesn't fit in int range. | | JSONKeyNotFoundException | If key doesn't exist in this JSON. | | JValueNullException | If value mapped to this key is null. | | JValueTypeException | If value mapped to this key is not JNumber. |
public JArray GetJArray (string key) Gets JArray specified by key. This method will cause exception if key/value pair doesn't exist or value is anything else than JArray or JNull. Parameters | key | Key of the array to get. |
Returns | JArray object or null if key was mapped to JNull. |
Exceptions | JSONKeyNotFoundException | If key doesn't exist in this JSON. | | JValueNullException | If value mapped to this key is JNull. | | JValueTypeException | If value mapped to this key is not JArray. |
public JNumber GetJNumber (string key) Gets JNumber value specified by key. This method will cause exception if key/value pair doesn't exist or value is anything else than JNumber or JNull. Parameters | key | Key of the JNumber value to get. |
Returns | JNumber value or null if key was mapped to JNull. |
Exceptions | JSONKeyNotFoundException | If key doesn't exist in this JSON. | | JValueTypeException | If value mapped to this key is not JNumber or null. |
public JSON GetJSON (string key) Gets JSON value specified by key. This method will cause exception if key/value pair doesn't exist or value is anything else than JSON or JNull. Parameters | key | Key of the JSON value to get. |
Returns | JSON value or null if key was mapped to JNull. |
Exceptions | JSONKeyNotFoundException | If key doesn't exist in this JSON. | | JValueTypeException | If value mapped to this key is not JSON or JNull. |
public string GetString (string key) Gets string value specified by key. This method will cause exception if key/value pair doesn't exist or value is anything else than JString or JNull. Parameters | key | Key of the string value to get. |
Returns | C# string or null if key was mapped to JNull. |
Exceptions | JSONKeyNotFoundException | If key doesn't exist in this JSON. | | JValueTypeException | If value mapped to this key is not JString of JNull. |
public bool IsJNull (string key) Check if value specified by key is JNull. This method throws exception if key doesn't exist. Parameters | key | Key of the value to test. |
Returns | true if value exists and it is JNull, false otherwise. |
Exceptions | JSONKeyNotFoundException | If key doesn't exist in this JSON. |
public bool IsProtected () Check whatever this JSON is protected (read only). If it is, it also means that all its childen are protected, but nothing can be assumed of parents of this object. Returns | true if this object is protected, false otherwise |
public void Remove (string key) Remove value specified by key. This method throws exception if key doesn't exist. Parameters | key | Key of the value to remove. |
Exceptions | JArgumentNullException | If parameter key is null. | | JSONKeyNotFoundException | If parameter key doesn't exist in this JSON. | | ProtectedException | If this JSON object is set protected (read only). |
public void Replace (string key, object value) Replace existing key/value pair in this JSON. If there isn't key/value pair with same key, this method will throw an exception. Parameters | key | Key of the value, can not be null. | | value | Value to set. Either any JValue, or any basic c# object that can be changed to JValue (string, bool, numbers and null). |
Exceptions | JArgumentException | If adding parameter value to this JSON would cause circular JSON, meaning this JSON or recursively any of its children would contain this JSON itself. | | JArgumentNullException | If parameter key or value is null. | | JSONKeyNotFoundException | If parameter key doesn't exist in this JSON. | | ProtectedException | If this JSON object is set protected (read only). | | UnknownObjectTypeException | If parameter value is unsupported type. |
public void SetDebugIDForExceptions (string debugIdForExceptions) Sets debug ID for this JSON object. This ID is added to any possible exception messages that may occur when handling this JSON.
This is typically useful in production builds where only exception message is logged but full stacktrace may not be available. Settings this debug ID helps to identify which JSON object caused exception.
Note that if this JSON object was parsed from string and ParseStringSettings.DebugIDForExceptions for already set during parse, that same ID is copied to this JSON object. Parameters | debugIdForExceptions | Debug ID to add to exception messages. Setting this null will disable this feature. |
public void SetProtected () Sets this JSON and all its childen protected (read only). New values can't be added and old values replaced or removed. Typically this is called only for top level JSON. Protecetion can't be cancelled, once it is set, it will stay.
public override string ToString () Returns compact information of this JSON object as string, for debug purposes.
When you want to get all content of this JSON object in JSON formatted string, use . Returns | Single string with information of this JSON object. |
Static Methods
public static JSON ParseString (string jsonObjectAsSingleString) Turns JSON formatted string to new JSON object. Parameters | jsonObjectAsSingleString | Source JSON string to turn to JSON object. |
Returns | New JSON object, never null. |
Exceptions | JArgumentNullException | If parameter string is null. | | ParseException | If input string can not be parsed to JSON. |
public static JSON ParseString (string jsonObjectAsSingleString, string debugIDForExceptions) Turns JSON formatted string to new JSON object. Debug ID is added to any possible exceptions that may occur during parse or when handling resulting JSON object afterwards. This method is for convenience. This method will create new ParseStringSettings object and set value for DebugIDForExceptions, then call ParseString(string jsonObjectAsSingleString, ParseStringSettings parseStringSettings) method. See ParseStringSettings class for more information. Parameters | jsonObjectAsSingleString | Source JSON string to turn to JSON object. | | debugIDForExceptions | Debug ID that will be added to any possible exception message. This value have no effect to resulting JSON object. |
Returns | New JSON object, never null. |
Exceptions | JArgumentNullException | If parameter JSON string is null. | | ParseException | If input string can not be parsed to JSON. |
public static JSON ParseString (string jsonObjectAsSingleString, ParseStringSettings parseStringSettings) Turns JSON formatted string to new JSON object, using specified settings. Parameters | jsonObjectAsSingleString | Source JSON string where to read. | | parseStringSettings | Settings to be used when parsing the string. |
Returns | New JSON object, never null. |
Exceptions | JArgumentNullException | If parameter string or settings object is null. | | ParseException | If input string can not be parsed to JSON. |
public static JSON[] ParseStringToMultiple (string multipleJsonObjectsAsSingleString) Turns single string to multiple new JSON objects. Source string may contain any amount of JSON objects, including zero. Acceptable input string are for example: '' (empty string, returns JSON array with length of 0) '{"hello":"world"}' (returns JSON array with length of 1, normal ParseString method could be used to this one too) '{}{} {}' (returns JSON array of length 3, all JSON objects in array are empty) Parameters | multipleJsonObjectsAsSingleString | Source string where to read JSON objects. |
Returns | Array of JSON objects. Array may be also empty or contain just one item. Returned value is never null. |
Exceptions | JArgumentNullException | If parameter string object is null. | | ParseException | If input string can not be parsed to JSONs. |
public static JSON[] ParseStringToMultiple (string multipleJsonObjectsAsSingleString, ParseStringSettings parseStringSettings) Turns single string to multiple new JSON objects, using specified settings. Source string may contain any amount of JSON objects, including zero. Parameters | multipleJsonObjectsAsSingleString | Source string where to read JSON objects. | | parseStringSettings | Settings to be used when parsing the string. |
Returns | Array of JSON objects. Array may be also empty or contain just one item. Returned value is never null. |
Exceptions | JArgumentNullException | If parameter string or settings object is null. | | ParseException | If input string can not be parsed to JSONs. |
public static JSON Serialize (object objectToSerialize) Serialize object to JSON object, using default settings.
If parameter object is class or struct, only public fields or fields marked with [SerializeField] are serialized. Parameters | objectToSerialize | Object that can be serialized to JSON without ambiguity. Typically class, struct or dictionary. |
Returns | JSON object containing fields/values from parameter object. |
Exceptions | SerializeException | If any exceptions occurs when trying to serialize object to new JSON. |
public static JSON Serialize (object objectToSerialize, SerializeSettings serializeSettings) Serialize object to JSON object, using specific settings.
If parameter object is class or struct, only public fields or fields marked with [SerializeField] are serialized. Parameters | objectToSerialize | Object that can be serialized to JSON without ambiguity. Typically class, struct or dictionary. | | serializeSettings | Specified settings used for serialization. |
Returns | JSON object containing fields/values from parameter object. |
Exceptions | SerializeException | If any exceptions occurs when trying to serialize object to new JSON. |
|