Use Json(Gson) to Serialize/Deserialize Collection of Object to Java Properties File


It's common and easy to store application configuration information like key=value in java properties file.
But how to store object or collection of object type into java properties file.

For example, I want to store a list of failed to fetch items in java properties file: including item id, failed times, and some other properties of this item, then I need deserialize it to list of UnableToFetchItem.
unableToFetchList={"id_1"\:{"id"\:"id_1, "extracting"\:a_long_millseconds,"failedTimes"\:a_int}, "id_x":{}, }

If we have to write our own code, it gets troublesome: when serialize the item lists, we have to put delimiter to separate items, serialize key/value pairs; when ddeserialize it back, we have to parse the delimiter, construct item object and set field value.

As my application already uses the java Json library:Gson, so I decide to serialize the list of UnableToFetchItem as a Json string, and then deserialize the Json string to a list of UnableToFetchItem. The code is much simpler and easy to read and maintain.
Implementation
The code is simple, the only trick part is how to serialize and deserialize a list of object: 
Look at GSon User Guide: Collections Examples
The key is to define a TypeToken with correct parameterized type:
private static Type objType = new TypeToken<Map<String,UnableToFetch>>() {}.getType();
import java.lang.reflect.Type;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class AppConfig {
  private Map<String,UnableToFetch> unableToFetchMap = new HashMap<String,UnableToFetch>();
  private static Type objType = new TypeToken<Map<String,UnableToFetch>>() {}
      .getType();
  
  public static Map<String,UnableToFetch> fromJson(String jsonStr) {
    Map<String,UnableToFetch> result = null;
    if (StringUtils.isNotBlank(jsonStr)) {
      result = new Gson().fromJson(jsonStr, objType);
    }
    if (result == null) {
      result = new HashMap<String,UnableToFetch>();
    }
    return result;
  }
  
  public static String toJson(Map<String,UnableToFetch> map) {
    return new Gson().toJson(map, objType);
  }
  
  public AppConfig readFromPropertiesFile() {
    File proppertiesFile = new File("PROPERTIES_FILE_NAME");
    if (!proppertiesFile.exists()) {
      return this;
    }
    Properties properties = new Properties();
    FileInputStream fis = null;
    try {
      fis = new FileInputStream(proppertiesFile);
      properties.load(fis);
      String str = properties.getProperty("unableToFetchList");
      unableToFetchMap = fromJson(str);
    } catch (Exception e) {
      throw new RuntimeException(e);
    } finally {
      IOUtils.closeQuietly(fis);
    }
    return this;
  }

  public void saveProperties() {
    File proppertiesFile = new File("PROPERTIES_FILE_NAME");
    
    Properties properties = new Properties();
    FileOutputStream fos = null;
    try {
      fos = new FileOutputStream(proppertiesFile);
      if (unableToFetchMap != null) {
        properties.setProperty("unableToFetchList", toJson(unableToFetchMap));
      }
      properties.store(fos, null);
    } catch (Exception e) {
      throw new RuntimeException(e);
    } finally {
      IOUtils.closeQuietly(fos);
    }
  }
  
  public static void main(String[] args) {
    Map<String,UnableToFetch> unableToFetchMap = new HashMap<String,UnableToFetch>();
    
    unableToFetchMap.put("id1", new UnableToFetch("id1", 1, 11111));
    String jsonStr = AppConfig.toJson(unableToFetchMap);
    System.out.println(jsonStr);
    
    unableToFetchMap = AppConfig.fromJson(jsonStr);
    jsonStr = AppConfig.toJson(unableToFetchMap);
    System.out.println(jsonStr);
  }
  
  public static class UnableToFetch {
    private String id;
    private long along;
    private int failedTimes;
    public UnableToFetch() {}
    public String toString() {
      return ToStringBuilder.reflectionToString(this);
    }
  }
}

Labels

adsense (5) Algorithm (69) Algorithm Series (35) Android (7) ANT (6) bat (8) Big Data (7) Blogger (14) Bugs (6) Cache (5) Chrome (19) Code Example (29) Code Quality (7) Coding Skills (5) Database (7) Debug (16) Design (5) Dev Tips (63) Eclipse (32) Git (5) Google (33) Guava (7) How to (9) Http Client (8) IDE (7) Interview (88) J2EE (13) J2SE (49) Java (186) JavaScript (27) JSON (7) Learning code (9) Lesson Learned (6) Linux (26) Lucene-Solr (112) Mac (10) Maven (8) Network (9) Nutch2 (18) Performance (9) PowerShell (11) Problem Solving (11) Programmer Skills (6) regex (5) Scala (6) Security (9) Soft Skills (38) Spring (22) System Design (11) Testing (7) Text Mining (14) Tips (17) Tools (24) Troubleshooting (29) UIMA (9) Web Development (19) Windows (21) xml (5)