#1 2024-11-10 09:50:13

anouri
Member
Registered: 2024-02-11
Posts: 47

Different behaviour in variant to json

Hi. mormot result : {"a":"[\"2\"]","b":"2"}
TJSONObject result: {"a":"["2"]","b":"2"}

when I remove line V.a := '["2"]' it is ok! After changing a property json changes.

var
  V: Variant;
  J: TJSONObject;
  NewArray: TJSONArray;
  ss: string;
begin
  ss := '{"a":["1"],"b":"2"}';

  V := _Json(ss);
  V.a := '["2"]';
  ShowMessage(TDocVariantData(V).ToJson);

  j := TJSONObject.ParseJSONValue(ss) as TJSONObject;
  NewArray := TJSONArray.Create;
  NewArray.Add('2');
  j.RemovePair('a').Free;
  j.AddPair('a', NewArray);
  ShowMessage(j.ToJSON);

Last edited by anouri (2024-11-10 09:52:20)

Offline

#2 2024-11-10 10:16:27

anouri
Member
Registered: 2024-02-11
Posts: 47

Re: Different behaviour in variant to json

I found the problem. It seems it was my mistake:

var
  NewArray2: Variant;
begin
  ss := '{"a":["1"],"b":"2"}';
  NewArray2 := _Arr(['2']);
  V := _Json(ss);
  V.a := NewArray2;
  ShowMessage(TDocVariantData(V).ToJson);

or

  LDocDict := DocDict(ss);
  LDocDict['a'] := NewArray2;

Last edited by anouri (2024-11-10 10:22:20)

Offline

#3 2024-11-10 16:10:55

zen010101
Member
Registered: 2024-06-15
Posts: 66

Re: Different behaviour in variant to json

Here is some samples too:

   v := _Json('{"a":["1"],"b":"2"}');
   v.a.Delete(0);
   v.a.Add('02');
   v.a._ := '03'; // same as v.a.Add('03');

Offline

#4 2024-11-11 16:11:52

anouri
Member
Registered: 2024-02-11
Posts: 47

Re: Different behaviour in variant to json

Is it possible change value of element 0 of array v.a without deleting and adding again?

Offline

#5 2024-11-11 21:27:13

flydev
Member
From: France
Registered: 2020-11-27
Posts: 73
Website

Re: Different behaviour in variant to json

v.a := _Arr(['2']);
//
_safe(v)^.Values[0] := _Arr(['3']);
_safe(v.a)^.Values[0] := '4';
//
DocDictFrom(v).Update('a', _Arr(['5']));
with DocListFrom(v.a) do
  U[0] := '6';
{"a":["2"],"b":"2"}
{"a":["3"],"b":"2"}
{"a":["4"],"b":"2"}
{"a":["5"],"b":"2"}
{"a":["6"],"b":"2"}

Last edited by flydev (2024-11-11 21:36:00)

Offline

#6 2024-11-12 11:06:01

anouri
Member
Registered: 2024-02-11
Posts: 47

Re: Different behaviour in variant to json

Thank you so much. Great framework

Offline

Board footer

Powered by FluxBB