Summary

TJsonDeserializer is a specialized TDeserializer class type for reading values from JSON format, using built-in VCL JSON classes, declared in DBXJSON.pas unit.

Syntax

TJsonDeserializer = class(TDeserializer)

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 TJsonDeserializer has been designed to read root level values from a user provided storage. TJsonDeserializer 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 read from provided buffer of TJSONValue objects (or via callback function). Buffer content is not modified while de-serializing, and so, all TJSONValue instances still need to be destroyed by the user eventually.

Examples

DelphiCopyCode imageCopy Code
var
  v:   array[0..1] of TJSONValue;
  dsr: TJsonDeserializer;
begin
  v[0] := TJSONObject.ParseJSONValue(Memo1.Lines[0]);
  v[1] := TJSONObject.ParseJSONValue(Memo1.Lines[1]);
  dsr := TJsonDeserializer.Create(@v, 2);
  try
    book1 := srz.Value<TBook>;
    book2 := srz.Value<TBook>;
  finally
    dsr.Free;
    v[0].Free;
    v[1].Free;
  end;
end;
   
DelphiCopyCode imageCopy Code
idx := 0;
dsr := TJsonDeserializer.Create(function: TJSONValue
       begin
         if idx < Memo1.Lines.Count then
         begin
           TJSONObject.ParseJSONValue(Memo1.Lines[idx]);
           Inc(idx);
         end
         else
           Result := nil; // No more values.
       end);
try
  book1 := dsr.Value<TBook>;
  book2 := dsr.Value<TBook>;
finally
  dsr.Free;
end;