001    /**
002     * Copyright (c) 2000-2011 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.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    }