#1 2021-02-15 18:18:53

keinn
Member
Registered: 2014-10-20
Posts: 103

JSONToObject seems to change the json content

i test JSONToObject function ,it works , but seems to change the json content.
i want to reuse the json content ,

follow the Documentation's guide ,
i make a local copy use @vJson[1] or UniqueRawUTF8
but after JSONToObject ,i write the vJson variable content to a memo , it only contains "classname"

var
  vJson:rawutf8;
  vJsonP:PUTF8Char;
begin

    vJsonP :=UniqueRawUTF8(vJson);//@vJson[1];
    vObj :=TMyObject(JSONToNewObject(vJsonP,vObjValid,[j2oIgnoreUnknownProperty]));
    memo1.text:=vJson; //<--here it only output "classname" not the whole json content

end;

seems local copy did not work

Offline

#2 2021-02-15 18:27:40

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

Re: JSONToObject seems to change the json content

It is as expected.

When you call UniqueRawUtf8() your vJson variable will become unique, and JsonToNewObject() will parse it in-place.
Your local copy is parsed in-place, so modified.
This is as documented.

But if you make a copy of vJson before, this copy won't be escaped.

To make a copy with no memory allocation for small content, use a local TSynTempBuffer instance.
There are plenty of samples of TSynTempBuffer use in mORMot.pas.

Offline

#3 2021-02-15 19:31:01

keinn
Member
Registered: 2014-10-20
Posts: 103

Re: JSONToObject seems to change the json content

thanks for the quick reply .
so , if i DONT need to reuse the json, there is no need to call UniqueRawUtf8,
just
vObj :=TMyObject(JSONToNewObject(Pointor(vJson),vObjValid,[j2oIgnoreUnknownProperty]));
will be ok ? am i right?

Offline

#4 2021-02-15 19:47:09

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

Re: JSONToObject seems to change the json content

Yes, if vJson is not used afterwards.

Offline

Board footer

Powered by FluxBB