Sign in Join
FPDoc Onlineexperiment

fpJson unit

JSON support for Free Pascal.

The JSON unit implements JSON support for Free Pascal. It contains the data structures (TJSONData and descendent objects) to treat JSON data and output JSON as a string AsJSON. The generated JSON can be formatted in several ways FormatJSON.

Using the JSON data structures is simple. Instantiate an appropriate descendent of TJSONData, set the data and call AsJSON. The following JSON data types are supported:

Numbers
in one of TJSONIntegerNumber, TJSONFloatNumber or TJSONInt64Number, depending on the type of the number.
Strings
in TJSONString.
Boolean
in TJSONBoolean.
null
is supported using TJSONNull
Array
is supported using TJSONArray
Object
is supported using TJSONObject

The constructors of these objects allow to set the value, making them very easy to use. The memory management is automatic in the sense that arrays and objects own their values, and when the array or object is freed, all data in it is freed as well.

Typical use would be:

Var
  O : TJSONObject;

begin
  O:=TJSONObject.Create(['Age',44,
                         'Firstname','Michael',
                         'Lastname','Van Canneyt']);
  Writeln(O.AsJSON);
  Write('Welcome ',O.Strings['Firstname'],', ');
  Writeln(O.Get('Lastname','')); // empty default.
  Writeln(', your current age is ',O.Integers('Age']);
  O.Free;
end;

The TJSONArray and TJSONObject classes offer methods to examine, get and set the various members and search through the various members.

Currently the JSON support only allows the use of UTF-8 data.

Parsing incoming JSON and constructing the JSON data structures is not implemented in the fpJSON unit. For this, the jsonscanner unit must be included in the program unit clause. This sets several callback hooks (using SetJSONParserHandler and then the GetJSON function can then be used to transform a string or stream to JSON data structures:

uses fpjson, jsonparser;

Var
  D,E : TJSONData;

begin
  D:=GetJSON('{ "Children" : ['+
             '  { "Age" : 23, '+
             '    "Names" : { "LastName" : "Rodriquez",'+
             '                "FirstName" : "Roberto" }},'+
             '  { "Age" : 20,'+
             '    "Names" : { "LastName" : "Rodriquez",'+
             '                "FirstName" : "Maria" }}'+
             '  ]}');
  E:=D.FindPath('Children[1].Names.FirstName');
  Writeln(E.AsJSON);
  D.Free;
end.

will print "Maria".

The FPJSON code does not use hardcoded class names when creating the JSON: it uses the various CreateJSON functions to create the data. These functions use a registry of classes, so it is possible to create descendents of the classes in the fpjson unit and have these used for construction of JSON Data structures. The GetJSONInstanceType and SetJSONInstanceType functions can be used to get or set the classes that must be used. the default parser used by GetJSON will also use these functions.

Classes and records

EJSONclassJSON exception.
TBaseJSONEnumeratorclassJSON enumerator.
TJSONArrayclassClass of TJSONArray.
TJSONBooleanclassClass to represent boolean JSON data.
TJSONDataclassBase (abstract) object for all JSON based data types.
TJSONEnumrecordJSON enumerator loop variable type.
TJSONFloatNumberclassClass to represent floating-point JSON data.
TJSONInt64NumberclassClass to represent 64-bit integer JSON data.
TJSONIntegerNumberclassClass to represent 32-bit integer JSON data.
TJSONNullclassClass to represent boolean JSON data.
TJSONNumberclassCommon ancestor for the numerical value JSON classes.
TJSONObjectclassClass of TJSONObject.
TJSONQWordNumberclassClass to represent 64-bit unsigned integer JSON data.
TJSONStringclassClass to represent string JSON data.

Procedures and functions

CreateJSONfunctionCreate a JSON data item.
CreateJSONArrayfunctionCreate a JSON array.
CreateJSONObjectfunctionCreate a JSON object.
GetJSONfunctionConvert JSON string to JSON data structure.
GetJSONInstanceTypefunctionJSON factory: Get the JSONData class types to use.
GetJSONParserHandlerfunctionGet the current JSON parser handler.
GetJSONStringParserHandlerfunctionreturn the current JSON string to JSON Data conversion callback.
JSONStringToStringfunctionConvert a JSON-escaped string to a string.
JSONTypeNamefunctionConvert a JSON type to a string.
SetJSONInstanceTypefunctionJSON factory: Set the JSONData class types to use.
SetJSONParserHandlerfunctionSet the JSON parser handler.
SetJSONStringParserHandlerfunctionInstall a JSON string to JSON Data conversion callback.
StringToJSONStringfunctionConvert a string to a JSON-escaped string.

Types

PJSONCharTypetypePointer to TJSONCharType, used in parsing.
TFormatOptionenumerationFormatting options for FormatJSON.
TFormatOptionssetSet of TFormatOption options.
TFPJSStreamtypeStream alias type.
TJSONArrayClasstypeClass of TJSONArray.
TJSONArrayIteratorprocedure typeIterator callback for TJSONArray.Iterate.
TJSONBooleanClasstypeClass of TJSONBoolean.
TJSONCharTypetypeCharacter type used in parsing.
TJSONDataClasstypeClass of TJSONData.
TJSONFloattypeFloat type used in JSON unit.
TJSONFloatNumberClasstypeClass of TJSONFloatNumber.
TJSONInstanceTypeenumerationType of instance to create.
TJSONInt64NumberClasstypeClass of TJSONInt64Number.
TJSONIntegerNumberClasstypeClass of TJSONIntegerNumber.
TJSONLargeInttypeAlias type.
TJSONLargeIntNumbertypeClass for TLargeInt value.
TJSONLargeIntNumberClasstypeClass reference for TJSONLargeIntNumberClass.
TJSONNullClasstypeClass of TJSONNull.
TJSONNumberTypeenumerationEnumerate the different kind of numerical types.
TJSONObjectClasstypeClass of TJSONObject.
TJSONObjectIteratorprocedure typeIterator callback for TJSONObject.Iterate.
TJSONParserHandlerprocedure typeCallback to parse JSON into JSONData.
TJSONQWordNumberClasstypeClass of TJSONQWordNumber.
TJSONStringClasstypeClass of TJSONString.
TJSONStringParserHandlerprocedure typeJSON String value parser handler prototype.
TJSONStringTypetypeString type used in JSON unit.
TJSONtypeenumerationPossible types of JSON data.
TJSONUnicodeStringTypetypeAlias for Unicode strings.
TJSONVarianttypeVariant alias type.

Constants

ActualValueJSONTypesconstantSet constant with JSON types of non-structured values that are not null.
AsCompactJSONconstantConstant to format JSON with minimal whitespace and unquoted property names.
AsCompressedJSONconstantConstant to format JSON with minimal whitespace.
AsJSONFormatconstantFormatting options to reproduce JSON.
DefaultFormatconstantDefault formatting options.
DefaultIndentSizeconstantDefault indent size for formatted JSON.
jitNumberLargeIntconstantLargeInt type definition.
StructuredJSONTypesconstantSet constant with JSON types of structured values.
ValueJSONTypesconstantSet constant with JSON types of non-structured values.

Uses

An experiment, built from development snapshots. Not the official Free Pascal or Lazarus documentation.