Class JsonObject
Represents a basic JSON object.
Inheritance
Implements
Inherited Members
Namespace: Subsembly.Json
Assembly: Subsembly.Json.dll
Syntax
public class JsonObject : JsonValue, ICloneable
Remarks
It is possible, and recommended, to derive more specialized JSON object classes for particular usages. In order to parse JSON data in a derived JsonObject instance, the method ParseJson(String) shall be used.
If a specialized JSON object may contain nested JSON objects, then the method OnCreateChildObject(String) shall be overridden in order to create a specialized JsonObject instance, based on the field name.
By overriding OnCreateChildObject(String) and OnCreateChildArray(String), a complete specialized object tree can be created by parsing the JSON.
Constructors
JsonObject()
Declaration
public JsonObject()
JsonObject(IDictionary<String, JsonValue>)
Declaration
public JsonObject(IDictionary<string, JsonValue> aValue)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IDictionary<System.String, JsonValue> | aValue |
Methods
GetBool(String)
Declaration
public bool GetBool(string sName)
Parameters
Type | Name | Description |
---|---|---|
System.String | sName |
Returns
Type | Description |
---|---|
System.Boolean |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | The parameter sName was |
GetDateIfNotEmpty(String)
Declaration
public DateTime GetDateIfNotEmpty(string sName)
Parameters
Type | Name | Description |
---|---|---|
System.String | sName |
Returns
Type | Description |
---|---|
System.DateTime | If the property does not exist, or if the property contains a
Null or an Undefined value,
then |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | The parameter |
JsonTypeException | The property did contain a JSON value that cannot be meaningfully converted
to a |
GetIfNotEmpty(String)
Convenience for conditionally getting a string property.
Declaration
public string GetIfNotEmpty(string sName)
Parameters
Type | Name | Description |
---|---|---|
System.String | sName | Name of property to get. Must not be |
Returns
Type | Description |
---|---|
System.String | If the named property exists, then its string value will be returned. If the
property does not exist, or if the property contains a Null
or an Undefined value, then |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | The parameter sName was |
GetIfNotNaN<T>(String, T)
Declaration
public T GetIfNotNaN<T>(string sName, T nDefaultValue = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | sName | |
T | nDefaultValue |
Returns
Type | Description |
---|---|
T |
Type Parameters
Name | Description |
---|---|
T |
Remarks
Assumes int as underlying type of T, if T is an enum type.
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | The parameter sName was |
GetOwnPropertyNames()
Declaration
public ICollection<string> GetOwnPropertyNames()
Returns
Type | Description |
---|---|
System.Collections.Generic.ICollection<System.String> |
GetUuidIfNotEmpty(String)
Declaration
public Guid GetUuidIfNotEmpty(string sName)
Parameters
Type | Name | Description |
---|---|---|
System.String | sName |
Returns
Type | Description |
---|---|
System.Guid |
HasOwnProperty(String)
Declaration
public bool HasOwnProperty(string sName)
Parameters
Type | Name | Description |
---|---|---|
System.String | sName |
Returns
Type | Description |
---|---|
System.Boolean |
OnAfterParseObject()
This is called by the JSON parser after this object was fully parsed. It can be used as a hook to validate the data, or to prepare a cache.
Declaration
protected virtual void OnAfterParseObject()
Remarks
The default implementation does nothing.
OnCreateChildArray(String)
This is called whenever the JSON parser needs a child array object for this object.
Declaration
protected virtual JsonArray OnCreateChildArray(string sFieldName)
Parameters
Type | Name | Description |
---|---|---|
System.String | sFieldName | The name of the field for which a child array object is needed. |
Returns
Type | Description |
---|---|
JsonArray |
Remarks
An override can use this as an opportunity to returned a more specific object, derived from JsonArray. Such a derived object could provide additional convenient properties for accessing the fields of it.
The default implementation just returns a new JsonArray.
OnCreateChildObject(String)
This is called whenever the JSON parser needs a child object for this object.
Declaration
protected virtual JsonObject OnCreateChildObject(string sFieldName)
Parameters
Type | Name | Description |
---|---|---|
System.String | sFieldName | The name of the field for which a child object is needed. |
Returns
Type | Description |
---|---|
JsonObject |
Remarks
An override can use this as an opportunity to returned a more specific object, derived from JsonObject. Such a derived object could provide additional convenient properties for accessing the fields of it.
The default implementation just returns a new JsonObject.
Parse(Byte[])
Creates a new, base JsonObject and fills it with the JSON data from
vbJson
. To parse and load data in an instance derived from
JsonObject, use ParseJson(Byte[]) instead.
Declaration
public static JsonObject Parse(byte[] vbJson)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | vbJson | The JSON to be parsed. If this is is |
Returns
Type | Description |
---|---|
JsonObject |
Parse(String)
Creates a new, base JsonObject and fills it with the JSON data from
sJson
. To parse and load data in an instance derived from
JsonObject, use ParseJson(String) instead.
Declaration
public static JsonObject Parse(string sJson)
Parameters
Type | Name | Description |
---|---|---|
System.String | sJson | The JSON to be parsed. If this is is |
Returns
Type | Description |
---|---|
JsonObject |
See Also
ParseJson(Byte[])
Parse the given vbJson
data, adding all its properties to this
object.
Declaration
public void ParseJson(byte[] vbJson)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | vbJson | The JSON to be parsed. If this is is |
See Also
ParseJson(String)
Parse the given sJson
string, adding all its properties to this
object.
Declaration
public void ParseJson(string sJson)
Parameters
Type | Name | Description |
---|---|---|
System.String | sJson | The JSON to be parsed. If this is is |
Remarks
This JsonObject will receive all the parsed values. The values will be added to this object. Existing values with the same field name are replaced.
See Also
SetIfNotEmpty(String, JsonValue)
Convenience for conditionally setting an object property.
Declaration
public void SetIfNotEmpty(string sName, JsonValue aValue)
Parameters
Type | Name | Description |
---|---|---|
System.String | sName | Name of property to set. Must not be |
JsonValue | aValue | Value to set for the named property. If this is |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | The parameter sName was |
SetUuidIfNotEmpty(String, Guid)
Declaration
public void SetUuidIfNotEmpty(string sName, Guid tGuid)
Parameters
Type | Name | Description |
---|---|---|
System.String | sName | Name of property to set. Must not be |
System.Guid | tGuid | The UUID to set. If this is |