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

Internal method responsible for serializing the JSON object to the stream.

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

Parameters:

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

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

Internal method responsible for serializing the JSON object to the stream.

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

Parameters:

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

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

Internal method responsible for serializing the JSON object to the stream.

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

Parameters:

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

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

IntLoadFromStream  protected

Internal method responsible for serializing the JSON object to the stream.

procedure IntLoadFromStream(aStream: TStream)

Parameters:

  • aStream: Target stream

IntSaveToStream  protected

Internal method responsible for serializing the JSON object to the stream.

procedure IntSaveToStream(aStream: TStream)

Parameters:

  • aStream: Target stream

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

Internal method responsible for serializing the JSON object to the stream.

procedure LoadFromStream(aStream: TStream; AUTF8Stream: Boolean)

Parameters:

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

SaveToStream

Internal method responsible for serializing the JSON 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

Internal method responsible for serializing the JSON object to the stream.

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

Parameters:

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

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

Internal method responsible for serializing the JSON object to the stream.

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

Parameters:

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

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

Internal method responsible for serializing the JSON object to the stream.

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

Parameters:

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

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

IntLoadFromStream  protected

Internal method responsible for serializing the JSON object to the stream.

procedure IntLoadFromStream(aStream: TStream)

Parameters:

  • aStream: Target stream

IntSaveToStream  protected

Internal method responsible for serializing the JSON object to the stream.

procedure IntSaveToStream(aStream: TStream)

Parameters:

  • aStream: Target stream

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

Internal method responsible for serializing the JSON object to the stream.

procedure LoadFromStream(aStream: TStream; AUTF8Stream: Boolean)

Parameters:

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

SaveToStream

Internal method responsible for serializing the JSON 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: