You are not logged in.
Pages: 1
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
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
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
thx,
now is result of DynArrayLoadJSON not nil!
Offline
Pages: 1