Class JsonFilePersister<T>

java.lang.Object
org.pipservices3.data.persistence.JsonFilePersister<T>
All Implemented Interfaces:
org.pipservices3.commons.config.IConfigurable, ILoader<T>, ISaver<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
    Modifier and Type
    Field
    Description
    protected org.pipservices3.components.log.CompositeLogger
     
    protected com.fasterxml.jackson.databind.ObjectMapper
     
    protected String
     
    protected Class<T>
     
    protected com.fasterxml.jackson.databind.JavaType
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new instance of the persistence.
    Creates a new instance of the persistence.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    configure(org.pipservices3.commons.config.ConfigParams config)
    Configures component by passing configuration parameters.
    Gets the file path where data is stored.
    load(String correlationId)
    Loads data items from external JSON file.
    void
    save(String correlationId, List<T> entities)
    Saves given data items to external JSON file.
    void
    setPath(String value)
    Sets the file path where data is stored.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • _mapper

      protected com.fasterxml.jackson.databind.ObjectMapper _mapper
    • _type

      protected Class<T> _type
    • _typeRef

      protected com.fasterxml.jackson.databind.JavaType _typeRef
    • _path

      protected String _path
    • _logger

      protected org.pipservices3.components.log.CompositeLogger _logger
  • Constructor Details

    • JsonFilePersister

      public JsonFilePersister(Class<T> type)
      Creates a new instance of the persistence.
      Parameters:
      type - the class type.
    • JsonFilePersister

      public JsonFilePersister(Class<T> type, String path)
      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

      public String getPath()
      Gets the file path where data is stored.
      Returns:
      the file path where data is stored.
    • setPath

      public void setPath(String value)
      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 interface org.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.
      Specified by:
      load in interface ILoader<T>
      Parameters:
      correlationId - (optional) transaction id to trace execution through call chain.
      Returns:
      loaded items.
      Throws:
      org.pipservices3.commons.errors.ApplicationException - when error occured.
    • save

      public void save(String correlationId, List<T> entities) throws org.pipservices3.commons.errors.ApplicationException
      Saves given data items to external JSON file.
      Specified by:
      save in interface ISaver<T>
      Parameters:
      correlationId - (optional) transaction id to trace execution through call chain.
      entities - list if data items to save
      Throws:
      org.pipservices3.commons.errors.ApplicationException - when error occured.