Class JsonObject
Represents a basic JSON object.
public class JsonObject : JsonValue, ICloneable
- Inheritance
-
JsonObject
- Implements
- Derived
- Inherited Members
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()
public JsonObject()
JsonObject(IDictionary<string, JsonValue>)
public JsonObject(IDictionary<string, JsonValue> aValue)
Parameters
aValue
IDictionary<string, JsonValue>
Methods
GetBool(string)
public bool GetBool(string sName)
Parameters
sName
string
Returns
Exceptions
- ArgumentNullException
The parameter sName was
null
.
GetDateIfNotEmpty(string)
public DateTime GetDateIfNotEmpty(string sName)
Parameters
sName
string
Returns
- DateTime
If the property does not exist, or if the property contains a Null or an Undefined value, then
DateTime.MinValue
is returned. If the property contains a Number, then it is interpreted as a tick value. Otherwise an attempt is made to parse it as an ISO date string.
Exceptions
- ArgumentNullException
The parameter
sName
wasnull
.- JsonTypeException
The property did contain a JSON value that cannot be meaningfully converted to a
DateTime
. For example Boolean, Object, or Array.
GetIfNotEmpty(string)
Convenience for conditionally getting a string property.
public string GetIfNotEmpty(string sName)
Parameters
sName
stringName of property to get. Must not be
null
.
Returns
- 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
null
is returned.
Exceptions
- ArgumentNullException
The parameter sName was
null
.
GetIfNotNaN<T>(string, T)
public T GetIfNotNaN<T>(string sName, T nDefaultValue = default)
Parameters
sName
stringnDefaultValue
T
Returns
- T
Type Parameters
T
Remarks
Assumes int as underlying type of T, if T is an enum type.
Exceptions
- ArgumentNullException
The parameter sName was
null
.
GetOwnPropertyNames()
public ICollection<string> GetOwnPropertyNames()
Returns
GetUuidIfNotEmpty(string)
public Guid GetUuidIfNotEmpty(string sName)
Parameters
sName
string
Returns
HasOwnProperty(string)
public bool HasOwnProperty(string sName)
Parameters
sName
string
Returns
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.
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.
protected virtual JsonArray OnCreateChildArray(string sFieldName)
Parameters
sFieldName
stringThe name of the field for which a child array object is needed.
Returns
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.
protected virtual JsonObject OnCreateChildObject(string sFieldName)
Parameters
sFieldName
stringThe name of the field for which a child object is needed.
Returns
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.
public static JsonObject Parse(byte[] vbJson)
Parameters
vbJson
byte[]The JSON to be parsed. If this is is
null
, then this method returnsnull
.
Returns
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.
public static JsonObject Parse(string sJson)
Parameters
sJson
stringThe JSON to be parsed. If this is is
null
or an empty string, then this method returnsnull
.
Returns
- See Also
ParseJson(byte[])
Parse the given vbJson
data, adding all its properties to this
object.
public void ParseJson(byte[] vbJson)
Parameters
vbJson
byte[]The JSON to be parsed. If this is is
null
, then this method does nothing.
- See Also
ParseJson(string)
Parse the given sJson
string, adding all its properties to this
object.
public void ParseJson(string sJson)
Parameters
sJson
stringThe JSON to be parsed. If this is is
null
or an empty string, then this method does nothing.
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.
public void SetIfNotEmpty(string sName, JsonValue aValue)
Parameters
sName
stringName of property to set. Must not be
null
.aValue
JsonValueValue to set for the named property. If this is
null
or an instance of type Null, or an instance of type Undefined, or a null or empty String, then the property will not be set.
Exceptions
- ArgumentNullException
The parameter sName was
null
.
SetUuidIfNotEmpty(string, Guid)
public void SetUuidIfNotEmpty(string sName, Guid tGuid)