System.JSON.Builders.TJSONIterator

From RAD Studio API Documentation
Jump to: navigation, search

System.TObjectTJSONIterator

Delphi

TJSONIterator = class

C++

class PASCALIMPLEMENTATION TJSONIterator : public System::TObject

Properties

Type Visibility Source Unit Parent
class public
System.JSON.Builders.pas
System.JSON.Builders.hpp
System.JSON.Builders System.JSON.Builders

Description

Allows you to iterate through the content that a JSON reader provides.

Iterators are similar to JSON reader, but they provide some additional features, and behave slightly differently. For example:

Iterating Through the Input Data

To iterate through the contents of the source JSON reader:

  1. Create a JSON iterator that reads data from a JSON reader.
  2. Call Next so that the reader moves to the first JSON token of the input data.
    • Type indicates the type of the token.
    • If you are iterating an array, use Index to obtain the index of the current token. If you are iterating an object, use Key to obtain the key of the current token.
    • Use one of the value properties of the iterator to access the value of the current token. The type of a token determines the type of its value, which determines the value property that you must use to access the value of the token. For example, if Type is Boolean, you can call AsBoolean to access the boolean value of the current token.
  3. Keep calling Next to go through all the JSON tokens of the input data. Next returns False when it reaches the end of the input data.

You can use Rewind to move the iterator back to the beginning of the input data.

Value Properties

TJSONIterator provides several properties that you can read to obtain the value of the current token of the iterator. The value of the Type property determines which properties you can use to read the value.

The following table shows some values of the Type property and properties of TJSONIterator that you can use to read the value of the current token:

Type Value TJSONIterator Properties

Boolean

AsBoolean

Bytes

AsBytes

CodeWScope

AsCodeWScope

Date

AsDateTime

DBRef

AsDBRef

Float

AsDouble
AsExtended

Integer

AsInteger
AsInt64

MaxKey

IsMaxKey

MinKey

IsMinKey

Null

IsNull

Oid

AsOid

RegEx

AsRegEx

String

AsString
AsGUID (if you know that the string contains a GUID)

Undefined

IsUndefined

To access the value of the current token in a different format, you may use AsValue or AsVariant.

Using Recursion

TJSONIterator is a shallow iterator by default. Every time that the iterator reaches a token of type TJsonToken.StartObject or TJsonToken.StartObject, you must call Recurse before your next call to Next to ensure that the iterator does not skip the whole array or object.

During recursion, you can use Return to skip the remaining content of the current array or object, and returns the iterator back to the parent of the array or object.

When you start iterating the source JSON reader, Depth is 0. Each time that you use recursion to go into an array or an object, the depth increases by 1. When you leave an array or object, either because you have reached its end or because you skipped to its end using Return, the depth decreases by 1. InRecurse indicates whether or not the iterator is in a depth higher than 0.

ParentType indicates the type of the parent of the current token, either TJsonToken.StartObject or TJsonToken.StartObject. If you are not in any depth, ParentType is TJsonToken.None.

Checking if Specific Data Exists

Use Find to determine if the content of the source JSON reader contains data matching a given JSON path.

See Also