Class JsonFilePersister<T>
java.lang.Object
org.pipservices3.data.persistence.JsonFilePersister<T>
public class JsonFilePersister<T>
extends Object
implements ILoader<T>, ISaver<T>, org.pipservices3.commons.config.IConfigurable
Persistence component that loads and saves data from/to flat file.
It is used by FilePersistence
, but can be useful on its own.
### Configuration parameters ###
- path: path to the file where data is stored
### Example ###
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"]
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionJsonFilePersister
(Class<T> type) Creates a new instance of the persistence.JsonFilePersister
(Class<T> type, String path) Creates a new instance of the persistence. -
Method Summary
Modifier and TypeMethodDescriptionvoid
configure
(org.pipservices3.commons.config.ConfigParams config) Configures component by passing configuration parameters.getPath()
Gets the file path where data is stored.Loads data items from external JSON file.void
Saves given data items to external JSON file.void
Sets the file path where data is stored.
-
Field Details
-
_mapper
protected com.fasterxml.jackson.databind.ObjectMapper _mapper -
_type
-
_typeRef
protected com.fasterxml.jackson.databind.JavaType _typeRef -
_path
-
_logger
protected org.pipservices3.components.log.CompositeLogger _logger
-
-
Constructor Details
-
JsonFilePersister
Creates a new instance of the persistence.- Parameters:
type
- the class type.
-
JsonFilePersister
Creates a new instance of the persistence.- Parameters:
type
- the class type.path
- (optional) a path to the file where data is stored.
-
-
Method Details
-
getPath
Gets the file path where data is stored.- Returns:
- the file path where data is stored.
-
setPath
Sets the file path where data is stored.- Parameters:
value
- the file path where data is stored.
-
configure
public void configure(org.pipservices3.commons.config.ConfigParams config) throws org.pipservices3.commons.errors.ConfigException Configures component by passing configuration parameters.- Specified by:
configure
in interfaceorg.pipservices3.commons.config.IConfigurable
- Parameters:
config
- configuration parameters to be set.- Throws:
org.pipservices3.commons.errors.ConfigException
- when configuration is wrong.
-
load
public List<T> load(String correlationId) throws org.pipservices3.commons.errors.ApplicationException Loads data items from external JSON file. -
save
public void save(String correlationId, List<T> entities) throws org.pipservices3.commons.errors.ApplicationException Saves given data items to external JSON file.
-