XML
OmniXML is a smart way to serialize smth.
First point, your objects should be derived from
TPersistent, TCollection or TCollection item.
Second point. Saved information should be in published section.
I do it like this
uses
OmniXML, OmniXMLPersistent
Saving
var
o : TSomeObject; // derived from TPersistent
...
TOmniXMLWriter.SaveToFile( o, 'c:\path\file.xml', pfAttributes, ofIndent );
Loading
TOmniXMLWriter.LoadFromFile( o, 'c:\path\file.xml' );
JSON
I use superobject library, example from github
TTest = class // Field, Property Support
private
FB: String;
FSubObj: TSubObj;
FSubRec: TSubRec;
FTestSets: TTestSets;
FH: TDateTime;
FJ: TDate;
FK: TTime;
FList: TObjectList<TSubObj>; // or TList<>; But only object types are supported
public
A: Integer;
B: TTestSet;
C: Boolean;
property D: String read FB write FB;
property E: TSubRec read FSubRec write FSubRec;
property F: TSubObj read FSubObj write FSubObj;
property G: TTestSets read FTestSets write FTestSets;
property H: TDateTime read FH write FH;
property J: TDate read FJ write FJ;
property K: TTime read FK write FK;
property L: TObjectList<TSubObj> read FList write FList;
end;
TTestRec = record // Only Field Support
A: Integer;
B: TTestSet;
C: Boolean;
D: String;
E: TSubRec;
F: TSubObj;
G: TTestSets;
H: TDateTime;
J: TDate;
K: TTime;
L: TObjectList<TSubObj>; // or TList<>; But only object types are supported
end;
implementation
...
var
Parse: TTest; // For Class;
S: String;
begin
Parse := TTest.FromJSON('{"A": 1, "B": 0, "C": true, "D": "Hello", "E":{"A": 3, "B": "Delphi"}, "F": {"A": 4, "B": 5}, "G": [0,2], "H": "2014-05-03T03:25:05.059", "J": "2014-05-03", "K": "03:25:05", "L":[{"A": 4, "B": 5},{"A": 6, "B": 7}] }');
S := Parse.AsJSON;
end;
...
var
Parse: TTestRec; // For Record;
S: String;
begin
Parse := TJSON.Parse<TTestRec>('{"A": 1, "B": 0, "C": true, "D": "Hello", "E":{"A": 3, "B": "Delphi"}, "F": {"A": 4, "B": 5}, "G": [0,2], "H": "2014-05-03T03:25:05.059", "J": "2014-05-03", "K": "03:25:05", "L":[{"A": 4, "B": 5},{"A": 6, "B": 7}]}');
S := TJSON.Stringify<TTestRec>(Parse);
end;
Also fix by krapotkin