viniciussanchez / dataset-serialize

JSON to DataSet and DataSet to JSON converter for Delphi and Lazarus (FPC)
MIT License
653 stars 161 forks source link

Error when loading a JSON to a DataSet #189

Closed sbeu closed 1 year ago

sbeu commented 1 year ago

Hello, I get an error when I run the line ADataSet.LoadFromJSON I am not sure it is an error due to your (wonderful) library, but still I don't understand why it happens. Here is the code:

Procedure PrepareDataSet(JsonContent: String);
Var
AField: TField;
ADataSet: TFDMemTable;
begin
  ADataSet := TFDMemTable.Create(nil);
  AField := TIntegerField.Create(ADataSet);
  AField.Name := '';
  AField.FieldName := 'BILL_ID';
  AField.DataSet := ADataSet;
  ADataSet.Active := True;
  ADataSet.LoadFromJSON(JsonContent); // ERROR happens here: Error:Access violation at address 000000000096FCA4 in module 'FunctionExportContactsForSamWin.exe'. Read of address 0000000000000000. 
  ADataSet.Free;
end;

When I look the exact line where the error happens, it is here, the file Data.DB:

procedure TStringField.SetAsAnsiString(const Value: AnsiString);
var
  Len: Integer;
begin
  Len := Integer(Length(PAnsiChar(Value)) + 1);
  if DataSize < Len then
    Len := DataSize;
  Move(PAnsiChar(Value)^, PAnsiChar(@FIOBuffer[0])^, Len - 1);
  FIOBuffer[Len - 1] := 0;
  if Transliterate then
    DataSet.Translate(PAnsiChar(@FIOBuffer[0]), PAnsiChar(@FIOBuffer[0]), True);
  SetData(FIOBuffer);
end;

The variable DataSet is evaluated to nil

Please note that when I execute ADataSet.LoadFromJSON(JsonContent); without defining the field types, the import is correct. But I need the fields to be typed.

Thank you

sbeu commented 1 year ago

Sorry for this issue, the problem was due to a field name in my dataset not matching the field name in the JSON.