System.JSON.TJSONPathParser

From RAD Studio API Documentation
Jump to: navigation, search

Delphi

  TJSONPathParser = record
  public type
    TToken = (Undefined, Name, ArrayIndex, Eof, Error);
  private
    FPathPtr: PChar;
    FPtr: PChar;
    FEndPtr: PChar;
    FTokenArrayIndex: Integer;
    FTokenName: string;
    FToken: TToken;
    function GetIsEof: Boolean; inline;
    procedure RaiseError(AMsg: PResStringRec);
    procedure RaiseErrorFmt(AMsg: PResStringRec; const AParams: array of const);
    procedure ParseName;
    procedure ParseQuotedName(AQuote: Char);
    procedure ParseArrayIndex;
    procedure ParseIndexer;
    function EnsureLength(ALength: Integer): Boolean; inline;
    procedure FrontTrim(var APtr: PChar); inline;
    procedure BackTrim(var APtr: PChar); inline;
  public
    constructor Create(const APath: string); overload;
    constructor Create(const APathPtr: PChar; ALen: Integer); overload;
    function NextToken: TToken;
    property IsEof: Boolean read GetIsEof;
    property Token: TToken read FToken;
    property TokenName: string read FTokenName;
    property TokenArrayIndex: Integer read FTokenArrayIndex;
  end;

C++

struct DECLSPEC_DRECORD TJSONPathParser
{
public:
    enum class DECLSPEC_DENUM TToken : unsigned char { Undefined, Name, ArrayIndex, Eof, Error };
private:
    System::WideChar *FPathPtr;
    System::WideChar *FPtr;
    System::WideChar *FEndPtr;
    int FTokenArrayIndex;
    System::UnicodeString FTokenName;
    TToken FToken;
    bool __fastcall GetIsEof();
    void __fastcall RaiseError(System::PResStringRec AMsg);
    void __fastcall RaiseErrorFmt(System::PResStringRec AMsg, const System::TVarRec *AParams, const int AParams_High);
    void __fastcall ParseName();
    void __fastcall ParseQuotedName(System::WideChar AQuote);
    void __fastcall ParseArrayIndex();
    void __fastcall ParseIndexer();
    bool __fastcall EnsureLength(int ALength);
    void __fastcall FrontTrim(System::WideChar * &APtr);
    void __fastcall BackTrim(System::WideChar * &APtr);
public:
    __fastcall TJSONPathParser(const System::UnicodeString APath)/* overload */;
    __fastcall TJSONPathParser(const System::WideChar * APathPtr, int ALen)/* overload */;
    TToken __fastcall NextToken();
    __property bool IsEof = {read=GetIsEof};
    __property TToken Token = {read=FToken};
    __property System::UnicodeString TokenName = {read=FTokenName};
    __property int TokenArrayIndex = {read=FTokenArrayIndex};
    TJSONPathParser() {}
};

Properties

Type Visibility Source Unit Parent
record
struct
public
System.JSON.pas
System.JSON.hpp
System.JSON System.JSON

Description

Parser of a JSON path.

A JSON path is a string that defines a path to a specific item within JSON data. A JSON path is to JSON what XPath is to XML.

Supported JSON Path Syntax

TJSONPathParser implements a subset of the JSON path specification defined by Stefan Göessner. Specifically, supported elements are:

  • Child operators for objects:
    • Use . to access object properties that do not contain a dot in their name. For example, use root.child to access the child property of the root object.
    • Use [] to access object properties that do contain a quoting character in their name. For example, use root['child.name'] or root["child.name"] to access the child.name property of the root object.
  • Subscript operator ([]) for arrays. For example, use root[0] to access the first item of the root array.

These operators do not support special expressions, they only support actual values (object properties or array indexes).

Using TJSONPathParser

When you create an instance of TJSONPathParser, you must pass the target JSON path to the constructor.

Once you have an instance of TJSONPathParser, call NextToken to iterate through the tokens of the specified JSON path until IsEof is True or NextToken returns an Error token.

NextToken may return any of the following tokens:

Token Description

ArrayIndex

The next token in the JSON path is the index of an item of a JSON array. TokenArrayIndex contains the target index.

Name

The next token in the JSON path is the key of a key-value pair of a JSON object. TokenName contains the target key.

Eof

You have reached the end of the JSON path.

You cannot continue iterating through the path, if you call NextToken again on this instance of TJSONPathParser an exception raises.

Error

There is an error in the specified JSON path.

You cannot continue iterating through the path, if you call NextToken again on this instance of TJSONPathParser an exception raises.

Token contains the token that the last call to NextToken returned.

For a code example that shows how to use TJSONPathParser, see the implementation of TJSONIterator.Find.

See Also