Summary

TJsonSerializer is a specialized TSerializer class type for writing values in JSON format, using built-in VCL JSON classes, declared in DBXJSON.pas unit.

Syntax

TJsonSerializer = class(TSerializer)

Remarks

Unlike XML, JSON library do not provide a document notion. So, initially there are no standard container, into which serializing values can be stored. Moreover, if following JSON format strictly, it allows only single root level value. However, serialization engine is designed to support multiple root level values.

As a result TJsonSerializer has been designed to store root level values in a user provided storage. TJsonSerializer object can be created either, by specifying address and length of the buffer or a reference to callback function.

Each root-level data value will be written into provided buffer (or via callback function) as a TJSONValue object. If the buffer is too small to hold the next serializing value, exception will be raised.

All root level values stored in user provided buffer (or via callback function) should be destroyed by the user.

Examples

DelphiCopyCode imageCopy Code
var
  v:   array[0..1] of TJSONValue;
  szr: TJsonSerializer;
begin
  FillChar(v, SizeOf(v), 0);
   try
    szr := TJsonSerializer.Create(@v, 2);
    try
      srz.Value<TBook>(book1);
      srz.Value<TBook>(book2);
    finally
      szr.Free;
    end;

     Memo1.Lines.Add(v[0].ToString);
     Memo1.Lines.Add(v[1].ToString);
   finally
     v[0].Free;
     v[1].Free;
   end;
end;
   
DelphiCopyCode imageCopy Code
szr := TJsonSerializer.Create(procedure(V: TJSONValue)
        begin
          Memo1.Lines.Add(V.ToString);
        end);
try
  srz.Value<TBook>(book1);
  srz.Value<TBook>(book2);
finally
  szr.Free;
end;