JSON and XML Handling   (hJson unit)

ThJsonNode_Type

Defines public JSON node kinds.
ThJsonNode_Type = (JObject, JValue, JValueBare, JArray, ...internal values omitted...);

Return Value

`JObject` object container.
`JValue` quoted scalar value.
`JValueBare` unquoted scalar token.
`JArray` array container.

ThJsonNode

Represents one node in a JSON/XML tree.
ThJsonNode = Class(TObject)

_Type

Node kind field.
_Type: ThJsonNode_Type;

Name

Node name field.
Name: String;

Value

Node scalar value field.
Value: String;

Parent

Read-only parent reference.
Property Parent: ThJsonNode Read _Parent_Node;

Return Value

Returns parent node or `NIL` for root.

Create

Creates a new node with optional parent.
Constructor ThJsonNode.Create(ParentNode: ThJsonNode = NIL);

Parameters

  • ParentNode: ThJsonNode; optional parent.

Destroy

Destroys node and owned resources.
Destructor ThJsonNode.Destroy; Override;

Clear

Clears current node and all children.
Procedure ThJsonNode.Clear;

ChildCount

Returns direct child count.
Function ThJsonNode.ChildCount: Cardinal;

Return Value

Direct child count.

Child

Returns child by index.
Function ThJsonNode.Child(CONST Index: Integer): ThJsonNode;

Parameters

  • Index: Integer; zero-based index.

Return Value

Child node or `NIL` when out of range.

LastChild

Returns last child.
Function ThJsonNode.LastChild: ThJsonNode;

Return Value

Last child node or `NIL` if empty.

Empty

Checks if node has meaningful content.
Function ThJsonNode.Empty(Value_As_Numeric_Type: Boolean = False): Boolean;

Parameters

  • Value_As_Numeric_Type: Boolean; numeric-style empty check for scalar values.

Return Value

True if empty by node-kind rules, otherwise False.

Exist

Checks path existence plus non-empty target content.
Function ThJsonNode.Exist(CONST Find_Names: ThFlds_Name_Array): Boolean;

Parameters

  • Find_Names: ThFlds_Name_Array; path segments.

Return Value

True if target exists and is not empty.

Find_Node_By_Name

Searches by field name with recursion and case options.
Function ThJsonNode.Find_Node_By_Name(CONST FldName: String; CONST Recurse: Boolean = False; CONST CaseSens: Boolean = False): ThJsonNode;

Parameters

  • FldName: String; name to find.
  • Recurse: Boolean; deep search when True.
  • CaseSens: Boolean; case-sensitive compare when True.

Return Value

First match or `NIL`.

Find_Node_ML

Finds by exact multi-level path.
Function ThJsonNode.Find_Node_ML(CONST Find_Names: ThFlds_Name_Array): ThJsonNode;

Parameters

  • Find_Names: ThFlds_Name_Array; ordered path.

Return Value

Target node or `NIL`.

Value_String

Reads scalar text from path.
Function ThJsonNode.Value_String(CONST Find_Names: ThFlds_Name_Array): String;

Parameters

  • Find_Names: ThFlds_Name_Array; target path.

Return Value

Scalar value or empty string.

Value_Integer

Reads scalar value as `Int64`.
Function ThJsonNode.Value_Integer(CONST Find_Names: ThFlds_Name_Array): Int64;

Parameters

  • Find_Names: ThFlds_Name_Array; target path.

Return Value

Converted integer or `0`.

Value_Real

Reads scalar value as `Double`.
Function ThJsonNode.Value_Real(CONST Find_Names: ThFlds_Name_Array; Filter_All_But_Digits: Boolean = False): Double;

Parameters

  • Find_Names: ThFlds_Name_Array; target path.
  • Filter_All_But_Digits: Boolean; filter non-digits before parse.

Return Value

Converted numeric value or `0`.

Value_Date

Reads scalar value as encoded date.
Function ThJsonNode.Value_Date(CONST Find_Names: ThFlds_Name_Array; DateMask: ThDateMask = he_YYYY_MM_DD): Integer;

Parameters

  • Find_Names: ThFlds_Name_Array; target path.
  • DateMask: ThDateMask; expected date format.

Return Value

Encoded date or `0`.

Value_DateTime

Reads scalar value as `TDateTime`.
Function ThJsonNode.Value_DateTime(CONST Find_Names: ThFlds_Name_Array; DateMask: ThDateMask = he_YYYY_MM_DD): TDateTime;

Parameters

  • Find_Names: ThFlds_Name_Array; target path.
  • DateMask: ThDateMask; expected date format.

Return Value

Parsed date-time or `0`.

Value_Byte

Reads scalar value as `Byte`.
Function ThJsonNode.Value_Byte(CONST Find_Names: ThFlds_Name_Array): Byte;

Parameters

  • Find_Names: ThFlds_Name_Array; target path.

Return Value

Byte value in `0..255`, otherwise `0`.

Value_Boolean

Reads scalar value as `Boolean`.
Function ThJsonNode.Value_Boolean(CONST Find_Names: ThFlds_Name_Array): Boolean;

Parameters

  • Find_Names: ThFlds_Name_Array; target path.

Return Value

True for `1` or `true`, otherwise False.

Add_Object

Adds object child node.
Function ThJsonNode.Add_Object(CONST Name: String = ''): ThJsonNode;

Parameters

  • Name: String; optional child name.

Return Value

New child node.

Add_Value

Adds scalar child node, quoted or bare.
Function ThJsonNode.Add_Value(CONST Name, Value: String; CONST Bare: Boolean = False): ThJsonNode;

Parameters

  • Name: String; child name.
  • Value: String; value text.
  • Bare: Boolean; True for unquoted token.

Return Value

New child node.

Add_String

Adds quoted string child with optional text length limit.
Function ThJsonNode.Add_String(CONST Name, Value: String; CONST LimitTextLength: Word = 0): ThJsonNode;

Parameters

  • Name: String; child name.
  • Value: String; string value.
  • LimitTextLength: Word; max chars, `0` means unlimited.

Return Value

New child node.

Add_Integer

Adds integer as bare value.
Function ThJsonNode.Add_Integer(CONST Name: String; CONST Value: Integer): ThJsonNode;

Parameters

  • Name: String; child name.
  • Value: Integer; integer value.

Return Value

New child node.

Add_Money

Adds money value as bare numeric text.
Function ThJsonNode.Add_Money(CONST Name: String; CONST Amount: Double): ThJsonNode;

Parameters

  • Name: String; child name.
  • Amount: Double; money amount.

Return Value

New child node.

Add_Real

Adds real value as bare numeric text.
Function ThJsonNode.Add_Real(CONST Name: String; CONST Amount: Double): ThJsonNode;

Parameters

  • Name: String; child name.
  • Amount: Double; real amount.

Return Value

New child node.

Add_Date

Adds date as string (`YYYY-MM-DD`).
Function ThJsonNode.Add_Date(CONST Name: String; CONST Value: TDateTime): ThJsonNode;

Parameters

  • Name: String; child name.
  • Value: TDateTime; date value.

Return Value

New child node.

Add_DateTime

Adds date-time string in regular or ISO format.
Function ThJsonNode.Add_DateTime(CONST Name: String; CONST Value: TDateTime; CONST ISO: Boolean = False): ThJsonNode;

Parameters

  • Name: String; child name.
  • Value: TDateTime; date-time value.
  • ISO: Boolean; True for ISO `...T...Z` output.

Return Value

New child node.

Add_Bool

Adds boolean as bare `true` or `false` token.
Function ThJsonNode.Add_Bool(CONST Name: String; CONST Value: Boolean): ThJsonNode;

Parameters

  • Name: String; child name.
  • Value: Boolean; boolean value.

Return Value

New child node.

Add_Array

Adds array child node.
Function ThJsonNode.Add_Array(CONST Name: String = ''): ThJsonNode;

Parameters

  • Name: String; optional array name.

Return Value

New array node.

Add_Array_Value

Adds scalar value item into current array.
Function ThJsonNode.Add_Array_Value(CONST Value: String; CONST Bare: Boolean = False): ThJsonNode;

Parameters

  • Value: String; item value.
  • Bare: Boolean; True for unquoted token.

Return Value

New item node.

Add_Field_Value

Adds one table field value to JSON node using field JSON metadata.
Function ThJsonNode.Add_Field_Value(SourceTb: ThTable; FldName: String; Row: Integer): ThJsonNode;

Parameters

  • SourceTb: ThTable; source table.
  • FldName: String; source field name.
  • Row: Integer; row index.

Return Value

New node or `NIL`.

Remove_Child

Removes direct child by index or by name.
Function ThJsonNode.Remove_Child(CONST Index: Integer): Boolean; Overload;
Function ThJsonNode.Remove_Child(CONST Name: String): Boolean; Overload;

Parameters

  • Index: Integer; direct child index.
  • Name: String; direct child name.

Return Value

True on success, otherwise False.

Add_Dev_Help_Table_Structure

Adds table schema metadata for developer help output.
Procedure ThJsonNode.Add_Dev_Help_Table_Structure(SourceTb: ThTable; ObjectName: String; Expose_Hidden_Fields: Boolean = False);

Parameters

  • SourceTb: ThTable; source table.
  • ObjectName: String; output object name.
  • Expose_Hidden_Fields: Boolean; include hidden fields when True.

Add_Table_Stream

Serializes table rows to JSON nodes with row range and format options.
Procedure ThJsonNode.Add_Table_Stream(SourceTb: ThTable; ObjectName: String;
                          SingleRecord, Multiline_As_Array: Boolean;
                          FromRow: Integer = 0; UntilRow: Integer = -1; Check_If_Empty: Boolean = True;
                          Expose_Hidden_Fields: Boolean = False);

Parameters

  • SourceTb: ThTable; source table.
  • ObjectName: String; output object name.
  • SingleRecord: Boolean; one object or array.
  • Multiline_As_Array: Boolean; multiline fields as JSON arrays.
  • FromRow: Integer; first row index.
  • UntilRow: Integer; last row index, `-1` means end.
  • Check_If_Empty: Boolean; skip empty rows.
  • Expose_Hidden_Fields: Boolean; include hidden fields when True.

Update_Table_From_Json

Updates table rows/fields from JSON nodes.
Function ThJsonNode.Update_Table_From_Json(TargetTb: ThTable; SingleRecord, SkipEmptyValues, Enforce_Lang_Input: Boolean;
                               VAR Wrong_Lang_Fields_List: String): Boolean;

Parameters

  • TargetTb: ThTable; target table.
  • SingleRecord: Boolean; expects object when True, array when False.
  • SkipEmptyValues: Boolean; keep existing values for empty input.
  • Enforce_Lang_Input: Boolean; enforce field language validation.
  • Wrong_Lang_Fields_List: String; output list of invalid language fields.

Return Value

True if update flow applied successfully, otherwise False.

Write_To_MS

Writes compact JSON to destination stream (append mode).
Procedure ThJsonNode.Write_To_MS(CONST MS: ThMemoryStream; Limit_MS_Size: Integer = 0);

Parameters

  • MS: ThMemoryStream; destination stream.
  • Limit_MS_Size: Integer; max destination size, `0` means unlimited.

Debug_Write_To_Log

Writes current JSON output to log.
Procedure ThJsonNode.Debug_Write_To_Log;

Write_Pretty_Html_To_MS

Writes pretty-formatted JSON to destination stream (append mode).
Procedure ThJsonNode.Write_Pretty_Html_To_MS(CONST MS: ThMemoryStream; Limit_MS_Size: Integer = 0);

Parameters

  • MS: ThMemoryStream; destination stream.
  • Limit_MS_Size: Integer; max destination size, `0` means unlimited.

Filter_Json_Object_Name

Keeps only `A..Z`, `a..z`, `0..9`, `-`, `_`.
Function Filter_Json_Object_Name(CONST Name: String): RawByteString;

Parameters

  • Name: String; source text.

Return Value

Filtered name as `RawByteString`.

Convert_Wide_To_Json

Escapes Unicode text for JSON output.
Function Convert_Wide_To_Json(Value: String): RawByteString;

Parameters

  • Value: String; source text.

Return Value

Escaped JSON-safe text as `RawByteString`.

Calc_Json_Field_Name

Builds normalized JSON field name from free text.
Function Calc_Json_Field_Name(CONST Name: String): RawByteString;

Parameters

  • Name: String; source text.

Return Value

Normalized field name as `RawByteString`.

Clean_JSON_Stream_From_Inverted_Commas

Repairs wrapped escaped JSON stream in-place when format matches expected pattern.
Function Clean_JSON_Stream_From_Inverted_Commas(MS: ThMemoryStream): Boolean;

Parameters

  • MS: ThMemoryStream; stream to inspect and fix.

Return Value

True if wrapped format detected and fixed, otherwise False.

Load_JSON_From_Stream

Loads and parses JSON from current stream position.
Function Load_JSON_From_Stream(MS: ThMemoryStream; ReadSize: Int64 = -1): ThJsonNode;

Parameters

  • MS: ThMemoryStream; source stream.
  • ReadSize: Int64; compatibility parameter.

Return Value

Root node or `NIL` on failure.

Load_JSON_From_File

Loads and parses JSON file.
Function Load_JSON_From_File(CONST Filename: String): ThJsonNode;

Parameters

  • Filename: String; source file path.

Return Value

Root node or `NIL` on failure.

Save_JSON_To_File

Serializes tree and saves to file.
Function Save_JSON_To_File(Root_Node: ThJsonNode; CONST Filename: String; Pretty: Boolean): Boolean;

Parameters

  • Root_Node: ThJsonNode; root node.
  • Filename: String; destination path.
  • Pretty: Boolean; pretty or compact output.

Return Value

True on success, otherwise False.

Load_JSON_From_WideString

Parses JSON from Unicode string.
Function Load_JSON_From_WideString(CONST Source: String): ThJsonNode;

Parameters

  • Source: String; source JSON.

Return Value

Root node or `NIL` on failure.

Save_JSON_To_UTF8_RawString

Serializes tree to UTF-8 `RawByteString`.
Function Save_JSON_To_UTF8_RawString(Root_Node: ThJsonNode; Pretty: Boolean): RawByteString;

Parameters

  • Root_Node: ThJsonNode; root node.
  • Pretty: Boolean; pretty or compact output.

Return Value

Serialized JSON text, empty string on failure.

Load_XML_From_Stream

Loads and parses XML stream into `ThJsonNode` tree.
Function Load_XML_From_Stream(MS: ThMemoryStream; CodePage: Cardinal = CP_UTF8): ThJsonNode;

Parameters

  • MS: ThMemoryStream; source stream.
  • CodePage: Cardinal; fallback decode codepage.

Return Value

Root node or `NIL` on failure.

Load_XML_From_File

Loads and parses XML file into `ThJsonNode` tree.
Function Load_XML_From_File(CONST Filename: String): ThJsonNode;

Parameters

  • Filename: String; source file path.

Return Value

Root node or `NIL` on failure.