A JSON Util
Nowadays JSON(json.org) is a very common data-interchange format other than XML, and its much more lightweights. I was trying to write a JSON util class today and came across many JSON lbraries. Its hard to determine which library is better, and I am not planning to do it here in this post neither. I wrote a simple wrapper and wish to share it out with you guys. I used json-simple(http://code.google.com/p/json-simple/), a simple yet useful JSON library. Here’s my code :
package com.justthor;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.json.simple.parser.JSONParser;
public final class JsonUtil {
private static final LoggingUtil logger = LoggingUtil.getInstance(JsonUtil.class);
private static final JSONParser jsonParser = new JSONParser();
@SuppressWarnings("unchecked")
private static final Map EMPTY_MAP = Collections.EMPTY_MAP;
@SuppressWarnings("unchecked")
private static final List EMPTY_LIST = Collections.EMPTY_LIST;
private final JSONObject obj;
/**
* Private constructor
*/
private JsonUtil() {
obj = new JSONObject();
}
/**
* Encode object to JSON string. Object can be String, Integer etc.
* @param Object
* @return String
*/
public static String encode(final Object obj) {
return JSONValue.toJSONString(obj);
}
/**
* Decode string to raw object
* @param String
* @return Object
*/
public static Object decode(final String string) {
Object result;
try{
result = jsonParser.parse(string);
} catch (Exception e) {
result = new Object();
logger.error(e.getMessage(), e);
}
return result;
}
/**
* Decode string to List
* @param String
* @return List
*/
@SuppressWarnings("unchecked")
public static List decodeAsList(final String string) {
List result = EMPTY_LIST;
try{
result = (List)jsonParser.parse(string);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
return result;
}
/**
* Decode string to Map
* @param String
* @return Map
*/
@SuppressWarnings("unchecked")
public static Map decodeAsMap(final String string) {
Map result = EMPTY_MAP;
try{
result = (Map)jsonParser.parse(string);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
return result;
}
/**
* Get an instance of JsonUtil.
* @return JsonUtil
*/
public static JsonUtil getInstance() {
return new JsonUtil();
}
/**
* Put a name and value.
* Example :
* JsonUtil.getInstance().put("name", "thor").put("vehicle", "none").put("height", 165).toJsonString();
* @param Object
* @param Object
* @return JsonUtil
*/
@SuppressWarnings("unchecked")
public JsonUtil put(final Object name, final Object value) {
obj.put(name, value);
return this;
}
/**
* Convert stored name and value into Json string.
* @return String
*/
public String toJsonString(){
return obj.toString();
}
}
You can use it as a static class, such as
List<String> list = new ArrayList<String>(); ... your code to populate the list here... // Encode! String jsonEncodedString = JsonUtil.encode(list);
To decode, simply type the following
List decodedList = (List)JsonUtil.decode(jsonEncodedString); // Or even easier, I wrote another method to decode JSON String as List List decodedList = JsonUtil.decodeList(jsonEncodedString);
If you don’t feel like using Map, you can do this
String jsonEncodedString = JsonUtil.getInstance()
.put("name", "Thor")
.put("url", "www.just-thor.com")
.toJsonString();
A wrapper is important to encapsulate how things actually works. When you using the wrapper I wrote, you don’t have to import or even know that I am using json-simple or whatsoever. I hope this example helps you to understand the importance of writing a wrapper class.
Happy programming












Recent Comments