#1 2015-11-26 19:13:16

ioda19
Member
Registered: 2015-11-18
Posts: 14

JSON with class and Reserved Word or KeyWord

Hi,

I have this JSON

{
  "Product":
  {
    "blablabla":"test",
    "Unit": {
      "Code_0": "UN",
      "Code_1": "UN",
      ....
    }
  }
}

How I can create my class with UNIT keyword in Delphi6 to serialize this JSON
I can't create like this because Delphi return error

TProduct = class(TSynPersistent)
private
  Fblablabla : RAWUTF8 ;
  FUnit : TUnit;
published
  property blablabla: RAWUTF8 read Fblablabla write Fblablabla;
  property Unit : TUnit read FUnit;   <----------  ERROR HERE
end;

Could I map field like AutoMapKeywordFields option into TSQLRecord class ?

Offline

#2 2015-11-27 10:36:51

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

Re: JSON with class and Reserved Word or KeyWord

I'm afraid this is a language feature/limitation...

What you could do is use a TDocVariantData storage.
Here, the key names would be plain strings, so would not interfere with Delphi keywords.

Offline

#3 2016-02-16 18:22:43

ioda19
Member
Registered: 2015-11-18
Posts: 14

Re: JSON with class and Reserved Word or KeyWord

Hi,

I don't understant how I can build my object with TDocVariantData
Could you show me an exemple

I always use JSONToObject to unserialize my JSON

Thank you

Offline

#4 2016-02-16 22:24:25

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

Re: JSON with class and Reserved Word or KeyWord

If you use a record with custom text registration, you can set any field name you need.

Offline

#5 2016-02-17 16:41:53

ioda19
Member
Registered: 2015-11-18
Posts: 14

Re: JSON with class and Reserved Word or KeyWord

Yeah I know but I use Classes in my unit
My unit contain more than 30 classes and I don't want to rewrite all my code ton convert it into record

The only idea I have is to replace into JSON the reserved keyword and unserialize it like

   JSONString := StringReplace(JSONString, '"Unit" :', '"UnitKW" :', [rfReplaceAll]);
   JSONString := StringReplace(JSONString, '"begin" :', '"beginKW" :', [rfReplaceAll]); //Do for all keyword

When I'll serialize again I replace the reseved keyword again

   JSONString := StringReplace(JSONString, '"UnitKW" :', '"Unit" :', [rfReplaceAll]);
   JSONString := StringReplace(JSONString, '"beginKW" :', '"begin" :', [rfReplaceAll]); //Do for all keyword

But if you have another idea... go for it

Thank

Offline

#6 2016-02-18 09:42:26

DigDiver
Member
Registered: 2013-04-29
Posts: 137

Re: JSON with class and Reserved Word or KeyWord

In Delphi XE10 I can add & before keyword:

 Type
  TProduct = class(TSynPersistent)
private
  Fblablabla : RAWUTF8 ;
  FUnit : RawUTF8;
  FBegin : RawUTF8;
published
  property blablabla: RAWUTF8 read Fblablabla write Fblablabla;
  property &Unit : RawUTF8 read FUnit write FUnit;
  property &begin: RawUTF8 read FBegin write FBegin;
end;

I have test it and works fine:

procedure TForm14.Button8Click(Sender: TObject);
var
 FProduct : TProduct;
 JSON : RawUTF8;
 FValid : Boolean;
begin

 FProduct := TProduct.Create;
 try
  FProduct.&begin := 'Begin value';
  FProduct.&Unit  := 'Unit value';
  FProduct.blablabla := 'Bla';
  JSON :=   ObjectToJSON(FProduct);

  Memo1.Lines.add(UTF8ToString(JSON));

 finally
  FProduct.Free
 end;

 FProduct := TProduct.Create;
 try
  JSONToObject(FProduct, Pointer(JSON), FValid);
  ShowMessage(FProduct.&begin);
 finally
  FProduct.Free;
 end;
end;

Offline

#7 2016-02-18 13:04:43

ioda19
Member
Registered: 2015-11-18
Posts: 14

Re: JSON with class and Reserved Word or KeyWord

Yeah I already tried it and in delphi 6 it doesn't work.
Delphi 6 is f%%$ing crap but I don't have a choice

So, I tried find and replace on JSON file with 2 600 000 lines and it take 22 seconds
On standard JSON file, I have no difference of time to process it

Just for your information, I don't use delphi StringReplace
I use FastReplace in subtitleworkshop/FastStrings.pas library (https://github.com/dekked/subtitleworkshop)

I just replace this keywork and all it work
unit -> unit_
type -> type_

That's it

Last edited by ioda19 (2016-02-18 13:06:10)

Offline

Board footer

Powered by FluxBB