You are not logged in.
Pages: 1
I have a json string containing a collection of objects that I want to read into an object list. Are then any examples that showcase this behaviour? Essentially just like jsontoobject using a objectlist but with the ability to customize field mappings.
Offline
Hello!
I have simple test case, that probably can help you.
TObjectList - serialization and deserialization works as expected.
Also, i included code that should map fields, but that not works, last line of code (commented).
Personally i have question about generic version of TObjectList<T>.
With similar usage, serialized text not contain objects (that behavior included in test case), while documentation says:
You could also create a TObjectList, or - even better for newer versions of Delphi supporting the generics syntax - a TObjectList<T>
{$M+}
TCustomClass = class
private
FField1: RawUTF8;
FField2: RawUTF8;
published
property Field1: RawUTF8 read FField1 write FField1;
property Field2: RawUTF8 read FField2 write FField2;
end;
{$M-}
procedure TForm1.Serialize();
var
i: Integer;
CustomClassObject: TCustomClass;
CustomClassList: TObjectList;
begin
CustomClassList := TObjectList.Create(True);
// fill test data
for i := 0 to 4 do
begin
CustomClassObject := TCustomClass.Create();
CustomClassObject.FField1 := 'value ' + StringToUTF8(i.ToString);
CustomClassObject.FField1 := 'another value ' + StringToUTF8(i.ToString);
// TObjectList
CustomClassList.Add(CustomClassObject);
end;
// Serialize and show results in memo
MemoObjectList.Lines.Text := UTF8ToString(ObjectToJSON(CustomClassList, [woStoreClassName, woHumanReadable]));
// Cleanup
CustomClassList.Free();
// Additional generic serialization test
MemoGenericObjectList.Lines.Text := UTF8ToString(GenSerializationTest.DoSerializeTest());
end;
initialization
// TJSONSerializer.RegisterCustomSerializerFieldNames(TCustomClass, ['field1', 'field2'], ['RenamedField1', 'RenamedField2']);
Last edited by George (2017-04-10 09:02:51)
Offline
Personally i have question about generic version of TObjectList<T>.
Found answer here: https://synopse.info/forum/viewtopic.php?id=2943
Offline
Personally i have question about generic version of TObjectList<T>.
Found answer here: https://synopse.info/forum/viewtopic.php?id=2943
Not like dependency on REST.Json.
So, i've changed test case code to use mORMot functionality for TObjectList<TCustomObjectForGenerics> serialization / deserialization.
There is memory leak in JSONToObject, when json text is not correct.
Last edited by George (2017-04-06 21:34:38)
Offline
Test project updated, now looks more simple, use TObjectList serialization to serialize/deserialize TObjectList<T> objects.
Offline
Can anyone help with complicated class deserialization (UnitComplicatedObjectSerializationTest)?
Added test case with class that include TObjectList fields with nested objects that include another TObjectList.
Serialization works well, but i can't restore objects from json. I've used documentation that says:
won't handle TObjectList (even if ObjectToJSON is able to serialize them) since has no way of knowing the object type to add (TCollection.Add is missing), unless: 1. you set the TObjectListItemClass property as expected, and provide a TObjectList object, or 2. woStoreClassName option has been used at ObjectToJSON() call and the corresponding classes have been previously registered by TJSONSerializer.RegisterClassForJSON()
Both conditions are implemented, but without luck.
Offline
Welcome back to my blog
I updated test/demo project with TDocVariant (with TDocVariantData) use case.
Works good, TDocVariant is very powerful part of framework.. Again, not simple as pie, but check the demo and don't forget that every mORMot method well commented.
Last edited by George (2017-04-13 17:59:41)
Offline
AB, can you take a look at this module to approve that it use TDocVariant by design?
There is 4 classes for serialization / deserialization:
TComplicatedClass
TCustomObjectWithNestedObjLists
TSimpleObjectOne
TSimpleObjectTwo
One class to execute test:
TComplSerializationWithTDocVariantTest
I have one place where i can't add array item by reference:
class function TComplicatedClass.SerializeAsVariant(ComplicatedClass: TComplicatedClass): Variant;
var
idx: Int64;
CustomObjectWithNestedObjLists: TCustomObjectWithNestedObjLists;
VObj: Variant;
begin
with TDocVariantData(Result) do
begin
// Init result
InitObject([], DocVariantOptionsForSerialization);
// Add class info
AddValue('className', Self.QualifiedClassName);
// Add data
{ Name }
AddValue('name', ComplicatedClass.Name);
{ field1 }
// add empty array, to save items from TObjectList<T>
idx := AddValue('field1', _Arr([], DocVariantOptionsForSerialization));
// direct access to field2 by index by reference
with _safe(Value[idx], dvArray)^ do
// serialize ComplicatedClass.Field1 per item and add it to array
for CustomObjectWithNestedObjLists in ComplicatedClass.Field1 do
begin
VObj := TCustomObjectWithNestedObjLists.SerializeAsVariant(CustomObjectWithNestedObjLists);
AddItem(VObj); // << Here VObj is Variant with TDocVariant object
VObj := Unassigned; // Without this line i have memory leaks, so AddItem() not owning VObj as i want.
end;
end;
end;
Last edited by George (2017-04-13 16:19:09)
Offline
Pages: 1