// Fast stream json writing
unit djsonwriter;

interface

uses
  dstream;

type
PJsonWriter = ^TJsonWriter;
TJsonWriter = object
private
  FData: TData;
  FIndent: LongInt;
  FSpaces: LongInt;
  FLineEnding: AnsiString;
public
  constructor Init(Data: TData; Indent: LongInt = 0; const LE: AnsiString = LineEnding);
  destructor Done; virtual;
  procedure BeginObject;
  procedure EndObject;
  procedure BeginArray;
  procedure EndArray;
  function AddString(const Value: AnsiString): PJsonWriter;
  function AddInt(Value: LongInt): PJsonWriter;
  function AddFloat(F: Single): PJsonWriter;
  function AddDouble(D: Double): PJsonWriter;
  function AddBoolean(B: Boolean): PJsonWriter;
  function AddNull: PJsonWriter;
  // inside object
  function Field(const Name: AnsiString): PJsonWriter;
end;

implementation

constructor TJsonWriter.Init(Data: TData; Indent: LongInt = 0; const LE: AnsiString = LineEnding);
begin
  FData := Data;
  FIndent := Ident;
  FSpaces := 0;
  if FIndent <> 0 then
    FLineEnding := LE;
end;

destructor TJsonWriter.Done;
begin
  FData.Done;
end;

procedure TJsonWriter.BeginObject;
begin
  FStream.Write('{' + FLineEnding);
end;

procedure TJsonWriter.EndObject;
begin
  FStream.Write(Space(FSpaces) + '}');
end;

procedure TJsonWriter.BeginArray;
begin
end;

procedure TJsonWriter.EndArray;
begin
end;

function TJsonWriter.AddString(const Value: AnsiString): PJsonWriter;
begin
end;

function TJsonWriter.AddInt(Value: LongInt): PJsonWriter;
begin
end;

function TJsonWriter.AddFloat(F: Single): PJsonWriter;
begin
end;

function TJsonWriter.AddDouble(D: Double): PJsonWriter;
begin
end;

function TJsonWriter.AddBoolean(B: Boolean): PJsonWriter;
begin
end;

function TJsonWriter.AddNull: PJsonWriter;
begin
end;

function TJsonWriter.Field(const Name: AnsiString): PJsonWriter;
begin
end;

end.
