uses
  dansiview,
  dstream,
  dstreamfile,
  djsonserializer;

const
  FILENAME = 'serialized.json';

var
  Serializer: TJsonSerializer;
  Deserializer: TJsonDeserializer;
  Struct: record
    A: Int32;
    B: Int32;
    C: Int32;
  end;

begin
  Struct.A := 123;
  Struct.B := 0;
  Struct.C := 516;
  Serializer.Init(Data(TStreamFile.Create(FILENAME, FM_WRITE), True));
  Serializer.Attr('a', Struct.A, 0);
  Serializer.Attr('b', Struct.B, 0);
  Serializer.Attr('c', Struct.C, 0);
  if not Serializer.Success then begin
    Writeln(stderr, 'ERROR: ', Serializer.ErrMsg, ' ', FILENAME);
  end;
  Serializer.Done;

  Struct.A := 0;
  Struct.B := 0;
  Struct.C := 0;

  Deserializer.Init(FILENAME);
  while Deserializer.Next do begin
    Writeln(Deserializer.Name.ToString);
    if Deserializer.Name = AnsiView('a', 1) then begin
      Deserializer.Attr(Struct.A, 0);
    end else if Deserializer.Name = AnsiView('b', 1) then begin
      Deserializer.Attr(Struct.B, 0);
    end else if Deserializer.Name = AnsiView('c', 1) then begin
      Deserializer.Attr(Struct.C, 0);
    end;
  end;
  if Deserializer.Failed then begin
    Writeln(stderr, 'ERROR: ', Deserializer.ErrMsg, ' ', FILENAME);
  end else begin
    Writeln('a = ', Struct.A);
    Writeln('b = ', Struct.B);
    Writeln('c = ', Struct.C);
  end;
  Deserializer.Done;
end.
