FireDAC: Loading JSON data with LoadFromFile
Loading JSON data into datasets is pretty straightforward using LoadFromFile or LoadFromStream. FireDAC provides these methods as part of its TFDDataset descendants. I have published several videos showing how easy it is to store generate JSON data with FireDAC and consuming it in a client application be this client application a web application or a VCL desktop application. However, I have also created some confusion that I want to clear up writing this detailed blog post about the implications.
As said, FireDAC provides saving and loading of its datasets in JSON. However, with the latest release of FireDAC the TFDBatchMove components have also been improved to offer JSON support to write JSON content. RAD Server makes heavy use of this technology as datasets and query results are serialized using this component.
There is one big difference between the two approaches, however. The JSON generated by the BatchMove components only contains the data. There is never any meta-information about the underlying dataset. The methods included in TFDDataset, on the other hand, provide multiple options for the generation of the JSON data. For example, the TFDConnection component has a property named ResourceOptions with StoreItems:
Using these properties, you can pick the content to be included in your JSON. Further, you may generate human-readable JSON by setting StorePrettyPrint to true.
In order to use LoadFromFile or LoadFromStream with JSON data it has to be created using SaveToFile or SaveToStream from a TFDDataset descendant class, e.g. TFDMemTable, TFDQuery. You cannnot load JSON content that has been generated by the TFDBatchMove components this way. There is simply no information for FireDAC how to create the dataset. To be more precise, the JSON generated by TFDDataset is very specific for FireDAC.
For that exact reason, I also showed TFDBatchMoveJSONWriter as an option if you do not want to use any FireDac-specific datasets on the client side. One example is TMS Web Core that does not use FireDAC-specific datasets. It could parse JSON content generated by TFDDataset, but it is much easier to consume content generated without any FireDAC-specific information. For that purpose, TFDBatchMoveJSONWriter is the ideal solution.
Both approaches have been shown in my videos about XData and SQL. Be aware to generate the JSON content with your client side in mind. When building a public web service you should always offer the non-FireDAC-specific solution. Otherwise, only Delphi/C++ developers will be able to have an additional benefit, but other developers will have a hard time finding the actual data in all the information provided.