Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 3 of 3 total
Thread JSON Serialization BUG: Missing Object Property Name
Wed, Dec 2 2015 6:18 PMPermanent Link

Doug B

The unit in the can be used to reproduce a problem I'm having when serializing an object to JSON that contains another nested object.

I'm using EWB 2.03 Build 1.

Here's the output:

{
"field": "field"{
"value": 10,
"description": "description" } }

Here's what I'm expecting.

{
"field": "field",
"Nested": {
"value": 10,
"description": "description" } }

Here's the code:

unit TestCase;

interface

uses WebCore;

type
  TNested = class(TPersistent)
    fValue: Integer;
    fDescription: string;
  published
    property Value: Integer read fValue write fValue;
    property Description: string read fDescription write fDescription;
  end;

  TComposite = class(TPersistent)
    fField: string;
    fNested: TNested;
  public
    constructor Create; override;
    destructor Destroy; override;
    function ToJson: string;
  published
    property Field: string read fField write fField;
    property Nested: TNested read fNested write fNested;
  end;

function TestJson: string;

implementation

constructor TComposite.Create;
begin
 inherited Create;
 fNested := TNested.Create;
end;

destructor TComposite.Destroy;
begin
 fNested.Free;
 inherited Destroy;
end;

function TComposite.ToJson: string;
var
 Writer: TWriter;
begin
 Writer := TWriter.Create;
 try
   Writer.Initialize;
   Save(Writer);
   Result := Writer.Output;
 finally
   Writer.Free;
 end;
end;
   
function TestJson: string;
var
 c: TComposite;
begin
 c := TComposite.Create;
 try
   c.Field := 'field';
   c.Nested.Value := 10;
   c.Nested.Description := 'description';
   Result := c.ToJson;
 finally
   c.Free;
 end;
end;

end.
Thu, Dec 3 2015 2:35 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Doug,

<< The unit in the can be used to reproduce a problem I'm having when serializing an object to JSON that contains another nested object. >>

It's a bug, the fix is as follows:

WebCore unit:

procedure TPersistent.SaveProperty(AWriter: TWriter; const AName: String);
var
  TempType: Integer;
  TempInstance: TObject;
begin
  TempType:=PropertyType(AName);
  if (TempType <> TYPE_INFO_UNKNOWN) then
     begin
     AWriter.BeginNewLine;
     case TempType of
        TYPE_INFO_BOOLEAN:
           AWriter.BooleanProperty(AName,GetProperty(AName));
        TYPE_INFO_CHAR,TYPE_INFO_STRING:
           AWriter.StringProperty(AName,GetProperty(AName));
        TYPE_INFO_CLASS:
           begin
           TempInstance:=GetProperty(AName);
           if Assigned(TempInstance) and (TempInstance is TPersistent) then
              begin
              AWriter.ObjectProperty(AName); /// Add !!!!
              AWriter.IncIndent; /// Add !!!!
              TPersistent(TempInstance).Save(AWriter);
              AWriter.DecIndent; /// Add !!!!
              end
           else
              AWriter.NullProperty(AName);
           end;
        TYPE_INFO_DATETIME:
           AWriter.DateTimeProperty(AName,GetProperty(AName));
        TYPE_INFO_ENUM,TYPE_INFO_INTEGER:
           AWriter.IntegerProperty(AName,GetProperty(AName));
        TYPE_INFO_DOUBLE:
           AWriter.FloatProperty(AName,GetProperty(AName));
        end;
     end;
end;

Tim Young
Elevate Software
www.elevatesoft.com
Thu, Dec 3 2015 2:41 PMPermanent Link

Doug B

Thanks Tim!

Doug
Image