fpjsondataset - Example(s)? lazarus / 16-Databases ---------------------------------------------------------------------- PaulANormanNZ · 2018-06-10 06:10 Hi, I keep seeing web search results with tantalizing references to an announcement made by Michael Van Canneyt back in 2011 in a short thread staring at ... http://lists.lazarus.freepascal.org/pipermail/lazarus/2011- December/069306.html I located it currently in a mirror repository as -- Freepascal/packages/fcl-db/src/json/fpjsondataset.pp It is very well documented internally, but I am afraid I have no better idea of how to use it after glancing through. The fcl-db  ReadMe there, gives no further information, nor does ... http://wiki.freepascal.org/fcl-db I searched existing Lazarus Forum Messages... and this was one of the best results (from 2014).. https://forum.lazarus.freepascal.org/index.php/topic,25393.msg154226.h tml#msg154226 Quotefpdataset.pp is in ../fcl-db/src/json/ but there is no example included" (I can't find any examples in Lazarus Example Projects either.) Is that where things stand - or does any one know of any sample projects, or examples any where please? TIA Paul ---------------------------------------------------------------------- wp · 2018-06-10 11:06 In folder source/packages/fcl-db/tests of your fpc directory you find the test program testjsondataset from which you can learn the basic usage of the json dataset. And this is the essential code of a GUI program with a DBGrid, DBNavigator, and  DataSource: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TForm1 = class(TForm)    DataSource1: TDataSource;    DBGrid1: TDBGrid;    DBNavigator1: TDBNavigator;    procedure FormCreate(Sender: TObject);    procedure FormDestroy(Sender: TObject);  private    FDataset: TExtJsJsonObjectDataset;   public   end;//...procedure TForm1.FormCreate(Sender: TObject);begin  FDataset := TExtJSJsonObjectDataset.Create(self);  Datasource1.Dataset := FDataset;  FDataset.LoadFromFile('test.json');  FDataset.Open;end; procedure TForm1.FormDestroy(Sender: TObject);begin  FDataset.SaveToFile('test.json', true);end; ---------------------------------------------------------------------- PaulANormanNZ · 2018-06-10 12:28 Thank you very much WP, I'm sure this will help anyone wanting to use this approach. I would never have ever got to that "tests" directory by myself - I was relying on the Project dialogue that offers to open examples, and had turned on sub directories in there but nothing had shown up.. In your essential GUI code, is there any documentation anywhere as to what from 'test.json' is expected to take? Or perhaps even a 'test.json' anywhere to be down loaded? (A json array of simple top level json objects I'm guessing?) I've checked in the console app you've pointed me to, but there is nothing in that directory. I'll try outputting the console app's file and see if the GUI program can read it. Thanks very much again, Paul ---------------------------------------------------------------------- wp · 2018-06-10 14:27 I created the "test.json" with the following console program which is essentially the procedure DoTest3 of the testjsondataset. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program project1; {$mode objfpc}{$H+} uses  db, fpjsondataset; Procedure DumpDataset(DS : TDataset);Var  I,J : Integer;begin  I:=0;  Writeln('Dataset contains ',DS.RecordCount,' records');  While not DS.EOF do    begin    Inc(I);    Writeln('=== Record ',I,' : ',DS.RecNo,' ===');    For J:=0 to DS.Fields.Count-1 do      With DS.Fields[J] do        Writeln(FieldName,' : ',AsString);    DS.Next;    end;  Writeln('Dataset contained ',I,' records');end; Var  DS : TExtjsJSONObjectDataset;  I,J : Integer;  F : TFieldDef; const  FILE_NAME = 'test.json'; begin  DS:=TExtjsJSONObjectDataset.Create(Nil);  try    With DS do      begin      FieldDefs.Add('ID',ftLargeint,0);      FieldDefs.Add('Name',ftString,20);      FieldDefs.Add('Email',ftString,30);      Open;      // Record 1      Append;      FieldByName('ID').AsInteger:=3;      FieldByName('Name').AsString:='Michael';      FieldByName('Email').AsString:='michael@freepascal.org';      Post;    // Record 2      Append;      FieldByName('ID').AsInteger:=4;      FieldByName('Name').AsString:='jonas';      FieldByName('Email').AsString:='jonas@freepascal.org';      Post;    DumpDataset(DS);      First;      // insert record 1      Insert;    FieldByName('ID').AsInteger:=1;      FieldByName('Name').AsString:='Florian';      FieldByName('Email').AsString:='Florian@freepascal.org';      Post;    DumpDataset(DS);      Writeln('First');      First;      Writeln('Editing record ', RecNo,' ',FieldByName('Name').AsString);    Edit;      FieldByName('ID').AsInteger:=12;      FieldByName('Name').AsString:='Marco';      FieldByName('Email').AsString:='Marco@stack.nl';      Post;      First;      DumpDataset(DS);      First;      Next;      Writeln('Deleting record ', RecNo,' ',FieldByName('Name').AsString);    Delete;      First;      DumpDataset(DS);      SaveToFile(FILE_NAME, True);      end;  finally    DS.Free  end;   ReadLn;end.So, the basic idea is that common to all simple TDataset descendants: define the fields that you want to have in the dataset by calling Fieldsdefs.Add(). Open the dataset, and begin appending records using the sequence Append - populate field values - Post. Finally, when done SaveToFile. ---------------------------------------------------------------------- PaulANormanNZ · 2018-06-12 00:21 Thanks, This has absolutely tremendous potential. We're wanting this kind of thing for keeping track of human readable files describing multimedia type resources. I've integrated and attached the basic projects (uncompiled form) as a .7z for any one who needs them. Paul