Download Now

    Reference TJsonNode

    Declared in Codebot.Text.Json.pas on line 79

    TJsonNode is the class used to parse, build, and navigate a json document. You should only create and free the root node of your document. The root node will manage the lifetime of all children through methods such as Add, Delete, and Clear.
    When you create a TJsonNode node it will have no parent and is considered to be the root node. The root node must be either an array or an object. Attempts to convert a root to anything other than array or object will raise an exception.
    Note: The parser supports unicode by converting unicode characters escaped as values such as \u20AC. If your json string has an escaped unicode character it will be unescaped when converted to a pascal string.
    JsonStringDecode to convert a JSON string to a normal string JsonStringEncode to convert a normal string to a JSON string
      TJsonNode = class
      public
        { A parent node owns all children. Only destroy a node if it has no parent.
          To destroy a child node use Delete or Clear methods instead. }
        destructor Destroy; override;
        { GetEnumerator adds 'for ... in' statement support }
        function GetEnumerator: TJsonNodeEnumerator;
        { Load json from a stream }
        procedure LoadFromStream(Stream: TStream);
        { Save json to a stream }
        procedure SaveToStream(Stream: TStream);
        { Load json from a file }
        procedure LoadFromFile(const FileName: string);
        { Save json to a file }
        procedure SaveToFile(const FileName: string);
        { Convert a json string into a value or a collection of nodes. If the
          current node is root then the json must be an array or object. }
        procedure Parse(const Json: string);
        { The same as Parse, but returns true if no exception is caught }
        function TryParse(const Json: string): Boolean;
        { Add a child node by node kind. If the current node is an array then the
          name parameter will be discarded. If the current node is not an array or
          object the Add methods will convert the node to an object and discard its
          current value. Note: If the current node is an object then adding an
          existing name will overwrite the matching child node instead of adding. }
        function Add(const Name: string; K: TJsonNodeKind = nkObject): TJsonNode; overload;
        function Add(const Name: string; B: Boolean): TJsonNode; overload;
        function Add(const Name: string; const N: Double): TJsonNode; overload;
        function Add(const Name: string; const S: string): TJsonNode; overload;
        { Convert to an array and add an item }
        function Add: TJsonNode; overload;
        { Delete a child node by index or name }
        procedure Delete(Index: Integer); overload;
        procedure Delete(const Name: string); overload;
        { Remove all child nodes }
        procedure Clear;
        { Get a child node by index. EJsonException is raised if node is not an
          array or object or if the index is out of bounds. See also: Count }
        function Child(Index: Integer): TJsonNode; overload;
        { Get a child node by name. If no node is found nil will be returned. }
        function Child(const Name: string): TJsonNode; overload;
        { Search for a node using a path string and return true if exists }
        function Exists(const Path: string): Boolean;
        { Search for a node using a path string }
        function Find(const Path: string): TJsonNode; overload;
        { Search for a node using a path string and return true if exists }
        function Find(const Path: string; out Node: TJsonNode): Boolean; overload;
        { Force a series of nodes to exist and return the end node }
        function Force(const Path: string): TJsonNode;
        { Format the node and all its children as json }
        function ToString: string; override;
        { Root node is read only. A node is the root when it has no parent. }
        property Root: TJsonNode read GetRoot;
        { Parent node is read only }
        property Parent: TJsonNode read FParent;
        { Kind can also be changed using the As methods. Note: Changes to Kind cause
          Value to be reset to a default value. }
        property Kind: TJsonNodeKind read FKind write SetKind;
        { Name is unique within the scope }
        property Name: string read GetName write SetName;
        { Value of the node in json e.g. '[]', '"hello\nworld!"', 'true', or
          '1.23e2' }
        property Value: string read GetValue write Parse;
        { The number of child nodes. If node is not an object or array this property
          will return 0. }
        property Count: Integer read GetCount;
        { AsJson is the more efficient version of Value. Text returned from AsJson
          is the most compact representation of the node in json form. Note: If you
          are writing a service to transmit or receive json data then use AsJson. If
          you want friendly human readable text use Value. }
        property AsJson: string read GetAsJson write Parse;
        { Convert the node to an array }
        property AsArray: TJsonNode read GetAsArray;
        { Convert the node to an object }
        property AsObject: TJsonNode read GetAsObject;
        { Convert the node to null }
        property AsNull: TJsonNode read GetAsNull;
        { Convert the node to a bool }
        property AsBoolean: Boolean read GetAsBoolean write SetAsBoolean;
        { Convert the node to a string }
        property AsString: string read GetAsString write SetAsString;
        { Convert the node to a number }
        property AsNumber: Double read GetAsNumber write SetAsNumber;
      end;

    See also