#1 2019-02-18 12:55:53

nobug
Member
Registered: 2019-02-18
Posts: 2

TDocVariant: strange behavor to create JSON

While trying to create JSON using TDocVariant, I encounter a problem. The following test shows the problem.

program test;

{$APPTYPE CONSOLE}

uses
  SynCommons;


    function f2(aJO: Variant): string;
    begin
      aJO.ip2  := aJO.ip1;
      aJO.ip3  := aJO.ip1;
      result := VariantSaveJSON(aJO);
    end;

    function f1(aStr: string): string;
    var
      JO: variant;
    begin
      JO := _Json(aStr);
      JO.p := 'ss';
      JO.ip1 := '192.168.1.1';
      Result := f2(JO);
    end;

var
  Str1, Str2: string;

begin
  Str1 := '{"a":1,"b":2,"c":3,"d":4,"e":5,"f":6,"g":7,"h":8,"i":9,"j":10,"k":11,"l":12,"m":13,"n":14}';
  Writeln(f1(Str1));

  Writeln('======================================================');

  Str2 := '{"a":1,"b":2,"c":3,"d":4,"e":5,"f":6,"g":7,"h":8,"i":9,"j":10,"k":11,"l":12,"m":13,"n":14,"o":15}';
  Writeln(f1(Str2));

  readln;
end.

Run under Win32, it is ok!  Under Win64,  the result is:

{"a":1,"b":2,"c":3,"d":4,"e":5,"f":6,"g":7,"h":8,"i":9,"j":10,"k":11,"l":12,"m":13,"n":14,"p":"ss","ip1":"192.168.1.1","ip2":"192.168.1.1","ip3":"192.168.1.1"}
======================================================
{"a":1,"b":2,"c":3,"d":4,"e":5,"f":6,"g":7,"h":8,"i":9,"j":10,"k":11,"l":12,"m":13,"n":14,"o":15,"p":"ss","ip1":"192.168.1.1","ip2":null,"ip3":"192.168.1.1"}

The result of the function call:  Writeln(f1(Str2))  is not correct,  the corresponding value of "ip2" is null !
Is there something wrong in my test?

Thanks.

Offline

#2 2019-02-19 20:28:21

damiand
Member
From: Greece
Registered: 2018-09-21
Posts: 94

Re: TDocVariant: strange behavor to create JSON

I'm a newbie in mORMot, too. I've tested your code with FPC/Lazarus in Win64 with the results you mentioned. It seems to me that during the assignment to ip2 misses somehow the data type of ip1 and due to a probably wrong typecast passes null. I modified your f2 as follows and it works:

function f2(aJO: Variant): string;
begin
  aJO.ip2  := _Safe(aJO)^.U['ip1'];
  aJO.ip3  := _Safe(aJO)^.U['ip1'];
  result := VariantSaveJSON(aJO);
end;

Last edited by damiand (2019-02-19 20:36:31)

Offline

#3 2019-02-27 02:29:39

nobug
Member
Registered: 2019-02-18
Posts: 2

Re: TDocVariant: strange behavor to create JSON

The problem is solved. Thanks!

Offline

Board footer

Powered by FluxBB