Recent

Author Topic: fpjsondataset - Example(s)?  (Read 3818 times)

PaulANormanNZ

  • Full Member
  • ***
  • Posts: 115
fpjsondataset - Example(s)?
« on: June 10, 2018, 08:10:21 am »
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.html#msg154226

Quote
fpdataset.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

  • Hero Member
  • *****
  • Posts: 11853
Re: fpjsondataset - Example(s)?
« Reply #1 on: June 10, 2018, 01:06:35 pm »
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][+][-]
  1. type
  2.   TForm1 = class(TForm)
  3.     DataSource1: TDataSource;
  4.     DBGrid1: TDBGrid;
  5.     DBNavigator1: TDBNavigator;
  6.     procedure FormCreate(Sender: TObject);
  7.     procedure FormDestroy(Sender: TObject);
  8.   private
  9.     FDataset: TExtJsJsonObjectDataset;
  10.  
  11.   public
  12.  
  13.   end;
  14. //...
  15. procedure TForm1.FormCreate(Sender: TObject);
  16. begin
  17.   FDataset := TExtJSJsonObjectDataset.Create(self);
  18.   Datasource1.Dataset := FDataset;
  19.   FDataset.LoadFromFile('test.json');
  20.   FDataset.Open;
  21. end;
  22.  
  23. procedure TForm1.FormDestroy(Sender: TObject);
  24. begin
  25.   FDataset.SaveToFile('test.json', true);
  26. end;  
« Last Edit: June 10, 2018, 01:11:19 pm by wp »

PaulANormanNZ

  • Full Member
  • ***
  • Posts: 115
Re: fpjsondataset - Example(s)?
« Reply #2 on: June 10, 2018, 02:28:54 pm »
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

  • Hero Member
  • *****
  • Posts: 11853
Re: fpjsondataset - Example(s)?
« Reply #3 on: June 10, 2018, 04:27:28 pm »
I created the "test.json" with the following console program which is essentially the procedure DoTest3 of the testjsondataset.

Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   db, fpjsondataset;
  7.  
  8. Procedure DumpDataset(DS : TDataset);
  9. Var
  10.   I,J : Integer;
  11. begin
  12.   I:=0;
  13.   Writeln('Dataset contains ',DS.RecordCount,' records');
  14.   While not DS.EOF do
  15.     begin
  16.     Inc(I);
  17.     Writeln('=== Record ',I,' : ',DS.RecNo,' ===');
  18.     For J:=0 to DS.Fields.Count-1 do
  19.       With DS.Fields[J] do
  20.         Writeln(FieldName,' : ',AsString);
  21.     DS.Next;
  22.     end;
  23.   Writeln('Dataset contained ',I,' records');
  24. end;
  25.  
  26. Var
  27.   DS : TExtjsJSONObjectDataset;
  28.   I,J : Integer;
  29.   F : TFieldDef;
  30.  
  31. const
  32.   FILE_NAME = 'test.json';
  33.  
  34. begin
  35.   DS:=TExtjsJSONObjectDataset.Create(Nil);
  36.   try
  37.     With DS do
  38.       begin
  39.       FieldDefs.Add('ID',ftLargeint,0);
  40.       FieldDefs.Add('Name',ftString,20);
  41.       FieldDefs.Add('Email',ftString,30);
  42.       Open;
  43.       // Record 1
  44.       Append;
  45.       FieldByName('ID').AsInteger:=3;
  46.       FieldByName('Name').AsString:='Michael';
  47.       FieldByName('Email').AsString:='michael@freepascal.org';
  48.       Post;
  49.       // Record 2
  50.       Append;
  51.       FieldByName('ID').AsInteger:=4;
  52.       FieldByName('Name').AsString:='jonas';
  53.       FieldByName('Email').AsString:='jonas@freepascal.org';
  54.       Post;
  55.       DumpDataset(DS);
  56.       First;
  57.       // insert record 1
  58.       Insert;
  59.       FieldByName('ID').AsInteger:=1;
  60.       FieldByName('Name').AsString:='Florian';
  61.       FieldByName('Email').AsString:='Florian@freepascal.org';
  62.       Post;
  63.       DumpDataset(DS);
  64.       Writeln('First');
  65.       First;
  66.       Writeln('Editing record ', RecNo,' ',FieldByName('Name').AsString);
  67.       Edit;
  68.       FieldByName('ID').AsInteger:=12;
  69.       FieldByName('Name').AsString:='Marco';
  70.       FieldByName('Email').AsString:='Marco@stack.nl';
  71.       Post;
  72.       First;
  73.       DumpDataset(DS);
  74.       First;
  75.       Next;
  76.       Writeln('Deleting record ', RecNo,' ',FieldByName('Name').AsString);
  77.       Delete;
  78.       First;
  79.       DumpDataset(DS);
  80.       SaveToFile(FILE_NAME, True);
  81.       end;
  82.   finally
  83.     DS.Free
  84.   end;
  85.  
  86.   ReadLn;
  87. 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

  • Full Member
  • ***
  • Posts: 115
Re: fpjsondataset - Example(s)?
« Reply #4 on: June 12, 2018, 02:21:38 am »
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

 

TinyPortal © 2005-2018