Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
CodeDelphiTech Partner

This Is How To Store Cross Platform App Settings In JSON

This Is How To Store Cross Platform App Settings In JSON
Storing cross platform application settings in JSON

Several older approaches to storing data related to program settings have been replaced by newer ones since the introduction of Delphi as the language kept pace with emerging modern ideas and practices. In general, examples of saving settings always followed the principles of the host operating system. This article looks into the various ways to store the settings for a cross platform app in the simple, universally accepted JSON format.

Is saving settings different in a cross platform app?

Initially, INI files were widely used, then the use of a register was rather popular and then it became common to store data in files again. But due to some challenges, it was decided to use XML files instead of INI and then they were replaced with JSON.

Let’s have a look at the issues that a developer can face during the realization of settings storage. The methods that we will analyze in this article are not the only ones that are correct. But they have been already applied for several projects and have proved their efficiency in practice.

Looking ahead, we should note that the key peculiarity and the most pleasant part of the method is its automated serialization and deserialization of JSON objects in records and Delphi classes.

Delphi developers are spoiled for choice when it comes to using JSON

Modern Delphi versions offer two libraries for working with JSON. In this article, we will analyze the most widely used alternative open-source variant – X-SuperObject provided by a Turkish developer Onur YILDIZ 

https://github.com/onryldz/x-superobject

Let’s start. In the folder with our original program texts, let’s create a folder Subrepos and execute there

git clone https://github.com/onryldz/x-superobject.git 

We will see a folder x-superobject. Do not forget to add it to the Project — Options — Delphi Compiler — Search path “./subrepos/x-superobject”.

For storing the MyProg program settings, let’s create a module MP.Settings and a class inside it.

We have to choose where we will store a settings file. There can be a lot of variants, we will consider them later.

How do we store program settings in a cross platform way?

Now let’s make a simple decision and return to it later to make refactoring.

We are developing software for Windows and a commonly used platform today is Windows 64-bit. The development process is going on in an unprotected folder. That’s why we will store a settings file directly close to an exe file.

To ensure minimum refactoring, let’s introduce special methods for the class

Now let’s turn to the main form of the app.

Simple main form to display and change some values stored in the settings file

How to add fields to the settings class?

Ok, let’s add fields to the settings class

A constructor to indicate values of settings by default

And the main content – methods of saving/loading data from a file.
Pay attention to the following moments:

By default, the settings know in what file they are placed, a file name is an optional thing

serialization/deserialization take place in one line with helpers from XSuperObject

How to load cross platform app settings from a file?

In the main form, the TMainForm.LoadSettings method will be responsible for settings loading. If a file doesn’t exist, it will be created, and the default settings will be used.

Such methods as LoadFromSettings and SaveToSettings are responsible for transferring settings to the interface elements and vice versa.

How do we actually use the LoadSettings method in our cross platform app?

Now we need to call the LoadSettings method in the constructor of the main form and start the first variant of our program.

Form displays values read from settings

As we can see, the default values were applied, the interface is filled in according to the values from Settings.

Let’s have a look at the settings file. It is placed near the exe file and is named init.json.

If you call SaveToSettings() after changing every component and save Settings.SaveToFile()  settings every time you close the program, the file content will change in accordance with your actions. 

Is there a real cross platform app example of loading and saving settings?

Let’s make our project a little bit closer to reality.

For opening a program, a user will have to enter a username and a password. Moreover, to make the interface more user-friendly, we will save the recent username.

Let’s create the simplest login form.

Login form

To the constructor of the main form, we will add a call for the following method

Pay attention to the fact that we are talking about a desktop platform where we have modal forms. For mobile apps, a login form will be supported in a slightly different way. But now we have another topic for consideration.

How do we create a record to store our cross platform app settings?

Let’s start a program and make sure that everything is working correctly. In the LoginCorrect method, we will write the right username and password and later it will be possible to conduct real testing.

Now let’s come back to the settings. In order to make our example more complicated, we won’t just create a new field in the settings but make it of a more complicated type, a record, and will store there a user’s choice on whether we need to show the recent login.

Adding interface loading and saving methods to a login form

Let’s add interface loading and saving methods to the login form.

And add method calls into a constructor and destructor, respectively.

We won’t need to change anything in the settings storage and saving and loading methods. Everything is working. Let’s check. Here you can see the saved settings file.

Here’s what we will see during the repeated loading.

Lets ensure that stored value is used

The program is working.

What are the differences between a Windows app and a cross platform app?

At the start of realization, we’ve decided to store our settings file near the exe file. If our program is installed via an installer, it is highly possible that it will be placed somewhere inside the Project Files folder.

Access to the record of data there is prohibited by default, that’s why it is not a good idea to store there a settings file.

For these aims, we have the following folder C:Users<username>AppDataRoaming

It will be offered by the System.IOUtils.TPath.GetHomePath method.

Let’s change the TMyProgSettings.GetSettingsFolder method:

Now on all platforms, the file will be saved correctly. On iOS and Android – in its own app data storage that is protected from other programs, on Windows – in the user’s data folder. On MacOS – in the user catalog /Users/<username>  but it can be not very convenient that’s why it is possible to offer an alternative variant – for example, in the folder Library. On Windows, GetLibraryPath points to the app folder.

As at the first start of the program, we didn’t have this folder, it is necessary to add to the first settings saving the call of

ForceDirectories(Settings.GetSettingsFolder());

Here is a summary of what we learned in this article about saving settings to JSON

Pluses of this method

The described method allows adding data of a complex structure that are saved between the program starts without additional programming. Of course, it is necessary to write code where the data is used. But the code is simple and it is easy to support it.

Changes in a class structure automatically lead to changes in a file structure. 

It is important that if some fields are not presented in a file or, vice versa, there are some extra fields, it doesn’t lead to errors in processing.

The JSON format is easy to read and it is simple to introduce changes to a saved file at the stage of debugging.

Minuses of the method

JSON is not intended for storing huge data arrays

It is not comfortable to store binary data, for example, uploaded images

On desktop platforms, a settings file is not protected from unauthorized viewing and updating

In general, all the mentioned problems have a solution that’s why the method considered in this article can be a very good choice.

You can download the source code for this article from the Softacom GitHub site here: https://github.com/SoftacomCompany/FMX.Settings-in-JSON


This article was written by Embarcadero Tech Partner Softacom. Softacom specialize in all sorts of software development focused on Delphi. Read more about their services on the Softacom website.

See What's New in 12.2 Athens See What's New in 12.2 Athens Dev Days of Summer 2-24

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

3 Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES