#1 2021-02-24 14:04:47

zensan
Member
Registered: 2015-07-29
Posts: 12

ObjectToJSON vs RecordSaveJson

Hi!

Is that possible to serialize the given object a simple way (dump) just like RecordSaveJson?
The problem is that ObjectToJson always returns empty json if there is no properties! But I do not want any properties, I just have plain fields to serialize..
THe idea is that I want in some future switch from SuperObject to SynOpse for JSON serialization.

type
  TMyClass = class
  public
    name: String;
    value: Double;
  end;

  TMyRecord = record
  public
    name: String;
    value: Double;
  end;

Everything works fine with record, but never with a class.
SuperObject JSON or any other library does this without problem, but I cant get it working with SynCommons unit.
What do I miss?

1) I do not want or will descend from TSynPersistent;
2) I can't add properties/change the interfaces of the class;
3) I tried ObjectToVariant -> VariantToJson;
4) I have mORMot unit in uses list for TJSONSerializer.

Many thanks for your help!

Offline

#2 2021-02-24 15:29:12

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: ObjectToJSON vs RecordSaveJson

We use the standard class RTTI, so we need to have published properties.
For what you expect, you would need to use the Delphi 2010+ RTTI. Since we want to stay compatible with Delphi 7/2007 and even more with FPC which lacks RTTI for non published properties, it is not possible in mORMot yet.

Note that you don't need to inherit from TSynPersistent: you can inherit from TPersistent or whatever class which has {$M+} markups to generate the RTTI for published properties.

Offline

#3 2021-02-24 15:47:45

zensan
Member
Registered: 2015-07-29
Posts: 12

Re: ObjectToJSON vs RecordSaveJson

Thanks for your answer!

Offline

Board footer

Powered by FluxBB