001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
026     * @author Brian Wing Shun Chan
027     */
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    }