Delphi, TSuperObject > from JSON to Object

408 Views Asked by At

I have in Delphi XE4, TSuperObject:

TAspTransactionBasicData = Class(TObject)
  Currency          : Byte;                     
  Amount            : Currency;
  constructor         Create(aCurrency: Byte; aAmount: Currency);
end;

TStartWorkflowWithBasicData  = Class(TObject)
  AdditionalData    : TAspTransactionBasicData;  // here it is as an Object
  TypeOfWorkflow    : Byte;
  constructor         Create(aAdditionalData: TAspTransactionBasicData; aTypeOfWorkflow: Byte);
  function            toJSon(TObject:TStartWorkflowWithBasicData):String;
end;

and the ToJSon function puts the object into JSON:

function TStartWorkflowWithBasicData.toJSon(TObject:TStartWorkflowWithBasicData):String;
Var
  JSon:     ISuperObject;
  RttiCont: TSuperRttiContext;
begin
  Result   := '';
  RttiCont := TSuperRttiContext.Create;
  JSon     := RttiCont.AsJson<TStartWorkflowWithBasicData>(TObject); // Insert the object into JSON
  Result   := JSon.AsJSon(False);
  RttiCont.Free;
end;

and I would also need the exact opposite, a function fromJSon, which will read the JSON (where the object was inserted) back into the object, but I'm groping...

Can you please advise me?

1

There are 1 best solutions below

0
On

I can't help you with SuperObject, but kbmMW contains a very complete XML, JSON, YAML, BSON, MessagePack + CSV and in next release also TXT (fixed format) serializer/deserializer and object marshaller/unmarshaller.

It will easily convert both ways, and even between formats.

kbmMW Community Edition contains all (except the new TXT format), and is free, even for commercial use (limited by license).

https://components4developers.blog/2019/03/11/rest-easy-with-kbmmw-24-xml_json_yaml_to_object_conversion/