Implements: IConfigurable, ILoader
Description
The JsonFilePersister class allows you to create persistence components that load and save data from/to a flat file
Configuration parameters
- path: path to the file where the data is stored
Constructors
Creates a new instance of the JSON file persistence component.
public
JsonFilePersister(Classtype, String path)
- path: String - (optional) path to the file where the data is stored.
Properties
path
Gets the file path where the data is stored.
protected
String _path;
- returns: String - file path where the data is stored.
Sets the file path where data is stored.
public
void setPath(String value)
- value: String - file path where data is stored.
Instance methods
configure
Configures the component by passing its configuration parameters.
public
void configure(ConfigParams config) throws ConfigException
- config: ConfigParams - configuration parameters to be set.
load
Loads data items from an external JSON file.
public
Listload(IContext context) throws ApplicationException
- context: IContext - (optional) a context to trace execution through a call chain.
- returns: List
- loaded items
save
Saves given data items to an external JSON file.
public
void save(IContext context, Listentities) throws ApplicationException
- context: IContext - (optional) a context to trace execution through a call chain.
- entities: List
- list if data items to save
Examples
{@code
JsonFilePersister persister = new JsonFilePersister(MyData.class, "./data/data.json");
ArrayList<String> list = new ArrayList<String>() {{
add("A");
add("B");
add("C");
}};
persister.save("123", list);
...
persister.load("123", items);
System.out.println(items); // Result: ["A", "B", "C"]
}