Skip to content

EasyJson - Effortless JSON Handling for Delphi with Fluent Simplicity.

License

Notifications You must be signed in to change notification settings

tinyBigGAMES/EasyJson

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

10 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

EasyJson

Chat on Discord Follow on Bluesky

πŸš€ Overview

TEasyJson is a powerful and intuitive Delphi class that simplifies JSON manipulation. Designed with a fluent interface, it enables developers to seamlessly create, modify, and traverse JSON objects and arrays using method chaining. Whether handling simple key-value pairs or deeply nested structures, TEasyJson ensures efficiency, readability, and flexibility.

✨ Features

  • πŸš€ Fluent interface for effortless JSON manipulation
  • πŸ“‚ Supports both JSON objects and arrays
  • πŸ”— Chainable methods for concise and readable code
  • πŸ—οΈ Easily constructs and modifies nested JSON structures
  • 🎨 Provides formatted JSON output for better readability
  • ⚑ Optimized for performance and efficiency

πŸ“₯ Installation

Getting started with TEasyJson is quick and easy:

  1. Copy TEasyJson.pas into your project.
  2. Add TEasyJson to your uses clause.
  3. Start Coding! Enjoy simplified JSON handling with method chaining.

πŸ› οΈ Usage

πŸ”Ή Creating a JSON Object

var EJ: TEasyJson;
begin
  EJ := TEasyJson.Create;
  EJ.Put('name', 'Alice')
    .Put('age', 25)
    .AddArray('hobbies')
    .Put(0, 'Reading')
    .Put(1, 'Cycling');

  WriteLn(EJ.Format());
end;

🏷️ Output:

{
  "name": "Alice",
  "age": 25,
  "hobbies": [
    "Reading",
    "Cycling"
  ]
}

πŸ”Ή Creating a JSON Object from a String

var EJ: TEasyJson;
begin
  EJ := TEasyJson.Create('{"name":"John","age":30}');
  WriteLn(EJ.Format());
end;

πŸ”Ή Adding and Modifying Values

EJ.Put('email', 'alice@example.com');
EJ.Put('age', 26); // Updates existing key

πŸ”Ή Working with JSON Arrays

var EJ: TEasyJson;
begin
  EJ := TEasyJson.Create;
  EJ.AddArray('numbers')
    .Put(0, 10)
    .Put(1, 20)
    .Put(2, 30);

  WriteLn(EJ.Format());
end;

πŸ”Ή Nesting JSON Objects

EJ.AddObject('address',
  function(E: TEasyJson): TEasyJson
  begin
    Result := E.Put('city', 'New York')
               .Put('zip', '10001');
  end);

πŸ”Ή Accessing JSON Elements

WriteLn(EJ['name'].AsString);  // Alice
WriteLn(EJ['age'].AsInteger);  // 26
WriteLn(EJ['hobbies'][1].AsString);  // Cycling

πŸ“– API Reference

The TEasyJson class provides a robust set of methods and properties for seamless JSON manipulation.

πŸ”Ή Constructors

  • Create() – Creates an empty JSON object.
  • Create(const AJson: string) – Parses a JSON string.
  • Create(const AJsonValue: TJSONValue) – Wraps an existing JSON value.

πŸ”Ή Methods

  • Put(AKey: string; AValue: Variant): TEasyJson – Adds or updates a key.
  • Put(AIndex: Integer; AValue: Variant): TEasyJson – Sets an array element.
  • Add(AKey: string; AValue: Variant): TEasyJson – Adds a new key-value pair.
  • AddArray(AKey: string): TEasyJson – Adds an empty array.
  • AddObject(AKey: string; AFunc: TFunc<TEasyJson, TEasyJson>): TEasyJson – Adds a nested object.
  • ToString(): string – Returns a compact JSON string.
  • Format(): string – Returns formatted JSON.
  • Count(): Integer – Returns the number of elements.
  • AsString(): string – Converts a JSON value to a string.
  • AsInt32r(): Int32 – Converts a JSON value to an int32.
  • AsFloat(): Double – Converts a JSON value to a float.
  • AsBoolean(): Boolean – Converts a JSON value to a boolean.

πŸ”Ή Properties

  • Items[AKeyOrIndex: Variant]: TEasyJson – Accesses elements by key or index.

See EasyJson.pas for the full documented API.

πŸ’¬ Support & Resources

🀝 Contributing

We welcome contributions to EasyJson! πŸš€

πŸ’‘ Ways to Contribute:

  • πŸ› Report Bugs – Help improve TEasyJson by submitting issues.
  • ✨ Suggest Features – Share ideas to enhance its functionality.
  • πŸ”§ Submit Pull Requests – Improve the codebase and add features.

πŸ† Contributors

πŸ“œ License

EasyJson is distributed under the BSD-3-Clause License, allowing redistribution and modification in both source and binary forms. See the LICENSE for details.

πŸ’– Support & Sponsorship

Your support keeps EasyJson evolving! If you find this library useful, please consider sponsoring the project. Every contribution helps drive future enhancements and innovations.

Other ways to support:

  • ⭐ Star the repo – Show your appreciation.
  • πŸ“’ Share with your network – Spread the word.
  • πŸ› Report bugs – Help improve TEasyJson.
  • πŸ”§ Submit fixes – Contribute by fixing issues.
  • πŸ’‘ Suggest features – Help shape its future.

πŸš€ Every contribution makes a difference – thank you for being part of the journey!


πŸ”₯ EasyJson – Effortless JSON Handling for Delphi with Fluent Simplicity.

Delphi

Made with ❀️ in Delphi