a simple way to serialize / deserialize your classes by using decorators (custom attributes)
To request features or report a bug, open a github issue with details/steps to reproduce
Below is a sample pulled from the console tester application which shows a possible use for ezjson
{ TTestDecorated }
(*
a simple object decorated with a custom name to be used when serializing and
deserializing, as well as some simple typed properties to include in
serialization
*)
[JsonObject('myTestObject')]
TTestDecorated = class(TObject)
private
FTest : String;
FTestInt : Integer;
published
[JsonProperty('test')]
property Test : String read FTest write FTest;
[JsonProperty('testInteger')]
property TestInt : Integer read FTestInt write FTestInt;
end;
...
procedure TestSimple;
var
LTest : TTestDecorated;
LJSON,
LError : String;
begin
//setting a value on our test
LTest := TTestDecorated.Create;
LTest.Test := 'a test';
//calling serialize
if not (EZSerialize<TTestDecorated>(LTest, LJSON, LError)) then
raise Exception.Create(LError) //failed to serialize
else
WriteLn(LJSON); //success
end;
and here's the output json
{ "myTestObject" : { "test" : "a value", "testInteger" : 0 } }
- make sure your decorated properties are in the published section
- object / interface properties can be decorated as long as they also have property decorators
- JsonObject is not required (if not supplied will determine name from class) but JsonProperty is
- works as "opt-in" so non-decorated properties will not be serialized without
- download and install lazarus if you don't already have it (http://www.lazarus-ide.org) or by using the super simple fpcupdeluxe (https://github.com/LongDirtyAnimAlf/fpcupdeluxe/releases)
- git clone this repo
- open ezjson_test.lpr and attempt to compile/run (F9 Key)
- this project shows some basic usage of the library
- also, by going to
Toolbar -> Project\Project Options\Paths
you can copy theother units
text to include in your own project
- add
.\src
path to your projectother units
- also to note, this project requires that you use the latest trunk fpc / lazarus (another reason to use fpcupdeluxe :) )
Tip Jar
- 💵 BTC - bc1q55qh7xptfgkp087sfr5ppfkqe2jpaa59s8u2lz
- 💶 LTC - LPbvTsFDZ6EdaLRhsvwbxcSfeUv1eZWGP6