Json in Pascal
program JSONExample;
uses
SysUtils, Classes, fpjson, jsonparser;
type
TResponse1 = class
public
Page: Integer;
Fruits: TStringList;
constructor Create;
destructor Destroy; override;
end;
TResponse2 = class
public
Page: Integer;
Fruits: TStringList;
constructor Create;
destructor Destroy; override;
end;
constructor TResponse1.Create;
begin
inherited;
Fruits := TStringList.Create;
end;
destructor TResponse1.Destroy;
begin
Fruits.Free;
inherited;
end;
constructor TResponse2.Create;
begin
inherited;
Fruits := TStringList.Create;
end;
destructor TResponse2.Destroy;
begin
Fruits.Free;
inherited;
end;
procedure EncodeBasicTypes;
var
JSONData: TJSONData;
begin
// Encoding basic data types to JSON strings
JSONData := TJSONBoolean.Create(True);
WriteLn(JSONData.AsJSON);
JSONData.Free;
JSONData := TJSONIntegerNumber.Create(1);
WriteLn(JSONData.AsJSON);
JSONData.Free;
JSONData := TJSONFloatNumber.Create(2.34);
WriteLn(JSONData.AsJSON);
JSONData.Free;
JSONData := TJSONString.Create('gopher');
WriteLn(JSONData.AsJSON);
JSONData.Free;
end;
procedure EncodeArraysAndObjects;
var
JSONArray: TJSONArray;
JSONObject: TJSONObject;
begin
// Encoding arrays and objects
JSONArray := TJSONArray.Create(['apple', 'peach', 'pear']);
WriteLn(JSONArray.AsJSON);
JSONArray.Free;
JSONObject := TJSONObject.Create;
JSONObject.Add('apple', 5);
JSONObject.Add('lettuce', 7);
WriteLn(JSONObject.AsJSON);
JSONObject.Free;
end;
procedure EncodeCustomTypes;
var
Res1, Res2: TJSONObject;
FruitsArray: TJSONArray;
begin
// Encoding custom types
Res1 := TJSONObject.Create;
FruitsArray := TJSONArray.Create(['apple', 'peach', 'pear']);
Res1.Add('Page', 1);
Res1.Add('Fruits', FruitsArray);
WriteLn(Res1.AsJSON);
Res1.Free;
Res2 := TJSONObject.Create;
FruitsArray := TJSONArray.Create(['apple', 'peach', 'pear']);
Res2.Add('page', 1);
Res2.Add('fruits', FruitsArray);
WriteLn(Res2.AsJSON);
Res2.Free;
end;
procedure DecodeJSON;
var
JSONData: TJSONData;
JSONObject: TJSONObject;
Num: Double;
Strs: TJSONArray;
begin
// Decoding JSON data
JSONData := GetJSON('{"num":6.13,"strs":["a","b"]}');
if Assigned(JSONData) and (JSONData.JSONType = jtObject) then
begin
JSONObject := TJSONObject(JSONData);
WriteLn(JSONObject.AsJSON);
Num := JSONObject.Get('num', 0.0);
WriteLn(Num);
Strs := JSONObject.Get('strs', TJSONArray(nil));
if Assigned(Strs) and (Strs.Count > 0) then
WriteLn(Strs.Strings[0]);
end;
JSONData.Free;
end;
procedure DecodeIntoCustomType;
var
JSONData: TJSONData;
JSONObject: TJSONObject;
Response: TResponse2;
begin
JSONData := GetJSON('{"page": 1, "fruits": ["apple", "peach"]}');
if Assigned(JSONData) and (JSONData.JSONType = jtObject) then
begin
JSONObject := TJSONObject(JSONData);
Response := TResponse2.Create;
try
Response.Page := JSONObject.Get('page', 0);
Response.Fruits.AddStrings(TJSONArray(JSONObject.Find('fruits')));
WriteLn(Format('Page: %d, First Fruit: %s', [Response.Page, Response.Fruits[0]]));
finally
Response.Free;
end;
end;
JSONData.Free;
end;
begin
EncodeBasicTypes;
EncodeArraysAndObjects;
EncodeCustomTypes;
DecodeJSON;
DecodeIntoCustomType;
end.
This Pascal program demonstrates JSON encoding and decoding using the fpjson
and jsonparser
units, which are part of the Free Pascal Compiler (FPC) standard library. Here’s a breakdown of the example:
We define two custom types,
TResponse1
andTResponse2
, similar to the structs in the original Go example.The
EncodeBasicTypes
procedure shows how to encode basic data types (boolean, integer, float, and string) to JSON.EncodeArraysAndObjects
demonstrates encoding arrays and objects (maps in Go) to JSON.EncodeCustomTypes
shows how to create and encode custom types as JSON objects.DecodeJSON
illustrates how to decode a JSON string into Pascal data structures.DecodeIntoCustomType
demonstrates decoding JSON data into a custom type (TResponse2
).
To run this program, you’ll need to have the Free Pascal Compiler installed. Save the code in a file with a .pas
extension (e.g., json_example.pas
), and compile it using:
fpc json_example.pas
Then run the compiled executable:
./json_example
This example covers the basics of JSON handling in Pascal. For more advanced usage, refer to the FPC documentation for the fpjson
and jsonparser
units.