A lightweight, user-friendly JSON library for Free Pascal with automatic reference counting.
Note
π¦ This package was extracted from TidyKit-FP, which was archived earlier due to its size. SimpleJSON-FP contains only the JSON functionality from the original TidyKit library, making it more focused and easier to use.
Working with JSON in Pascal has never been easier. SimpleJSON-FP provides a clean, intuitive API with automatic memory management.
// JSON has never been easier
var
Json: IJSONObject;
Person: IJSONObject;
begin
// Parse JSON with ease
Json := TJSON.Parse('{"name":"Pascal","age":50}').AsObject;
WriteLn('Name: ', Json['name'].AsString);
// Build JSON programmatically
Person := TJSON.Obj;
Person.Add('name', 'Alice');
Person.Add('age', 30);
Person.Add('isActive', True);
// No need to worry about freeing - automatic memory management!
WriteLn(Person.ToString(True)); // Pretty-printed JSON
end;- Focused and Lightweight: Just JSON, done right - no bloat, no unnecessary dependencies
- Modern Pascal: Clean, consistent API with FPC 3.2.2 compatibility
- Rock-Solid Reliability: Extensive test suite with 41 comprehensive test cases
- Standards Compliant: Fully adheres to ECMA-404 and RFC 8259 JSON specifications
- Smart Memory Management: Forget manual Free calls with interface-based reference counting
- Full Unicode Support: Proper handling of UTF-16, escape sequences, and surrogate pairs
- π§ SimpleJSON-FP: A Simple JSON Library for Free Pascal
Parse, create and modify JSON with ease
// Parse existing JSON
var
Config: IJSONValue;
User: IJSONObject;
begin
Config := TJSON.Parse(jsonText);
// Build JSON programmatically
User := TJSON.Obj;
User.Add('name', 'Alice');
User.Add('age', 30);
end;- π JSON: Memory-managed JSON with full Unicode support
- Interface-based design for automatic memory management
- Comprehensive JSON parsing and generation
- Support for all JSON data types (objects, arrays, strings, numbers, booleans, null)
- Pretty printing and compact output options
- Full Unicode support with proper escape sequence handling
- π Delphi System.JSON Compatibility: Easy migration from Delphi
- Drop-in replacement for Delphi's
System.JSONunit - Same class names:
TJSONObject,TJSONArray,TJSONValue, etc. - Same methods:
ParseJSONValue,AddPair,GetValue,ToJSON, etc. - Port your Delphi code to Lazarus with minimal changes
- Drop-in replacement for Delphi's
- Improved scanner validation and diagnostics for number parsing errors (including positional information)
- Error handling with descriptive messages
- Easy-to-use factory methods
- Standards compliant (ECMA-404, RFC 8259)
- Clone the repository:
git clone https://github.com/ikelaiah/simplejson-fp-
Open / start a new project in Lazarus IDE
-
Go to
PackageβOpen Package File (.lpk)... -
Navigate to the SimpleJSON-FP packages in the
packages/lazarus/folder and selectsimplejson_fp.lpk -
In the package window that opens, click
Compile -
Click
Use β Add to Projectto install the package
The SimpleJSON-FP package is now ready to use in your Lazarus project.
- Clone the repository:
git clone https://github.com/ikelaiah/simplejson-fp- Add the source directory to your project's search path.
uses
// Option 1: SimpleJSON (interface-based, automatic memory management)
SimpleJSON; // All JSON functionality with ARC
// Option 2: System.JSON (Delphi-compatible, class-based)
System.JSON; // Delphi System.JSON compatible APIvar
Json: IJSONObject;
begin
Json := TJSON.Parse('{"name":"John"}').AsObject;
WriteLn(Json['name'].AsString);
// No need to free - automatic memory management!
end;var
Obj: TJSONObject;
begin
Obj := TJSONObject.ParseJSONValue('{"name":"John"}') as TJSONObject;
try
WriteLn(Obj.GetValueString('name'));
finally
Obj.Free; // Manual free required, just like Delphi
end;
end;π‘ Tip: SimpleJSON's automatic memory management is safer and cleaner. Use System.JSON only when porting existing Delphi code.
See System.JSON Compatibility Guide for detailed migration instructions.
var
Person: IJSONObject;
Address: IJSONObject;
Hobbies: IJSONArray;
JSON: string;
Value: IJSONValue;
begin
// Create a person object
Person := TJSON.Obj;
Person.Add('name', 'John Smith');
Person.Add('age', 30);
Person.Add('isActive', True);
// Create and add an address object
Address := TJSON.Obj;
Address.Add('street', '123 Main St');
Address.Add('city', 'Springfield');
Address.Add('zipCode', '12345');
Person.Add('address', Address);
// Create and add a hobbies array
Hobbies := TJSON.Arr;
Hobbies.Add('reading');
Hobbies.Add('cycling');
Hobbies.Add('swimming');
Person.Add('hobbies', Hobbies);
// Convert to JSON string with pretty printing
JSON := Person.ToString(True);
WriteLn(JSON);
// Parse JSON string
JSON := '{"name":"Jane Doe","age":25,"skills":["Pascal","Python"]}';
Value := TJSON.Parse(JSON);
Person := Value.AsObject;
// Access values
WriteLn('Name: ', Person['name'].AsString);
WriteLn('Age: ', Person['age'].AsInteger);
WriteLn('First Skill: ', Person['skills'].AsArray[0].AsString);
// Error handling
try
Value := TJSON.Parse('{invalid json}');
except
on E: EJSONException do
WriteLn('Error: ', E.Message);
end;
end;| Module | Windows 11 | Ubuntu 24.04.2 |
|---|---|---|
| SimpleJSON | β | β |
| SimpleJSON.Factory | β | β |
| SimpleJSON.Parser | β | β |
| SimpleJSON.Scanner | β | β |
| SimpleJSON.Types | β | β |
| SimpleJSON.Writer | β | β |
- Uses only standard Free Pascal RTL units
- No external dependencies required
- Free Pascal Compiler (FPC) 3.2.2+
- Lazarus 3.6+
- Basic development tools (git, terminal, etc)
For detailed documentation, see:
- π Cheat Sheet
- β FAQ - Common questions about design decisions and patterns
- π JSON Reference
- Open the
TestRunner.lpiusing Lazarus IDE - Compile the project
- Run the Test Runner:
$ cd tests
$ ./TestRunner.exe -a --format=plain- The test suite includes 41 tests that cover parsing, writing, edge-cases, roundtrip validation, and fuzz tests.
- A diagnostic (Test34) will dump a JSON file to
tests/failed_large_array.jsonif a large array parse fails; useful for debugging.
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
- Fork the Project
- Create your Feature Branch (git checkout -b feature/AmazingFeature)
- Commit your Changes (git commit -m 'Add some AmazingFeature')
- Push to the Branch (git push origin feature/AmazingFeature)
- Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Originally part of TidyKit-FP
- FPC Team for Free Pascal
- Contributors and maintainers
Feedback and suggestions are welcome! See the issues page to contribute ideas or track progress.