JSON persistence
Key takeaways
JsonFilePersister | Component in the data module, persistence library used to persist data in JSON format. |
configure | Method used to pass the path to a JSON file. |
save | Method used to save data to a JSON file. |
load | Method used to extract data from a JSON file. |
Introduction
This tutorial will help you understand how to create a JSON persistence component. It starts by explaining the pre-requisites. Then it continues with an explanation on how to create a JSON persistence object, save data to it, and extract stored data from it. In the end, it provides an example where all the explained methods are included.
Persisting data in a JSON file
Pre-requisites
In order to create a JSON persistence component, we need to import the JsonFilePersister class. This class belongs to the persistence library in the data module. To include this class use the following command:
import (
persist "github.com/pip-services4/pip-services4-go/pip-services4-persistence-go/persistence"
)
from pip_services4_persistence.persistence import JsonFilePersister
Creating the JSON persistence component
There are two ways to create a JSON persistence component. The first consists of declaring an instance of the JsonFilePersister class and passing the JSON file’s path to the constructor. Our code will be as follows:
import (
persist "github.com/pip-services4/pip-services4-go/pip-services4-persistence-go/persistence"
)
persister := persist.NewJsonFilePersister[any]("./data.json")
from pip_services4_persistence.persistence import JsonFilePersister
persister = JsonFilePersister("data.json")
The second uses a config object to define the JSON file path. In this case, we need to define a ConfigParams object with the path and use the configure method to pass this object to the JSON persistence object.
import (
"context"
config "github.com/pip-services4/pip-services4-go/pip-services4-components-go/config"
persist "github.com/pip-services4/pip-services4-go/pip-services4-persistence-go/persistence"
)
persister := persist.NewJsonFilePersister[any]("")
myConfig := config.NewConfigParamsFromTuples("path", "./data.json")
persister.Configure(context.Background(), myConfig)
from pip_services4_persistence.persistence import JsonFilePersister
from pip_services4_components.config import ConfigParams
persister = JsonFilePersister()
my_config = ConfigParams.from_tuples("path", "data.json")
persister.configure(my_config)
Saving data on a JSON file
To store data in a JSON file we use the save method. The first parameter is the trace_id, which is used to trace execution through the call chain. The second one is the JSON value we want to save. The following example shows how to use it.
persister.Save(context.Background(), []interface{}{"A1", "B1", "C1"})
persister.save(None, ["A1", "B1", "C1"])
Loading data from a JSON file
To load data from a JSON file we use the load method, which returns the loaded items. This method has the trace_id parameter as input. The code below explains its usage.
items, _ := persister.Load(context.Background())
items = persister.load(None)
Example
In the following example, we put everything together. First, we create a JSON persistence object, then we save some data to the JSON file, and lastly, we extract the stored data from the JSON file.
import (
"context"
"fmt"
persist "github.com/pip-services4/pip-services4-go/pip-services4-persistence-go/persistence"
)
// Create the JSON persistence component
persister := persist.NewJsonFilePersister[any]("./data.json")
// Save data on the JSON persistence object
persister.Save(context.Background(), []interface{}{"A1", "B1", "C1"})
// Read data from the JSON persistence object
items, _ := persister.Load(context.Background())
fmt.Print(items)
from pip_services4_persistence.persistence import JsonFilePersister
# Create the JSON persistence component
persister = JsonFilePersister("data.json")
# Save data on the JSON persistence object
persister.save("123", ["A", "B", "C"])
# Read data from the JSON persistence object
items = persister.load("123")
print(items)
After running it, our output will be:
And, our data.json file will contain:
Wrapping up
In this tutorial, we have learned how to persist data and how to extract persisted data from a JSON file. In the end, we saw an example that summarizes all JSON persistence operations.