delphi - Serializing JSON object for an instance instantiated through a metaclass -


the superobject library has generic method serializing objects:

type    tsomeobject = class    ...    end;  var    ljson       : isuperobject;    lcontext    : tsuperrtticontext;    lsomeobject : tsomeobject; begin    lsomeobject := tsomeobject.create;    lcontext := tsuperrtticontext.create;    ljson := lcontext.asjson<tsomeobject>(lsomeobject); 

but i'm dealing metaclass instances.
object structure:

tjsonstructure = class(tobject);  treqbase = class(tjsonstructure) private    token: int64; public end;  treqlogin = class(treqbase) private    username,    password: string;    module  : integer; public end;   type    twebact = (ttlogin,               ttsignin);  treqclass = class of treqbase;  const    cwebactstructures: array[twebact] of    record       requestclass : treqclass;    end    = (       { ttlogin  } (requestclass: treqlogin;),       { ttsignin } (requestclass: treqsignin;)     // not in definitions above      );  

if try:

var    lcontext      : tsuperrtticontext;    ljson         : isuperobject;    lrequestclass : treqclass;    lrequestbase  : treqbase; begin    lrequestclass := cwebactstructures[ttlogin].requestclass;    lrequestbase := lrequestclass.create;     // instance of type treqlogin    lcontext := tsuperrtticontext.create;    ljson := lcontext.asjson<treqbase>(lrequestbase); 

i treqbase serialized: ljson.asstring = '{"token":-12346789}'
want have treqlogin serialized.
tried:

  • lcontext.asjson< treqlogin >(lrequestbase); won't work: incompatible types) [besides, have list/handle possible requestclass types in routine]

  • lcontext.asjson< lrequestbase.classtype >(lrequestbase) neither: e2531 method 'asjson' requires explicit type argument(s)

is there way can have lrequestbase serialized treqlogin, treq... without having code them all?

the trick not create tsuperrtticontext , call asjson method myself, use:

ljson := requestbase.tojson; 

the tojson method create tsuperrtticontext behind scenes , handle necessary conversions.

with big thank marjan help.


Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

jquery - How would i go about shortening this code? And to cancel the previous click on click of new section? -