| 1 |
This package implements JSON support for FPC. |
| 2 |
|
| 3 |
You might want to have a look at the lazarus jsonviewer tool, written using |
| 4 |
fpJSON (see lazarus/tools/jsonviewer). It visualizes the fpJSON data and |
| 5 |
shows how to program using fpjson. |
| 6 |
|
| 7 |
JSON support consists of 3 parts: |
| 8 |
|
| 9 |
unit fpJSON contains the data representation. Basically, it defines a set of |
| 10 |
classes: |
| 11 |
|
| 12 |
TJSONData |
| 13 |
+- TJSONNumber |
| 14 |
+- TJSONIntegerNumber |
| 15 |
+- TJSONFloatNumber |
| 16 |
+- TJSONInt64Number |
| 17 |
+- TJSONString |
| 18 |
+- TJSONBoolean |
| 19 |
+- TJSONNull |
| 20 |
+- TJSONObject |
| 21 |
+- TJSONArray |
| 22 |
|
| 23 |
The TJSONData.JSONType property is an enumerated: |
| 24 |
TJSONtype = (jtUnknown, jtNumber, jtString, jtBoolean, jtNull, jtArray, jtObject); |
| 25 |
|
| 26 |
Which allows to determine the type of a value. |
| 27 |
|
| 28 |
The following methods exist: |
| 29 |
|
| 30 |
Procedure Clear; |
| 31 |
Clears the value. For arrays and objects, removes all elements/members |
| 32 |
Function Clone : TJSONData; |
| 33 |
Creates an exact replica of the valye |
| 34 |
property Count: Integer; |
| 35 |
For simple values this is zero, for complex values this is the number of |
| 36 |
elements/members. Read only. |
| 37 |
property Items[Index: Integer]: TJSONData |
| 38 |
For simple values, reading this will result in an error. For complex |
| 39 |
values, this gives access to the members. |
| 40 |
property Value: variant; |
| 41 |
The value as a variant. Only for simple values. |
| 42 |
Property AsString : TJSONStringType: |
| 43 |
The value as a string. Only for simple values. |
| 44 |
Property AsFloat : TJSONFloat; |
| 45 |
The value as a float (double). only for simple values. |
| 46 |
Property AsInteger : Integer ; |
| 47 |
The value as an integer. only for simple values. |
| 48 |
Property AsInt64 : Int64; |
| 49 |
The value as an 64-bit integer. only for simple values. |
| 50 |
Property AsBoolean : Boolean ; |
| 51 |
The value as a boolean. |
| 52 |
Property IsNull : Boolean ; |
| 53 |
Is the value Null ? |
| 54 |
Property AsJSON : TJSONStringType |
| 55 |
Return the value in JSON notation. For simple and complex values. |
| 56 |
|
| 57 |
The TJSONArray type provides access to the elements in the array in the |
| 58 |
following ways: |
| 59 |
|
| 60 |
Property Types[Index : Integer] : TJSONType; |
| 61 |
Indexed access to the types of the elements in the array. |
| 62 |
Property Nulls[Index : Integer] : Boolean |
| 63 |
Checks if the Index-the element is NULL. |
| 64 |
Property Integers[Index : Integer] : Integer |
| 65 |
Read/Write element values as integers. |
| 66 |
Property Int64s[Index : Integer] : Int64 |
| 67 |
Read/Write element values as 64-bit integers. |
| 68 |
Property Strings[Index : Integer] : TJSONStringType; |
| 69 |
Read/Write element values as strings. |
| 70 |
Property Floats[Index : Integer] : TJSONFloat ; |
| 71 |
Read/Write element values as floats (doubles). |
| 72 |
Property Booleans[Index : Integer] : Boolean; |
| 73 |
Read/Write element values as booleans. |
| 74 |
Property Arrays[Index : Integer] : TJSONArray; |
| 75 |
Read/Write element values as arrays. |
| 76 |
Property Objects[Index : Integer] : TJSONObject; |
| 77 |
Read/Write element values a strings |
| 78 |
|
| 79 |
Reading an element as a type which is incompatible, will result in an |
| 80 |
exception. For instance if element 5 is an object value, then the following |
| 81 |
will result in an exception: |
| 82 |
i:=i+Array.Integers[5] |
| 83 |
|
| 84 |
The TJSONObject type similarly provides access to the elements in the array |
| 85 |
using the member names: |
| 86 |
property Names[Index : Integer] : TJSONStringType; |
| 87 |
Indexed access to the member names. |
| 88 |
property Elements[AName: string] : TJSONData; |
| 89 |
Read/Write a member as a raw TJSONData value. |
| 90 |
Property Types[AName : String] : TJSONType Read GetTypes; |
| 91 |
Read/Write the type of a member. |
| 92 |
Property Nulls[AName : String] : Boolean; |
| 93 |
Read/Write a member as a NULL value. |
| 94 |
Property Floats[AName : String] : TJSONFloat; |
| 95 |
Read/Write a member as a float value (double) |
| 96 |
Property Integers[AName : String] : Integer; |
| 97 |
Read/Write a member as an integer value |
| 98 |
Property Int64s[AName : String] : Int64; |
| 99 |
Read/Write a member as an 64-bit integer value |
| 100 |
Property Strings[AName : String] : TJSONStringType; |
| 101 |
Read/Write a member as a string value. |
| 102 |
Property Booleans[AName : String] : Boolean; |
| 103 |
Read/Write a member as a boolean value. |
| 104 |
Property Arrays[AName : String] : TJSONArray; |
| 105 |
Read/Write a member as an array value. |
| 106 |
Property Objects[AName : String] : TJSONObject |
| 107 |
Read/Write a member as an object value. |
| 108 |
|
| 109 |
Members can be added with the Add() call, which exists in various overloaded |
| 110 |
forms: |
| 111 |
function Add(const AName: TJSONStringType; Const AValue): Integer; |
| 112 |
Where the type of AVAlue is one of the supported types: |
| 113 |
integer, int64, double, string, TJSONArray or TJSONObject. |
| 114 |
|
| 115 |
The Delete() call deletes an element from an array or object. The element is |
| 116 |
freed. |
| 117 |
|
| 118 |
Important remark: |
| 119 |
The array and object classes own their members: the members are destroyed as |
| 120 |
they are deleted. For this, the Extract() call exists: it removes an |
| 121 |
element/member from the array/object, without destroying it. |
| 122 |
|
| 123 |
Converting from string/stream to JSONData |
| 124 |
========================================= |
| 125 |
|
| 126 |
The fpjson unit contains a GetJSON() function which accepts a string or a |
| 127 |
stream as a parameter. The function will parse the JSON in the stream and |
| 128 |
the return value is a TJSONData value corresponding to the JSON. |
| 129 |
The function works with a callback, which is set by the JSONParser unit. |
| 130 |
The JSONParser unit simply needs to be included in the project. |
| 131 |
|
| 132 |
The parsing happens with default settings for the parser class. |
| 133 |
You can override this behaviour by creating your own callback, |
| 134 |
and creating the parser with different settings. |
| 135 |
|
| 136 |
Enumerator support |
| 137 |
================== |
| 138 |
|
| 139 |
the TJSONData class offers support for an enumerator, hence the |
| 140 |
For e in JSON do |
| 141 |
construct can be used. The enumerator is a TJSONEnum value, which has 3 |
| 142 |
members: |
| 143 |
Key : The key of the element |
| 144 |
(name in TJSONObject, Index in TJSONArray, empty otherwise) |
| 145 |
KeyNum: The index of the element. |
| 146 |
(Index in TJSONArray/TJSONObject, 0 otherwise) |
| 147 |
Value : The value of the element |
| 148 |
(These are the member values for TJSONArray/TJSONObject, and is the |
| 149 |
element itself otherwise) |
| 150 |
|
| 151 |
While the enumerator is looping, it is not allowed to change the content of |
| 152 |
the array or object, and the value may not be freed. |
| 153 |
|
| 154 |
Scanner/Parser |
| 155 |
============== |
| 156 |
|
| 157 |
The JSONSCanner unit contains a scanner for JSON data: TJSONScanner. |
| 158 |
Currently it does not support full unicode, only UTF-8 is supported. |
| 159 |
|
| 160 |
The JSONParser unit contains the parser for JSON data: TJSONParser. |
| 161 |
It uses to scanner to read the tokens. The scanner is created automatically. |
| 162 |
|
| 163 |
|
| 164 |
The Parse method will parse the data that was passed to the parser and will |
| 165 |
return the JSON value. |
| 166 |
|
| 167 |
Sample use: |
| 168 |
|
| 169 |
Var |
| 170 |
P : TJSONParser; |
| 171 |
S : String; |
| 172 |
D : TJSONObject; |
| 173 |
|
| 174 |
begin |
| 175 |
P:=TJSONParser.Create('{ "top": 10, "left": 20}'); |
| 176 |
try |
| 177 |
D:=P.Parse as TJSONObject; |
| 178 |
Writeln('Top : ',D.Integers['top']); |
| 179 |
Writeln('Left : ',D.Integers['left']); |
| 180 |
D.free; |
| 181 |
Finally |
| 182 |
P.free; |
| 183 |
end; |
| 184 |
end; |
| 185 |
|
| 186 |
Note that the member names are case sensitive. |
| 187 |
|
| 188 |
As an alternative, a stream may be passed to the constructor of TJSONParser. |
| 189 |
|
| 190 |
The scanner and parser support the 'Strict' property. |
| 191 |
Strict JSON syntax requires the member names of an object to be strings: |
| 192 |
{ "top": 10, "left": 20} |
| 193 |
However, due to the sloppy definition of Javascript (and hence JSON), |
| 194 |
the following type of JSON notation is frequently encountered: |
| 195 |
{ top: 10, left: 20} |
| 196 |
By default, this sloppy notation is accepted. Setting 'Strict' to true will |
| 197 |
reject this. |
| 198 |
|
| 199 |
A second effect of the Strict property is the requirement of " as a string |
| 200 |
delimiter. A single quote is also often found in Javascript and JSON: |
| 201 |
{ title: 'A nice title' } |
| 202 |
By default, this is accepted. Setting 'Strict' to true will reject this. |
| 203 |
|
| 204 |
Customizing the classes : Factory support |
| 205 |
========================================= |
| 206 |
|
| 207 |
The various classes created by the methods can be customized. |
| 208 |
This can be useful to create customized descendents, for example to attach |
| 209 |
extra data to the various values. All instances of TJSONData are created |
| 210 |
through the CreateJSON() functions, which use a set of customizable classes |
| 211 |
to create the JSONData structures. |
| 212 |
|
| 213 |
All functions which somehow create a new instance (clone, add, insert, parsing) |
| 214 |
use the CreateJSON functions. |
| 215 |
|
| 216 |
Which classes need to be created for a specific value is enumerated in |
| 217 |
|
| 218 |
TJSONInstanceType = (jitUnknown, jitNumberInteger,jitNumberInt64,jitNumberFloat, |
| 219 |
jitString, jitBoolean, jitNull, jitArray, jitObject); |
| 220 |
|
| 221 |
when a Int64 value must be instantiated, the class identified with |
| 222 |
jitNumberInt64 is instantiated. |
| 223 |
|
| 224 |
To customize the classes, the new class can be set using SetJSONInstanceType: |
| 225 |
|
| 226 |
Procedure SetJSONInstanceType(AType : TJSONInstanceType; AClass : TJSONDataClass); |
| 227 |
Function GetJSONInstanceType(AType : TJSONInstanceType) : TJSONDataClass; |
| 228 |
|
| 229 |
The function checks whether sane classes are specified.; |