Difference between revisions of "fcl-json"

From Free Pascal wiki
Jump to navigationJump to search
Line 14: Line 14:
  
 
== Examples ==
 
== Examples ==
Example usage can be found in the Lazarus jsonviewer tool.
+
Example usage can be found in the Lazarus jsonviewer tool (located in lazarus/tools/jsonviewer.
 +
 
 +
In particular, this part of the tool shows how to use json:
 +
 
 +
<syntaxhighlight>
 +
procedure TMainForm.OpenFile(Const AFileName : String);
 +
 
 +
Var
 +
  S : TFileStream;
 +
  P : TJSONParser;
 +
  D : TJSONData;
 +
begin
 +
  S:=TFileStream.Create(AFileName,fmOpenRead);
 +
  try
 +
    P:=TJSONParser.Create(S);
 +
    try
 +
      P.Strict:=FStrict;
 +
      D:=P.Parse;
 +
    finally
 +
      P.Free;
 +
    end;
 +
  finally
 +
    S.Free;
 +
  end;
 +
  FFileName:=AFileName;
 +
  SetCaption;
 +
  FreeAndNil(FRoot);
 +
  FRoot:=D;
 +
  ShowJSONDocument;
 +
end;
 +
 
 +
procedure TMainForm.ShowJSONDocument;
 +
 
 +
begin
 +
  With TVJSON.Items do
 +
    begin
 +
    BeginUpdate;
 +
    try
 +
      TVJSON.Items.Clear;
 +
      SHowJSONData(Nil,FRoot);
 +
      With TVJSON do
 +
        If (Items.Count>0) and Assigned(Items[0]) then
 +
          begin
 +
          Items[0].Expand(False);
 +
          Selected:=Items[0];
 +
          end;
 +
    finally
 +
      EndUpdate;
 +
    end;
 +
    end;
 +
end;
 +
 
 +
procedure TMainForm.ShowJSONData(AParent : TTreeNode; Data : TJSONData);
 +
 
 +
Var
 +
  N,N2 : TTreeNode;
 +
  I : Integer;
 +
  D : TJSONData;
 +
  C : String;
 +
  S : TStringList;
 +
 
 +
begin
 +
  N:=Nil;
 +
  if Assigned(Data) then
 +
    begin
 +
    Case Data.JSONType of
 +
      jtArray,
 +
      jtObject:
 +
        begin
 +
        If (Data.JSONType=jtArray) then
 +
          C:=SArray
 +
        else
 +
          C:=SObject;
 +
        N:=TVJSON.Items.AddChild(AParent,Format(C,[Data.Count]));
 +
        S:=TstringList.Create;
 +
        try
 +
          For I:=0 to Data.Count-1 do
 +
            If Data.JSONtype=jtArray then
 +
              S.AddObject(IntToStr(I),Data.items[i])
 +
            else
 +
              S.AddObject(TJSONObject(Data).Names[i],Data.items[i]);
 +
          If FSortObjectMembers and (Data.JSONType=jtObject) then
 +
            S.Sort;
 +
          For I:=0 to S.Count-1 do
 +
            begin
 +
            N2:=TVJSON.Items.AddChild(N,S[i]);
 +
            D:=TJSONData(S.Objects[i]);
 +
            N2.ImageIndex:=ImageTypeMap[D.JSONType];
 +
            N2.SelectedIndex:=ImageTypeMap[D.JSONType];
 +
            ShowJSONData(N2,D);
 +
            end
 +
        finally
 +
          S.Free;
 +
        end;
 +
        end;
 +
      jtNull:
 +
        N:=TVJSON.Items.AddChild(AParent,SNull);
 +
    else
 +
      N:=TVJSON.Items.AddChild(AParent,Data.AsString);
 +
    end;
 +
    If Assigned(N) then
 +
      begin
 +
      N.ImageIndex:=ImageTypeMap[Data.JSONType];
 +
      N.SelectedIndex:=ImageTypeMap[Data.JSONType];
 +
      N.Data:=Data;
 +
      end;
 +
    end;
 +
end; 
 +
</syntaxhighlight>
  
 
Also, the [[FPC Applications/Projects Gallery#FPCTwit|fpctwit]] library makes use of JSON to send/receive data.
 
Also, the [[FPC Applications/Projects Gallery#FPCTwit|fpctwit]] library makes use of JSON to send/receive data.

Revision as of 13:17, 8 July 2015

fcl-json - a JSON (Javascript Object Notation) implementation

Notes

Contains the fpjson JSON manipulation unit.

Note: In FPJSON, accessing e.g. SomeJSONObject.Integers['price'] may give a SIGSEGV/Access Violation if that integer variable does not exist. This is apparently intentional, see [1]

You'd have to use the .Find method (available since FPC 2.6.2) to first check if the element/variable (price in this example) exists.

Streaming

fcl-json contains the unit "fpjsonrtti" which is used to load objects (TObject instances) from or save them to JSON format.

See Streaming JSON for a short example.

Examples

Example usage can be found in the Lazarus jsonviewer tool (located in lazarus/tools/jsonviewer.

In particular, this part of the tool shows how to use json:

procedure TMainForm.OpenFile(Const AFileName : String);

Var
  S : TFileStream;
  P : TJSONParser;
  D : TJSONData;
begin
  S:=TFileStream.Create(AFileName,fmOpenRead);
  try
    P:=TJSONParser.Create(S);
    try
      P.Strict:=FStrict;
      D:=P.Parse;
    finally
      P.Free;
    end;
  finally
    S.Free;
  end;
  FFileName:=AFileName;
  SetCaption;
  FreeAndNil(FRoot);
  FRoot:=D;
  ShowJSONDocument;
end;

procedure TMainForm.ShowJSONDocument;

begin
  With TVJSON.Items do
    begin
    BeginUpdate;
    try
      TVJSON.Items.Clear;
      SHowJSONData(Nil,FRoot);
      With TVJSON do
        If (Items.Count>0) and Assigned(Items[0]) then
          begin
          Items[0].Expand(False);
          Selected:=Items[0];
          end;
    finally
      EndUpdate;
    end;
    end;
end;

procedure TMainForm.ShowJSONData(AParent : TTreeNode; Data : TJSONData);

Var
  N,N2 : TTreeNode;
  I : Integer;
  D : TJSONData;
  C : String;
  S : TStringList;

begin
  N:=Nil;
  if Assigned(Data) then
    begin
    Case Data.JSONType of
      jtArray,
      jtObject:
        begin
        If (Data.JSONType=jtArray) then
          C:=SArray
         else
           C:=SObject;
        N:=TVJSON.Items.AddChild(AParent,Format(C,[Data.Count]));
        S:=TstringList.Create;
        try
          For I:=0 to Data.Count-1 do
            If Data.JSONtype=jtArray then
              S.AddObject(IntToStr(I),Data.items[i])
            else
              S.AddObject(TJSONObject(Data).Names[i],Data.items[i]);
          If FSortObjectMembers and (Data.JSONType=jtObject) then
            S.Sort;
          For I:=0 to S.Count-1 do
            begin
            N2:=TVJSON.Items.AddChild(N,S[i]);
            D:=TJSONData(S.Objects[i]);
            N2.ImageIndex:=ImageTypeMap[D.JSONType];
            N2.SelectedIndex:=ImageTypeMap[D.JSONType];
            ShowJSONData(N2,D);
            end
        finally
          S.Free;
        end;
        end;
      jtNull:
        N:=TVJSON.Items.AddChild(AParent,SNull);
    else
      N:=TVJSON.Items.AddChild(AParent,Data.AsString);
    end;
    If Assigned(N) then
      begin
      N.ImageIndex:=ImageTypeMap[Data.JSONType];
      N.SelectedIndex:=ImageTypeMap[Data.JSONType];
      N.Data:=Data;
      end;
    end;
end;

Also, the fpctwit library makes use of JSON to send/receive data.

See also

An article covering use of XML and JSON in FreePascal: PDF

Package List