#1 2019-02-24 02:53:10

wangchong
Member
Registered: 2011-08-15
Posts: 42

How to Inserialize arrays with CustomSeralize?

I registered a custom reader and writer

code:
  aSerializer.AddJSONEscape(['Key',V.Key,'RowData',v.RowData]);
but  return array proptery string is null
{"Key":"Eric","RowData":null}
rowdata is  variant type dynamic  array

Offline

#2 2019-02-24 10:47:29

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,242
Website

Re: How to Inserialize arrays with CustomSeralize?

Use a TDocVariant variant type instead.

Offline

#3 2019-02-25 00:01:20

wangchong
Member
Registered: 2011-08-15
Posts: 42

Re: How to Inserialize arrays with CustomSeralize?

thanks !
Below is my code for your reference

json:
'[{"Key":"Eric","RowData":["Eric",15]}]'

class function TSpiderDataitem.FVClassReader(const aValue: TObject;
  aFrom: PUTF8Char; var aValid: boolean;
  aOptions: TJSONToObjectOptions): PUTF8Char;
var
  V: TSpiderDataItem absolute aValue;
   Doc :TDocVariantData;
   Values:array[0..10] of TValuePUTF8Char;
begin
  result := JSonDeCode(aFrom, ['Key','RowData'], @Values[0],true);
  aValid := (result <> nil);
  if aValid then
  begin
    v.Key :=VAlues[0].ToString;
    Doc.InitJSON(Values[1].Value);
    V.FRowData :=Doc.Values;
    (V.Collection as TSpiderDataCollection).parent.AddHash(
    v.Key,V
    );
  end;


end;

class procedure TSpiderDataItem.FVClassWriter(const aSerializer:
    TJSONSerializer; aValue: TObject; aOptions: TTextWriterWriteObjectOptions);
var
  V: TSpiderDataItem absolute aValue;
  Row:TDocVariantData;
begin
  Row.InitArrayFromVariants(v.RowData);
  aSerializer.AddJSONEscape(['Key',v.Key,'RowData',Variant(Row)]);
end;

Last edited by wangchong (2019-02-27 12:02:02)

Offline

Board footer

Powered by FluxBB