You are not logged in.
Pages: 1
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
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
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
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
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
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
Pages: 1