001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.json;
016    
017    import com.liferay.portal.kernel.json.JSONArray;
018    import com.liferay.portal.kernel.json.JSONException;
019    import com.liferay.portal.kernel.json.JSONFactory;
020    import com.liferay.portal.kernel.json.JSONObject;
021    import com.liferay.portal.kernel.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    
024    import org.jabsorb.JSONSerializer;
025    import org.jabsorb.serializer.MarshallException;
026    import org.jabsorb.serializer.UnmarshallException;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     */
031    public class JSONFactoryImpl implements JSONFactory {
032    
033            public JSONFactoryImpl() {
034                    _serializer = new JSONSerializer();
035    
036                     try {
037                             _serializer.registerDefaultSerializers();
038                     }
039                     catch (Exception e) {
040                             _log.error(e, e);
041                     }
042            }
043    
044            public JSONArray createJSONArray() {
045                    return new JSONArrayImpl();
046            }
047    
048            public JSONArray createJSONArray(String json) throws JSONException {
049                    return new JSONArrayImpl(json);
050            }
051    
052            public JSONObject createJSONObject() {
053                    return new JSONObjectImpl();
054            }
055    
056            public JSONObject createJSONObject(String json) throws JSONException {
057                    return new JSONObjectImpl(json);
058            }
059    
060            public Object deserialize(JSONObject jsonObj) {
061                    return deserialize(jsonObj.toString());
062            }
063    
064            public Object deserialize(String json) {
065                    try {
066                            return _serializer.fromJSON(json);
067                    }
068                    catch (UnmarshallException ue) {
069                             _log.error(ue, ue);
070    
071                            throw new IllegalStateException("Unable to deserialize oject", ue);
072                    }
073            }
074    
075            public String serialize(Object obj) {
076                    try {
077                            return _serializer.toJSON(obj);
078                    }
079                    catch (MarshallException me) {
080                            _log.error(me, me);
081    
082                            throw new IllegalStateException("Unable to serialize oject", me);
083                    }
084            }
085    
086            private static Log _log = LogFactoryUtil.getLog(JSONFactoryImpl.class);
087    
088            private JSONSerializer _serializer;
089    
090    }