3

I have the following code that serializes a dynamic array of classes. For some reason SuperObject serializes on the private variables instead of the class property names. Can anyone please advise how to fix this behaviour in SuperObject?

class function TJSON.AsJSON<T>(AObject: T; Indent: Boolean = False): string;
var
  Ctx: TSuperRttiContext;
begin
  Ctx := TSuperRttiContext.Create;
  try
    Result := Ctx.AsJson<T>(AObject).AsJSon(Indent);
  finally
    Ctx.Free;
  end;
end;

type
  TMyClass = class
  private
    FName_: String;
    FAge_: Integer;
  public
    property Name: String read FName_ write FName_;
    property Age: Integer read FAge_ write FAge_;
  end;

procedure TFormTest.Button27Click(Sender: TObject);
var
  MyClassArray: TArray<TMyClass>;
  MyClass1, MyClass2: TMyClass;
begin
  MyClass1 := TMyClass.Create;
  MyClass1.Name := 'Joe';
  MyClass1.Age := 10;

  MyClass2 := TMyClass.Create;
  MyClass2.Name := 'Dan';
  MyClass2.Age := 13;

  SetLength(MyClassArray, 2);
  MyClassArray[0] := MyClass1;
  MyClassArray[1] := MyClass2;

  Memo1.Text := TJSON.AsJSON<TArray<TMyClass>>(MyClassArray);
end;

The above code generates the following JSON:

[{"FName_":"Joe","FAge_":10},{"FName_":"Dan","FAge_":13}]

what I am after is the following JSON:

[{"Name":"Joe","Age":10},{"Name":"Dan","Age":13}]
Jan Doggen
  • 8,799
  • 13
  • 70
  • 144
Phillip Roux
  • 1,009
  • 7
  • 12
  • 1
    Not sure if it helps, but maybe you should try to make the properties `published` instead of `public`? – Pateman May 29 '12 at 05:35
  • 1
    in addition to Pateman's comment, I'd recommend reading the documentation(if any), demos and source code –  May 29 '12 at 07:09
  • I also agree that it's better to use properties during serialization. I've posted an issue on this: https://code.google.com/p/superobject/issues/detail?id=61 because bypassing any property setters may become an issue. Rest.Json.TJson works with properties too. – iPath ツ Jan 06 '15 at 06:49

2 Answers2

1

I think it's not possible at this time and that you probably hit this issue. Even Delphi XE2 Datasnap serializes private fields at JSON marshalling and in my view it's just a consequence of a deeper visibility given to the new extended RTTI without considering the limits.

Community
  • 1
  • 1
TLama
  • 75,147
  • 17
  • 214
  • 392
  • 1
    I haven't investigated the issue, but what TLama said looks legitimate and if you don't necessarily need to use classes, switch to records, because they seem to work fine (at least by looking at the demos). – Pateman May 30 '12 at 06:44
  • 1
    I've posted an issue on this: code.google.com/p/superobject/issues/detail?id=61 because bypassing any property setters may become an issue. – iPath ツ Jan 06 '15 at 06:51
0

As far as I know, RTTI operates only on published properties (I may be wrong), but I think that you should simply switch your properties access level to published to get the desired JSON string.

Pateman
  • 2,727
  • 3
  • 28
  • 43
  • The 'classic' RTTI (Delphi until 2009) was limited. Delphi 2010 introduced 'Extended RTTI' which is able to access private fields. – mjn May 29 '12 at 11:02
  • You're right that the old style RTTI could access only `published` fields, however the `TSuperRttiContext` class used in the code example from the question can be used since Delphi 2010, because of the [`HAVE_RTTI`](http://code.google.com/p/superobject/source/browse/trunk/superobject.pas#93) conditional define, see [`the source`](http://code.google.com/p/superobject/source/browse/trunk/superobject.pas#773). So the code above won't be even compilable on Delphi below 2010. – TLama May 29 '12 at 11:49