TROJSONObject

Overview

The TROJSONObject class is created to represent a JSON object, which is an unordered set of name/value pairs. This class is used internally by the JSON (de)serializer inside the TROJSONMessage class.
Users are not required to deal with JSON directly, so there is no need to use this class.

Location

 

constructor Create

Creates a new instance.

constructor Create

Add

Adds a new empty name/value pair (property) object to the collection and returns it for further processing.

function Add: TROJSONProperty

AddArrayProperty

Adds a new JSON property whose value is a JSON array. The property object is returned for further processing.

function AddArrayProperty(const AName: JSON_String): TROJSONProperty

Parameters:

  • AName: Name of the property

AddBooleanProperty

Adds a new JSON property with a boolean value. The property object is returned for further processing.

function AddBooleanProperty(const AName: JSON_String; const AValue: Boolean): TROJSONProperty

Parameters:

  • AName: Name of the property
  • AValue: Value of the property

AddNullProperty

Adds a new JSON property with a null value. The property object is returned for further processing.

function AddNullProperty(const AName: JSON_String): TROJSONProperty

Parameters:

  • AName: Name of the property

AddNumberProperty

Adds a new JSON property with a number value. The property object is returned for further processing.

function AddNumberProperty(const AName: JSON_String; const AValue: Variant): TROJSONProperty

Parameters:

  • AName: Name of the property
  • AValue: Value of the property; must contain all values that can be converted to numbers

AddObjectProperty

Adds a new JSON property whose value is a JSON object. The property object is returned for further processing.

function AddObjectProperty(const AName: JSON_String): TROJSONProperty

Parameters:

  • AName: Name of the property

AddStringProperty

Adds a new JSON property with a string value. The property object is returned for further processing.

function AddStringProperty(const AName: JSON_String; const aValue: JSON_String): TROJSONProperty

Parameters:

  • AName: Name of the property
  • aValue: Value of the property

AddVariantProperty

Adds a new JSON property whose value type is unknown. The property object is returned for further processing.

function AddVariantProperty(const AName: JSON_String; const AValue: Variant): TROJSONProperty

Parameters:

  • AName: Name of the property.
  • AValue: Value of the property; must contain all values that can be converted to strings

FindItem

Locates a property by name. Returns the property object or nil if the property cannot be found.

function FindItem(const AName: JSON_String): TROJSONProperty

Parameters:

  • AName: Name of the property

GetArrayItemByName

Locates a property by name, then checks if this property is an array. If the property is not found, this method can create it depending on the ** ACreate** parameter. If the property is not an array, an exception is raised. Returns the found or created property or nil.

function GetArrayItemByName(const AName: JSON_String; ACreate: Boolean): TROJSONArray

Parameters:

  • AName: Name of the property
  • ACreate: Specifies wether to create a new property if the property with the specified name cannot be found.

GetBooleanValueByName

Locates a property by name, then checks if this property has a boolean value. If the property is not boolean, an exception is raised. Returns the property value or false if the property with the specified name cannot be found.

function GetBooleanValueByName(const AName: JSON_String): Boolean

Parameters:

  • AName: Name of the property

GetItems  protected

The Items property read accessor function.

function GetItems(Index: Integer): TROJSONProperty

Parameters:

  • Index:

GetNumberValueByName

Locates a property by name, then checks if this property has a number value. If the property is not a number, an exception is raised. Returns the property value or null if the property with the specified name cannot be found.

function GetNumberValueByName(const AName: JSON_String): Variant

Parameters:

  • AName: Name of the property

GetObjectItemByName

Locates a property by name, then checks if this property has a JSON object value. If the property is not a JSON object, an exception is raised. Returns the property value or null if the property with the specified name cannot be found. The method can create a new object depending on the ** ACreate** parameter value.

function GetObjectItemByName(const AName: JSON_String; ACreate: Boolean): TROJSONObject

Parameters:

  • AName: Name of the property
  • ACreate: Specifies wether to create a new property if the property with the specified name cannot be found.

GetStringValueByName

Locates a property by name then checks if this property has a string value. If the property does not have a string value, an exception is raised. Returns the property value or null if the property with the specified name cannot be found.

function GetStringValueByName(const AName: JSON_String): JSON_String

Parameters:

  • AName: Name of the property

GetVariantValueByName

Returns any value by name, if the property with the specified name cannot be found, an empty string is returned.

function GetVariantValueByName(const AName: JSON_String): Variant

Parameters:

  • AName: Name of the property

IndexOf

Return an index of the property with the given name. If the property with the specified name cannot be found, -1 is returned.

function IndexOf(const AName: JSON_String): integer

Parameters:

  • AName: Name of the property

ItemByName

Similar to FindItem, but raises an exception if the property with the specified name cannot be found.

function ItemByName(const AName: JSON_String): TROJSONProperty

Parameters:

  • AName: Name of the property

Items

Provides access to the name/value pairs that the JSON object consists of by index. The inherited Count property is used to get the pairs count.

property Items[Index: Integer]: TROJSONProperty read write

LoadFromStream

Deserializes the object from the stream.

procedure LoadFromStream(AStream: TStream; AUTF8Stream: boolean)

Parameters:

  • AStream: Source stream
  • AUTF8Stream: If the data in the stream is UTF-8 encoded, this parameter should be set to true, otherwise to false.

SaveToStream

Serializes the object to the stream.

procedure SaveToStream(AStream: TStream; AUTF8Stream: boolean)

Parameters:

  • AStream: Target stream
  • AUTF8Stream: Set this parameter to true to use UTF-8 encoding to serialize the data.

SetItems  protected

The Items property write accessor function.

procedure SetItems(Index: Integer; const Value: TROJSONProperty)

Parameters:

  • Index:
  • Value:

 

Items

Provides access to the name/value pairs that the JSON object consists of by index. The inherited Count property is used to get the pairs count.

property Items[Index: Integer]: TROJSONProperty read write

 

constructor Create

Creates a new instance.

constructor Create

Add

Adds a new empty name/value pair (property) object to the collection and returns it for further processing.

function Add: TROJSONProperty

AddArrayProperty

Adds a new JSON property whose value is a JSON array. The property object is returned for further processing.

function AddArrayProperty(const AName: JSON_String): TROJSONProperty

Parameters:

  • AName: Name of the property

AddBooleanProperty

Adds a new JSON property with a boolean value. The property object is returned for further processing.

function AddBooleanProperty(const AName: JSON_String; const AValue: Boolean): TROJSONProperty

Parameters:

  • AName: Name of the property
  • AValue: Value of the property

AddNullProperty

Adds a new JSON property with a null value. The property object is returned for further processing.

function AddNullProperty(const AName: JSON_String): TROJSONProperty

Parameters:

  • AName: Name of the property

AddNumberProperty

Adds a new JSON property with a number value. The property object is returned for further processing.

function AddNumberProperty(const AName: JSON_String; const AValue: Variant): TROJSONProperty

Parameters:

  • AName: Name of the property
  • AValue: Value of the property; must contain all values that can be converted to numbers

AddObjectProperty

Adds a new JSON property whose value is a JSON object. The property object is returned for further processing.

function AddObjectProperty(const AName: JSON_String): TROJSONProperty

Parameters:

  • AName: Name of the property

AddStringProperty

Adds a new JSON property with a string value. The property object is returned for further processing.

function AddStringProperty(const AName: JSON_String; const aValue: JSON_String): TROJSONProperty

Parameters:

  • AName: Name of the property
  • aValue: Value of the property

AddVariantProperty

Adds a new JSON property whose value type is unknown. The property object is returned for further processing.

function AddVariantProperty(const AName: JSON_String; const AValue: Variant): TROJSONProperty

Parameters:

  • AName: Name of the property.
  • AValue: Value of the property; must contain all values that can be converted to strings

FindItem

Locates a property by name. Returns the property object or nil if the property cannot be found.

function FindItem(const AName: JSON_String): TROJSONProperty

Parameters:

  • AName: Name of the property

GetArrayItemByName

Locates a property by name, then checks if this property is an array. If the property is not found, this method can create it depending on the ** ACreate** parameter. If the property is not an array, an exception is raised. Returns the found or created property or nil.

function GetArrayItemByName(const AName: JSON_String; ACreate: Boolean): TROJSONArray

Parameters:

  • AName: Name of the property
  • ACreate: Specifies wether to create a new property if the property with the specified name cannot be found.

GetBooleanValueByName

Locates a property by name, then checks if this property has a boolean value. If the property is not boolean, an exception is raised. Returns the property value or false if the property with the specified name cannot be found.

function GetBooleanValueByName(const AName: JSON_String): Boolean

Parameters:

  • AName: Name of the property

GetItems  protected

The Items property read accessor function.

function GetItems(Index: Integer): TROJSONProperty

Parameters:

  • Index:

GetNumberValueByName

Locates a property by name, then checks if this property has a number value. If the property is not a number, an exception is raised. Returns the property value or null if the property with the specified name cannot be found.

function GetNumberValueByName(const AName: JSON_String): Variant

Parameters:

  • AName: Name of the property

GetObjectItemByName

Locates a property by name, then checks if this property has a JSON object value. If the property is not a JSON object, an exception is raised. Returns the property value or null if the property with the specified name cannot be found. The method can create a new object depending on the ** ACreate** parameter value.

function GetObjectItemByName(const AName: JSON_String; ACreate: Boolean): TROJSONObject

Parameters:

  • AName: Name of the property
  • ACreate: Specifies wether to create a new property if the property with the specified name cannot be found.

GetStringValueByName

Locates a property by name then checks if this property has a string value. If the property does not have a string value, an exception is raised. Returns the property value or null if the property with the specified name cannot be found.

function GetStringValueByName(const AName: JSON_String): JSON_String

Parameters:

  • AName: Name of the property

GetVariantValueByName

Returns any value by name, if the property with the specified name cannot be found, an empty string is returned.

function GetVariantValueByName(const AName: JSON_String): Variant

Parameters:

  • AName: Name of the property

IndexOf

Return an index of the property with the given name. If the property with the specified name cannot be found, -1 is returned.

function IndexOf(const AName: JSON_String): integer

Parameters:

  • AName: Name of the property

ItemByName

Similar to FindItem, but raises an exception if the property with the specified name cannot be found.

function ItemByName(const AName: JSON_String): TROJSONProperty

Parameters:

  • AName: Name of the property

LoadFromStream

Deserializes the object from the stream.

procedure LoadFromStream(AStream: TStream; AUTF8Stream: boolean)

Parameters:

  • AStream: Source stream
  • AUTF8Stream: If the data in the stream is UTF-8 encoded, this parameter should be set to true, otherwise to false.

SaveToStream

Serializes the object to the stream.

procedure SaveToStream(AStream: TStream; AUTF8Stream: boolean)

Parameters:

  • AStream: Target stream
  • AUTF8Stream: Set this parameter to true to use UTF-8 encoding to serialize the data.

SetItems  protected

The Items property write accessor function.

procedure SetItems(Index: Integer; const Value: TROJSONProperty)

Parameters:

  • Index:
  • Value: