#1 2019-01-29 13:55:21

teleport.soccer
Member
Registered: 2019-01-29
Posts: 6

DynArrayLoadJSON problem

hi,
I'm trying to parse json with "DynArrayLoadJSON", but it seems not working correctly!

http://127.0.0.1:60401/myroot/TestService/Test1?TestRequest=[{"A":1,"B":2},{"A":3,"B":4}]
http://127.0.0.1:60401/myroot/TestService/Test2?TestRequest=[{"A":1,"B":2},{"A":3,"B":4}]
TTiTestRecord = packed record
    A: Integer;
    B: Integer;
    //C: RawUTF8; // <-- with this line, it's ok ???
  end;
  TTiTestRecordDynArray = array of TTiTestRecord;

ITestService = interface(IInvokable)
    ['{FA5628E6-1929-4382-B1D9-860CE4E67A6D}']
    function Test1(const TestRequest: TTiTestRecordDynArray): TServiceCustomAnswer;
    function Test2(TestRequest: RawUTF8): TServiceCustomAnswer;
  end;

....

function TTestService.Test1(const TestRequest: TTiTestRecordDynArray): TServiceCustomAnswer;
var 
  str: RawUTF8;
begin
  Result.Header := TEXT_CONTENT_TYPE_HEADER;
  Result.Status := HTTP_SERVERERROR;
  try
    str := RecordSaveJSON(TestRequest, TypeInfo(TTiTestRecordDynArray));
    Result.Status := HTTP_SUCCESS;
    Result.Content := RawByteString(str);
  except on E: Exception do
    Result.Content := RawByteString(E.Message);
  end;
end;

function TTestService.Test2(TestRequest: RawUTF8): TServiceCustomAnswer;
var 
  ar: TTiTestRecordDynArray;
begin
  Result.Header := TEXT_CONTENT_TYPE_HEADER;
  Result.Status := HTTP_SERVERERROR;
  try
    if (DynArrayLoadJSON(ar, PUTF8Char(TestRequest), TypeInfo(TTiTestRecordDynArray)) <> nil) then begin
      Result.Status := HTTP_SUCCESS;
      Result.Header := JSON_CONTENT_TYPE;
      Result.Content := RecordSaveJSON(ar, TypeInfo(TTiTestRecordDynArray));
    end;
  except on E: Exception do
    Result.Content := RawByteString(E.Message);
  end;
end;

if in record RawUTF8-field exists then is everything fine, why?

Offline

#2 2019-01-29 14:18:13

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

Re: DynArrayLoadJSON problem

RecordSaveJSON() is for a record, and TestRequest: TTiTestRecordDynArray is a dynamic array.

You need to use DynArraySaveJson() !!!!

Offline

#3 2019-01-29 14:25:32

teleport.soccer
Member
Registered: 2019-01-29
Posts: 6

Re: DynArrayLoadJSON problem

for Test2 my problem is on line:

if (DynArrayLoadJSON(ar, PUTF8Char(TestRequest), TypeInfo(TTiTestRecordDynArray)) <> nil) then begin

result DynArrayLoadJSON is always nil!!!

and for Test1 browser return:

{
"errorCode":406,
"errorText":"sicShared execution failed (probably due to bad input parameters) for TestService.Test1"
}

Offline

#4 2019-01-30 07:53:59

teleport.soccer
Member
Registered: 2019-01-29
Posts: 6

Re: DynArrayLoadJSON problem

what's wrong with this code?

program SynopseTest;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,
  mORMot,
  SynCommons;

type
  TTestRecord = packed record
    AAA: integer;
    BBB: integer;
  end;
  TTestDynArray = array of TTestRecord;


procedure Test1;
const
  __TTestRecord = 'AAA: integer; BBB: integer';
var
  array1: TTestDynArray;
  str: RawUTF8;
begin
  TTextWriter.RegisterCustomJSONSerializerFromText(
    TypeInfo(TTestRecord),__TTestRecord); //.Options := [soReadIgnoreUnknownFields,soWriteHumanReadable];
  str := '[{"AAA":1,"BBB":2},{"AAA":3,"BBB":4}]';
  if (DynArrayLoadJSON(array1, @str[1], TypeInfo(TTestDynArray)) <> nil) then begin
    Writeln('ok');
  end else Writeln('not ok');
end;

procedure Test2;
var
  array1, array2: TTestDynArray;
  str: RawUTF8;
begin
  SetLength(array1, 2);
  array1[0].AAA := 1;
  array1[0].BBB := 2;
  array1[1].AAA := 3;
  array1[1].BBB := 4;
  str := DynArraySaveJSON(array1,TypeInfo(TTestDynArray));
  Writeln(str); // [8589934593,17179869187]   - why?
  if (str = '[{"AAA":1,"BBB":2},{"AAA":3,"BBB":4}]') then begin
    Writeln('ok');
  end else Writeln('not ok');
end;

begin
  try
    Test1;
    Test2;
    ConsoleWaitForEnterKey;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

output:

not ok
[8589934593,17179869187]
not ok

Offline

#5 2019-01-30 17:39:47

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

Re: DynArrayLoadJSON problem

TTestRecord has no RTTI, so you should better write for instance:

  TTextWriter.RegisterCustomJSONSerializerFromText(
    TypeInfo(TTestRecordDynArray),__TTestRecord); 

Offline

#6 2019-01-31 10:53:41

teleport.soccer
Member
Registered: 2019-01-29
Posts: 6

Re: DynArrayLoadJSON problem

thx,
now is result of DynArrayLoadJSON not nil!

Offline

Board footer

Powered by FluxBB