Popular ClientDataSet with JSON truncates data in 255 characters

2

I'm trying to pass a JSON to a TClientDataSet using the following function:

procedure JsonToDataset(aDataset : TDataSet; aJSON : string);
var
  JObj: TJSONObject;
  JArr: TJSONArray;
  vConv : TCustomJSONDataSetAdapter;
  i: Integer;
begin
  if (aJSON = EmptyStr) then
  begin
    Exit;
  end;

  JArr := nil;
  JObj := nil;

  try
    JArr := TJSONObject.ParseJSONValue(aJSON) as TJSONArray;
  except
    JObj := TJSONObject.ParseJSONValue(aJSON) as TJSONObject;
  end;

  vConv := TCustomJSONDataSetAdapter.Create(Nil);

  try
    vConv.Dataset := aDataset;

    if JObj <> nil then
      vConv.UpdateDataSet(JObj)
    else
      vConv.UpdateDataSet(JArr);

  finally
    vConv.Free;
    JObj.Free;
  end;
end;

However, when I have a large field, the function truncates my string to 255 characters. JSON Example:

{
  "string": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur rhoncus convallis risus, nec posuere nisl gravida vitae. Duis elementum augue nec condimentum rutrum. Aliquam sodales, dolor at laoreet pharetra, tortor eros efficitur eros, vel euismod sem nulla quis erat. Aliquam erat volutpat. Ut vitae congue lectus, et sodales velit. Cras suscipit pulvinar dolor ut consequat. Praesent eget pellentesque justo. In at maximus lectus, posuere mattis felis."
}

Is there any way around this?

    
asked by anonymous 21.03.2018 / 16:01

1 answer

2

Class TCustomJSONDataSetAdapter identifies and creates by default the Type of fields.

In the case of content string , a field of type WideStrig of 255 characters will be created by default.

The ideal is for you to create the field before the conversion with a size larger than the one created in the conversion process.

Use Blob if you do not know the size that will be sent.

Edit:

Instead of using TDataSet , use TClientDataSet because the ease of creating the fields will be much greater:

var
  i    : Integer;
  JObj : TJSONObject;
begin
  if (aJSON <> EmptyStr) then
  begin
    JObj := TJSONObject.ParseJSONValue(aJSON) as TJSONObject;

    aDataset.Close;
    aDataset.FieldDefs.Clear; //Limpa os Campos Existentes

    for i := 0 to Pred(JObj.Size) do
    begin
      //Chave: JObj.Get(i).JsonString.Value
      if (Length(JObj.Get(i).JsonValue.Value) > 250) then
      begin
        aDataset.FieldDefs.Add(JObj.Get(i).JsonString.Value, ftBlob);
      end
      else
      begin
        aDataset.FieldDefs.Add(JObj.Get(i).JsonString.Value, ftString, 255);
      end;
    end;

    aDataset.CreateDataSet;

    for i := 0 to Pred(JObj.Size) do
    begin
      //Valor: JObj.Get(i).JsonValue.Value
      aDataset.Insert;
      aDataset.FieldByName(JObj.Get(i).JsonString.Value).AsString := JObj.Get(i).JsonValue.Value;
      aDataset.Post;
    end;

    JObj.Free;
  end;

This is an alternative so you do not have to use the TCustomJSONDataSetAdapter class that has a pattern to follow, of course, you can modify this class, but that is IDE level and not worth it, progress on an upcoming update!

    
21.03.2018 / 18:40