#1 2016-09-26 17:46:23

EMartin
Member
From: Buenos Aires - Argentina
Registered: 2013-01-09
Posts: 332

TDocVariantData.InitJSON with added element array: Bug or design ?

Hi @ab, when I add an item in position 0 (zero) in a TDocVariantData JSON array (code below) the data and count is not updated but if I assign the Count property with SetCount all works fine, is by design this behavior ?

This code reproduce the issue (check the Set Count checkbox for display right data):

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    btnAddElem: TButton;
    chkSetCount: TCheckBox;
    procedure btnAddElemClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses
  SynCommons;

{$R *.dfm}

procedure TForm1.btnAddElemClick(Sender: TObject);
const
  TEST_DATA = '[{"campotexto":"texto","camponum":123},{"campotexto":"texto1","camponum":456}]';
var
  lJSONArray: TDocVariantData;
  lValues: RawUTF8;
  lDA: TDynArray;
  lValuesArray: TRawUTF8DynArray;
  lElem: RawUTF8;
begin
  lJSONArray.InitJSON(TEST_DATA, [dvoJSONObjectParseWithinString]);
  lJSONArray.ToRawUTF8DynArray(lValuesArray);
  lDA.Init(TypeInfo(TRawUTF8DynArray), lValuesArray);
  lElem := '{"campotexto":"texto2","camponum":789}';
  lDA.Insert(0, lElem);
  lValues := lDA.SaveToJSON;
  lJSONArray.Clear;
  lJSONArray.InitJSON(lValues, [dvoJSONObjectParseWithinString]);
  if chkSetCount.Checked then
    lJSONArray.SetCount(lDA.Count); // if assigned, the new element is displayed and the Count is right
  ShowMessage(lJSONArray.ToJSON('', '', jsonHumanReadable) + sLineBreak + 'Count='+IntToStr(lJSONArray.Count));
end;

end.

Thanks.

Esteban Martin


Esteban

Offline

Board footer

Powered by FluxBB