001
014
015 package com.liferay.util.json;
016
017 import com.liferay.portal.kernel.json.JSONObject;
018 import com.liferay.portal.kernel.log.Log;
019 import com.liferay.portal.kernel.log.LogFactoryUtil;
020
021 import org.jabsorb.JSONSerializer;
022 import org.jabsorb.serializer.MarshallException;
023 import org.jabsorb.serializer.UnmarshallException;
024
025
028 public class JSONFactoryUtil {
029
030 public static Object deserialize(JSONObject jsonObj) {
031 return _instance._deserialize(jsonObj);
032 }
033
034 public static Object deserialize(String json) {
035 return _instance._deserialize(json);
036 }
037
038 public static String serialize(Object obj) {
039 return _instance._serialize(obj);
040 }
041
042 private JSONFactoryUtil() {
043 _serializer = new JSONSerializer();
044
045 try {
046 _serializer.registerDefaultSerializers();
047 }
048 catch (Exception e) {
049 _log.error(e, e);
050 }
051 }
052
053 private Object _deserialize(JSONObject jsonObj) {
054 return _deserialize(jsonObj.toString());
055 }
056
057 private Object _deserialize(String json) {
058 try {
059 return _serializer.fromJSON(json);
060 }
061 catch (UnmarshallException ue) {
062 _log.error(ue, ue);
063
064 throw new IllegalStateException("Unable to deserialize oject", ue);
065 }
066 }
067
068 private String _serialize(Object obj) {
069 try {
070 return _serializer.toJSON(obj);
071 }
072 catch (MarshallException me) {
073 _log.error(me, me);
074
075 throw new IllegalStateException("Unable to serialize oject", me);
076 }
077 }
078
079 private static Log _log = LogFactoryUtil.getLog(JSONFactoryUtil.class);
080
081 private static JSONFactoryUtil _instance = new JSONFactoryUtil();
082
083 private JSONSerializer _serializer;
084
085 }