#1 2019-10-09 19:26:48

ertank
Member
Registered: 2016-03-16
Posts: 168

@ sign in json string

Hello,

I have following web service format that I need to prepare for using a rest web service. This is for an online sales portal api. I do not have any chance of having it changed to something else.

      "specs":{
         "spec":[
            {
               "@required":"true",
               "@value":"Kullanılmış",
               "@name":"Durumu"
            },
            {
               "@required":"true",
               "@value":"10.0 - 11.9 MP",
               "@name":"Çözünürlük (Megapiksel)"
            },
            {
               "@required":"true",
               "@value":"Kodak",
               "@name":"Marka"
            }
         ]
      }

Above is just a part of a bigger json string. I also have some individual usage like:

"photos":{
         "photo":{
            "@photoId":"0",
            "url":"http://images.gittigidiyor.com/2941/KODAK-Z915-10MP-10X-ZOOM-HD-SIFIR-URUN__29416457_0.jpg"
         }
      }

These are json strings that I need to generate using records. I could not figure a solution myself.

Any help is appreciated.

Thanks & regards,
Ertan

Offline

#2 2019-10-09 20:40:39

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

Re: @ sign in json string

You could try https://github.com/synopse/mORMot/commi … 69dcdf54b4

It allows to parse identifier as SQL string (e.g. "@field0"), to define the field name of the record as yo expect.
Any feedback is welcome.

Offline

#3 2019-10-10 04:12:21

ertank
Member
Registered: 2016-03-16
Posts: 168

Re: @ sign in json string

I just need to generate (serialize) json string like in my example for this web service. Responds to requests from the web service seem to be fine (no @ sign in them) as to their documentation.

I maybe wrong. My understanding this commit is for deserialization. It is good to have anyway. Thank you for that.

I’m searching for a solution without manual inserts in json string after serialization. Well, as much as possible of course.

Offline

#4 2019-10-10 08:58:11

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

Re: @ sign in json string

This commit is to enhance TTextWriter.RegisterCustomJSONSerializerFromText() text definition of the record layout.
You can register your record as such:

type
  TMyPhoto = packed record
    photoID, uri: RawUTF8;
  end;
....
TTextWriter.RegisterCustomJSONSerializerFromText(TypeInfo(TMyPhoto), '"@photoId",uri:RawUTF8');

And it will work automatically for both serialization and deserialization of this record, stand-alone or nested in another record, or any dynamic array of this record.

Offline

#5 2019-10-10 09:29:06

ertank
Member
Registered: 2016-03-16
Posts: 168

Re: @ sign in json string

Sorry for the mis understanding. After explanations I used following code in an empty project using Delphi:

...

  TTest = packed record
    abc: string;
  end;

const
  __TTest = '"@abc" string';

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses
  SynCommons;

procedure TForm1.Button1Click(Sender: TObject);
var
  x: TTest;
  Json: RawUTF8;
begin
  x.abc := 'here I am';
  Json := SynCommons.RecordSaveJSON(x, TypeInfo(TTest));
  ShowMessage(string(Json));
end;

initialization
  TTextWriter.RegisterCustomJSONSerializerFromText(TypeInfo(TTest), __TTest);

end.

I see displayed message:

{"@abc":"here I am"}

Thanks for the help.

Offline

#6 2019-10-10 10:20:00

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

Re: @ sign in json string

You are welcome!

smile

Offline

Board footer

Powered by FluxBB