Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSONMarshal: Is it possible to serialize/desrialize into/from 'plain' JSON (without metadata)?

I'm writing a DataSnap Rest Client (in Delphi) and I want it to provide data in json format to a lot of platforms (like C#, java, C++, Delphi). I'm using TJsonMarshal to serialize an object. I'm using something similar to this:

marshal := TJSONMarshal.Create(TJSONConverter.create);
jsonString := marshal.marshal(myObject).ToString;

But When I do it, the generated JSON is something like this:

{"type":"WJsonObj.TWPedido","id":1,"fields":
  {"Numero":1234,"Data":41606.7632623727,"VlrTotal":2543,
  "Produtos":
    [{"type":"WJsonObj.TWProdutoPedido","id":2,"fields":
      {"Codigo":"P001","Descr":"Computador","Qtde":1,"VrUnitario":1500}},

...

      {"type":"WJsonObj.TWProdutoPedido","id":4,"fields":
        {"Codigo":"P003","Descr":"Projetor","Qtde":1,"VrUnitario":745}}
    ]
  }
}

And I wanted plain JSON, without metadata ('type', 'id' and 'fields'), so I wouldn't have extra json parsing in non-Delphi platforms. Is there a way to force the TJsonMarshal to serialize into "plain" JSON?

like image 941
jRicardo Avatar asked Nov 18 '25 21:11

jRicardo


1 Answers

You should use System.JSON and REST.JSON instead of Data.DBXJson and Data.DBXJSONReflect

var
  foo, newfoo: TFoo;
  s: string;

  foo := TFoo.Create;
  s := TJson.ObjectToJsonString(foo);

  newfoo := TJson.JsonToObject<TFoo>(s);
like image 84
Dalija Prasnikar Avatar answered Nov 21 '25 10:11

Dalija Prasnikar