Contents · Forum Home · StreamSec Tools 4.1.5.363 documentation

stJson

Classes, interfaces and records

classeJsonError

inherits from Exception

eJsonError is the exception this unit raises for malformed JSON handed to Parse/ParseUTF8, and for kind mismatches or out-of-range access on a tJsonValue (for example AsInt on a string, or Items past Count); a scalar accessor may still surface an RTL EConvertError when a well-typed number overflows its target, as in AsInt on a value beyond Int64.

(no public members)

classtJsonValue

constructor Create(aKind: tJsonKind);
Create builds an empty value of the given kind; applications normally use the New* factories or Parse instead of calling this directly.
destructor Destroy; override;
Destroy frees this value and, by containment, every value nested under it, so freeing a parsed or built root disposes the whole tree.
class function Parse(const aText: string): tJsonValue;
Parse decodes a complete JSON document from a Delphi UTF-16 string and returns the root value, raising eJsonError on malformed input or trailing content; the caller owns and must free the returned tree.
class function ParseUTF8(const aBytes: tBytes): tJsonValue;
ParseUTF8 decodes a raw UTF-8 byte payload, as received on the wire, rejecting an empty input; use it on an HTTP response body such as an ACME directory or order resource.
class function NewNull: tJsonValue;
The New* factories each create a single detached value of one kind; pass the result to Append or AddValue to graft it into an array or object, or to ToJson to serialise it on its own.
class function NewBool(aValue: Boolean): tJsonValue;
a boolean carrying aValue
class function NewInt(aValue: Int64): tJsonValue;
a number whose literal is the decimal of aValue
class function NewString(const aValue: string): tJsonValue;
a string holding aValue verbatim
class function NewArray: tJsonValue;
an empty array, ready for Append
class function NewObject: tJsonValue;
an empty object, ready for AddValue
function ToJson(aSorted: Boolean = False; aIndent: Integer = 0): string;
ToJson writes the value with no insignificant whitespace. With aSorted, every object's members are emitted in ascending order of name, compared by code unit - the canonical form RFC 7638 hashes. Full description »
function ToUTF8(aSorted: Boolean = False; aIndent: Integer = 0): tBytes;
ToUTF8 is ToJson encoded as UTF-8 bytes, the form to transmit; pass aSorted=True for the canonical RFC 7638 form used in a JWK thumbprint.
function IndexOfName(const aName: string): Integer;
Object access. Member returns nil when the name is absent, so a missing field is distinguishable from one holding null.
function Has(const aName: string): Boolean;
True when aName is present, whatever value it holds
function Member(const aName: string): tJsonValue; function StrValue(const aName: string; const aDefault: string = ''): string;
StrValue/IntValue/BoolValue read an optional member without a prior kind check: each returns the member's scalar when present and of the matching kind, and aDefault otherwise (missing, null, or wrong kind).
function IntValue(const aName: string; aDefault: Int64 = 0): Int64; function BoolValue(const aName: string; aDefault: Boolean = False): Boolean; function AddValue(const aName: string; aValue: tJsonValue): tJsonValue;
The Add* methods append a named member to an object (raising unless this value is an object) and return the added value, so calls chain to build a nested request body in one expression. AddValue takes ownership of aValue.
function AddStr(const aName, aValue: string): tJsonValue;
adds a string member
function AddInt(const aName: string; aValue: Int64): tJsonValue;
adds a number member
function AddBool(const aName: string; aValue: Boolean): tJsonValue;
adds a boolean member
function AddNull(const aName: string): tJsonValue;
adds a null member
function AddObject(const aName: string): tJsonValue;
adds and returns a new empty object member
function AddArray(const aName: string): tJsonValue;
adds and returns a new empty array member
function SetValue(const aName: string; aValue: tJsonValue): tJsonValue;
The Add* family always appends, which is what building a document wants and what AMENDING one must not do - adding a name that is already present would leave the object carrying it twice. Full description »
function SetStr(const aName, aValue: string): tJsonValue;
Delegates to SetValue, passing 2 argument(s), and returns its result.
function Remove(const aName: string): Boolean; function RemoveAt(aIndex: Integer): Boolean;
Removes one element of an array, or one member of an object, by position. False when aIndex is out of range or this is a scalar.
function Append(aValue: tJsonValue): tJsonValue;
Array access.
function AppendStr(const aValue: string): tJsonValue;
appends and returns a new string element
function AppendObject: tJsonValue;
appends and returns a new empty object element
function AsString: string;
Scalar access; each raises unless the kind matches.
function AsInt: Int64;
the number as Int64; raises unless a number
function AsBool: Boolean;
the boolean; raises unless this is a boolean
function AsFloat: Double;
the number as Double, parsed locale-independently; raises unless a number
function IsNull: Boolean;
True when this value is JSON null; never raises
property Kind: tJsonKind read fKind;
which of the six JSON kinds this value holds
property Count: Integer read GetCount;
element or member count; 0 for scalar kinds
property Items[aIndex: Integer]: tJsonValue read GetItem; default;
array element or object member value by 0-based index
property Names[aIndex: Integer]: string read GetName;
object member name at the given index, parallel to Items

Types (enumerations, sets, aliases)

enumtJsonKind

tJsonKind enumerates the six JSON value kinds; a value's kind is fixed when it is created and is reported by the Kind property.

  tJsonKind = (jkNull, jkBool, jkNumber, jkString, jkArray, jkObject);

Routines