Popular Clientdataset with JSON truncates the data in 255 characters

Asked

Viewed 2,324 times

3

I am 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 in 255 characters. Example of JSON:

{
  "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 problem?

1 answer

3


The Class TCustomJSONDataSetAdapter identifies and creates the Field Type by default.

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

Ideal is you create the field before the conversion with a size larger than the one created in the conversion process.

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

Edit:

Instead of using the TDataSet, use a TClientDataSet because, the facility to create 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 don’t need to use the class TCustomJSONDataSetAdapter which has a pattern to follow, of course, it is possible to modify this class, but that is at the IDE level and not worth it as it would lose this progress in a next update!

  • In this case I would lose the dynamic I want with the idea, because I do not know what the name of the field that will come from REST. This way, I can’t create the field before.

  • In the same way, friend, you will have to read the Json before converting and creating the fields dynamically! Because, it is a Class standard.

  • When I do the UpdateDataSet it rebuilds Dataset. Is there any way to do it better? Even if you don’t use Tcustomjsondatasetadapter?

  • Ai would have to be "TOTALLY dynamic in the hand", that is, what I said earlier, go through the Json Object and recognize the keys, and then yes Create the Fields.

  • I tried this way (http://collabedit.com/2e6pf), but after the Update, it erases and recreates my fields. I am doing something wrong?

  • The way you tried it is valid, but the result you’ve seen, it erases. Without option, try to do what I told you before, create the fields dynamically based on JSON Keys

  • Junior, could you give me a little example of how to generate the fields dynamically, please?

  • 1

    I’m on mobile rsrsrs, get home edits Reply.

  • 1

    I implemented the solution and the result came out as I needed! Thank you! But have some special reason pro Tcustomjsondatasetadapter do not automatically change the field to ftMemo?

  • 1

    Does not have, it prefers ftWidString by default!

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.