import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Properties; /** * JSONPropertiesConverter convert a {@link Properties} file to a JSON object * that can be used to access properties values with a dot separated path style. *
* E.g.:
* In properties : person.name.first = "Firstname"
* In JavaScript : var firstname = person.name.first;
* * * @see http://www.json.org/ */ public class JSONPropertiesConverter { public static void main(String[] args) { Properties properties = new Properties(); properties.put("mail.format.invalid", "Format invalide"); properties.put("format.date", "dd/mm/yy"); System.out.println(new JSONPropertiesConverter().convert(properties)); } private final HashMap parents = new HashMap(); /** * Convert the given {@link Properties} to a JSON Object to allow you * accessing the properties values with a dot separated path. * * @param properties - The properties to convert. * @return A string that represent the JSON Object. */ public String convert(final Properties properties) { parents.clear(); for (Object objKey : properties.keySet()) { String key = objKey.toString(); String parts[] = key.split("\\."); JSONEntry parent = null; for (int i=0; i0 ) { buffer.replace(buffer.length()-(",\n".length()), buffer.length(), ""); } return buffer.toString(); } // ~ Private methods ------------------------------------------------------- private JSONEntry getParent(final String name) { JSONEntry parent = null; if ( parents.containsKey(name) ) { parent = parents.get(name); } else { parent = new JSONEntry(name); parents.put(name, parent); } return parent; } // ~ Internal classes ------------------------------------------------------ private class JSONEntry { private final String key; private String value; private JSONEntry parent; private List childs; public JSONEntry(final String key) { this(key, null); } public JSONEntry(final String key, final String value) { this.key = key; this.value = value; } public void setParent(final JSONEntry parent) { this.parent = parent; } public JSONEntry getParent() { return parent; } public void addChild(final JSONEntry child) { if ( childs == null ) { childs = new ArrayList(1); } if ( childs.contains(child) ) { childs.remove(child); } childs.add(child); } public void setValue(final String value) { if ( (childs!=null) && (childs.size()>0) ) { throw new IllegalStateException("A final entry cannot hold childs. "+toString()); } this.value = value; } public String getKey() { return key; } public String getParentKey() { if ( key.indexOf('.')<=0 ) { return null; } String name = key.substring(key.lastIndexOf('.')+1); return key.substring(0, key.length()-name.length()+1); } public String toJSON() { /* Use double quotes for string. With using double quotes, special * chars are ignored with JavaScript and only the '"' character * must be escaped. */ StringBuilder buff = new StringBuilder("\"").append(key).append("\""); if ( value!=null) { String jsValue = value .replaceAll("''","'") // Remove double quotes .replaceAll("\\\"", "\\\\\"") // Escape " because they are used in json /*.replaceAll("\\'", "\\\\'") // Escape single quotes .replaceAll("\\{", "\\\\{") .replaceAll("\\}", "\\\\}") .replaceAll("\\[", "\\\\[") .replaceAll("\\]", "\\\\]")*/ .replaceAll("[ \\t\\n\\x0B\\f\\r]{2,}", " "); // Remove redundant white spaces buff.append(" : \"").append(jsValue).append("\""); } else { buff.append(" : {"); if ( childs!=null ) { for (JSONEntry child : childs) { buff.append(child.toJSON()).append(", "); } // Remove the last ", " buff.replace(buff.length()-2, buff.length(), " "); } buff.append("}"); } return buff.toString(); } } }