Read and write objects to SQLite database lazarus / 16-Databases ---------------------------------------------------------------------- Gritty · 2024-05-17 19:34 A few months ago I started using free pascal for cgi scripts on the Gemini protocol. I am making an app but have a need to read/write objects, such as TDictionary to/from sqlite. I can get TStringList to export csv, but more complex objects don't seem obvious. I've searched the wiki, looked for methods and units, and searched here in the forum but couldn't find anything applicable. In python the answer was JSON, but I don't see a similar function to load/dump objects to/from JSON in Free Pascal. Thanks for any help. ---------------------------------------------------------------------- cdbc · 2024-05-17 20:03 Hi You need to include e.g.: 'fpjson' amo, in your uses clause... There are lots of json-related stuff in there. Path = "...fpcsrc/packages/fcl-json/src/" excerpt from readme.txt:QuoteThis package implements JSON support for FPC. You might want to have a look at the lazarus jsonviewer tool, written using fpJSON (see lazarus/tools/jsonviewer). It visualizes the fpJSON data and shows how to program using fpjson. JSON support consists of 3 parts: unit fpJSON contains the data representation. Basically, it defines a set of classes: TJSONData +- TJSONNumber    +- TJSONIntegerNumber    +- TJSONFloatNumber    +- TJSONInt64Number +- TJSONString +- TJSONBoolean +- TJSONNull +- TJSONObject +- TJSONArray Regards Benny ---------------------------------------------------------------------- paweld · 2024-05-18 06:27 You can use RTTI to do this. FPC has a unit fpjsonrtti that works well with objects and collections. Currently, only published properties are supported. An example is attached. ---------------------------------------------------------------------- Thaddy · 2024-05-18 06:50 Another solution, provided your objects are derived from TPersistent, which is compiled in $m+ state, is simply to use the default object streaming mechanism, which is a built-in (Read/WriteComponentResFile). I have been using that for 10's of years, actually since early Delphi. everybody seems to forget that mechanism. But Benny is right: if you require JSON you can also do that in FreePascal, there are multiple good json libraries for fpc, fpjson is the default and good. Paweld option is similar to the above, but json format. ---------------------------------------------------------------------- Gritty · 2024-05-18 20:35 Thank you for the quick responses and example.  I will check out fpjsonrtti. ---------------------------------------------------------------------- Gritty · 2024-05-22 17:04 I tried it out and I was able to use the example code, and it works.  thanks! However, I have a question: Can TDictionary not be serialized?  Everytime I add items and serialize I get empty curly braces {} with no items inside. If it's not an easy answer, I will post my code. ---------------------------------------------------------------------- paweld · 2024-05-23 11:05 In this case, you have to write the function that converts to/from JSON yourself. Please find attached a project extended with such an example. ---------------------------------------------------------------------- Gritty · 2024-05-23 15:42 @paweld - thanks!  I see what you mean, having to create your own JSON objects and arrays and populating them yourself.  I thought maybe there was a streamlined way for generics but I guess there aren't. Much thanks.