Jump to content
Sign in to follow this  
pyscripter

TOML delphi parser, writer and serializer

Recommended Posts

TOML is a "config file format for humans", that has gained a lot of traction in the python and rust communities among others.   It is basically the INI file format on steroids.   It compares quite well to alternative formats such as JSON, YAML and XML.   Compared to JSON is way more readable and compact.

 

Since I could not find any Delphi library for processing TOML, I have created my own: toml-delphi.  

 

Features:

  • TOML v1.0.0 compliant.
  • Passes all 734 (valid/invalid) official validation tests.
  • Fast. Single stream tokenizer and lexer that doesn't use regex.
  • Converts TOML documents to Delphi's RTL's TJSONObject, thus allowing for easy traversal, manipulation and query of the generated documents.
  • Includes TTOMLWriter for converting TJSONObjects back to TOML.
  • Provides for easy (de)serialization of Delphi objects and records from/to TOML.

 

This is the interface of the main unit:

  TJSONObjectHelper = class helper for TJSONObject
    function ToTOML(MultilineStrings: Boolean = False; Indent: Integer = 4): string;
    procedure StreamTOML(Stream: TStream; MultilineStrings: Boolean = False; Indent: Integer = 4);
    procedure SaveTOMLtoFile(const FileName: string; MultilineStrings: Boolean = False; Indent: Integer = 4);
    class function FromTOML(const Contents: string): TJSONObject; overload;
    class function FromTOML(Contents: TBytes): TJSONObject; overload;
    class function FromTOML(Stream: TStream): TJSONObject; overload;
    class function FromTOMLFile(const FileName: string): TJSONObject;
  end;

  ETOMLSerializer = class(Exception);

  TTOMLSerializer = class
    class function Serialize<T>(const AValue: T): string; overload;
    class function Deserialize<T>(const ATOML: string): T; overload;
  end;

Example usage:

 

You can convert TOML source to TJSONObject using one of the FromTOML functions. For example to parse a TOML file you use:

var JsonObject := TJSONObject.FromTOMLFile(FileName);

//or for parsing a TOML string:

var JsonObject := TJSONObject.FromTOML(TOMLstring);

 

To convert a TJSONObject to TOML you use one of the methods ToTOML, StreamTOML or SaveTOMLToFile. For example:

TOMLString := JsonObject.ToTOML;

// or

JsonObject.SaveTOMLToFile(FileName);

 

Example serialization:

type
  TTestRec = record
    IntValue: Integer;
    FloatValue: double;
    StringValue: string;
    DateValue: TDateTime;
    ArrayValue: TArray<string>;
 end;

 procedure TestSerializer;
 var
   Rec: TTestRec;
   TOMLString: string;
 begin
   Rec.IntValue := 123;
   Rec.FloatValue := 3.14;
   Rec.StringValue := 'abc';
   Rec.DateValue := Now;
   Rec.ArrayValue := ['A', 'B', 'C'];

   Writeln('Serialized record:');
   WriteLn('==================');
   TOMLString := TTOMLSerializer.Serialize(Rec);
   Writeln(TOMLString);
   Writeln('Record deserialized and serialized again:');
   Writeln('=========================================');
   Rec := TTOMLSerializer.Deserialize<TTestRec>(TOMLString);
   TOMLString := TTOMLSerializer.Serialize(Rec);
   Writeln(TOMLString);
 end;

Output:

Serialized record:
==================
IntValue = 123
FloatValue = 3.14
StringValue = "abc"
DateValue = "2025-06-18T05:37:02.110+03:00"
ArrayValue = [
    "A",
    "B",
    "C"
]

Record deserialized and serialized again:
=========================================
IntValue = 123
FloatValue = 3.14
StringValue = "abc"
DateValue = "2025-06-18T05:37:02.110+03:00"
ArrayValue = [
    "A",
    "B",
    "C"
]

 

I hope you find it useful.

 

Edited by pyscripter
  • Like 5
  • Thanks 3

Share this post


Link to post

My only problem with that is the license: GPL simply makes it useless for me. But since it's based on another GPL library you probably didn't have a choice.

  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×