001    /**
002     * Copyright (c) 2000-2012 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.alloy.util.json.StringTransformer;
018    import com.liferay.portal.json.jabsorb.serializer.LiferayJSONSerializer;
019    import com.liferay.portal.json.jabsorb.serializer.LiferaySerializer;
020    import com.liferay.portal.json.jabsorb.serializer.LocaleSerializer;
021    import com.liferay.portal.kernel.json.JSONArray;
022    import com.liferay.portal.kernel.json.JSONDeserializer;
023    import com.liferay.portal.kernel.json.JSONException;
024    import com.liferay.portal.kernel.json.JSONFactory;
025    import com.liferay.portal.kernel.json.JSONObject;
026    import com.liferay.portal.kernel.json.JSONSerializer;
027    import com.liferay.portal.kernel.json.JSONTransformer;
028    import com.liferay.portal.kernel.log.Log;
029    import com.liferay.portal.kernel.log.LogFactoryUtil;
030    import com.liferay.portal.kernel.util.Validator;
031    
032    import java.lang.reflect.InvocationTargetException;
033    
034    import java.util.List;
035    
036    import org.jabsorb.serializer.MarshallException;
037    
038    import org.json.JSONML;
039    
040    /**
041     * @author Brian Wing Shun Chan
042     */
043    public class JSONFactoryImpl implements JSONFactory {
044    
045            public JSONFactoryImpl() {
046                    JSONInit.init();
047    
048                    _jsonSerializer = new LiferayJSONSerializer();
049    
050                    try {
051                            _jsonSerializer.registerDefaultSerializers();
052    
053                            _jsonSerializer.registerSerializer(new LiferaySerializer());
054                            _jsonSerializer.registerSerializer(new LocaleSerializer());
055                    }
056                    catch (Exception e) {
057                            _log.error(e, e);
058                    }
059            }
060    
061            public String convertJSONMLArrayToXML(String jsonml) {
062                    try {
063                            org.json.JSONArray jsonArray = new org.json.JSONArray(jsonml);
064    
065                            return JSONML.toString(jsonArray);
066                    }
067                    catch (Exception e) {
068                            if (_log.isWarnEnabled()) {
069                                    _log.warn(e, e);
070                            }
071    
072                            throw new IllegalStateException("Unable to convert to XML", e);
073                    }
074            }
075    
076            public String convertJSONMLObjectToXML(String jsonml) {
077                    try {
078                            org.json.JSONObject jsonObject = new org.json.JSONObject(jsonml);
079    
080                            return JSONML.toString(jsonObject);
081                    }
082                    catch (Exception e) {
083                            if (_log.isWarnEnabled()) {
084                                    _log.warn(e, e);
085                            }
086    
087                            throw new IllegalStateException("Unable to convert to XML", e);
088                    }
089            }
090    
091            public String convertXMLtoJSONMLArray(String xml) {
092                    try {
093                            org.json.JSONArray jsonArray = JSONML.toJSONArray(xml);
094    
095                            return jsonArray.toString();
096                    }
097                    catch (Exception e) {
098                            if (_log.isWarnEnabled()) {
099                                    _log.warn(e, e);
100                            }
101    
102                            throw new IllegalStateException("Unable to convert to JSONML", e);
103                    }
104            }
105    
106            public String convertXMLtoJSONMLObject(String xml) {
107                    try {
108                            org.json.JSONObject jsonObject = JSONML.toJSONObject(xml);
109    
110                            return jsonObject.toString();
111                    }
112                    catch (Exception e) {
113                            if (_log.isWarnEnabled()) {
114                                    _log.warn(e, e);
115                            }
116    
117                            throw new IllegalStateException("Unable to convert to JSONML", e);
118                    }
119            }
120    
121            public JSONTransformer createJavaScriptNormalizerJSONTransformer(
122                    List<String> javaScriptAttributes) {
123    
124                    StringTransformer stringTransformer = new StringTransformer();
125    
126                    stringTransformer.setJavaScriptAttributes(javaScriptAttributes);
127    
128                    return stringTransformer;
129            }
130    
131            public JSONArray createJSONArray() {
132                    return new JSONArrayImpl();
133            }
134    
135            public JSONArray createJSONArray(String json) throws JSONException {
136                    return new JSONArrayImpl(json);
137            }
138    
139            public <T> JSONDeserializer<T> createJSONDeserializer() {
140                    return new JSONDeserializerImpl<T>();
141            }
142    
143            public JSONObject createJSONObject() {
144                    return new JSONObjectImpl();
145            }
146    
147            public JSONObject createJSONObject(String json) throws JSONException {
148                    return new JSONObjectImpl(json);
149            }
150    
151            public JSONSerializer createJSONSerializer() {
152                    return new JSONSerializerImpl();
153            }
154    
155            public Object deserialize(JSONObject jsonObj) {
156                    return deserialize(jsonObj.toString());
157            }
158    
159            public Object deserialize(String json) {
160                    try {
161                            return _jsonSerializer.fromJSON(json);
162                    }
163                    catch (Exception e) {
164                            if (_log.isWarnEnabled()) {
165                                    _log.warn(e, e);
166                            }
167    
168                            throw new IllegalStateException("Unable to deserialize object", e);
169                    }
170            }
171    
172            public String getNullJSON() {
173                    return _NULL_JSON;
174            }
175    
176            public Object looseDeserialize(String json) {
177                    try {
178                            JSONDeserializer<?> jsonDeserializer = createJSONDeserializer();
179    
180                            return jsonDeserializer.deserialize(json);
181                    }
182                    catch (Exception e) {
183                            if (_log.isWarnEnabled()) {
184                                    _log.warn(e, e);
185                            }
186    
187                            throw new IllegalStateException("Unable to deserialize object", e);
188                    }
189            }
190    
191            public <T> T looseDeserialize(String json, Class<T> clazz) {
192                    JSONDeserializer<?> jsonDeserializer = createJSONDeserializer();
193    
194                    jsonDeserializer.use(null, clazz);
195    
196                    return (T)jsonDeserializer.deserialize(json);
197            }
198    
199            public Object looseDeserializeSafe(String json) {
200                    try {
201                            JSONDeserializer<?> jsonDeserializer = createJSONDeserializer();
202    
203                            jsonDeserializer.safeMode(true);
204    
205                            return jsonDeserializer.deserialize(json);
206                    }
207                    catch (Exception e) {
208                            if (_log.isWarnEnabled()) {
209                                    _log.warn(e, e);
210                            }
211    
212                            throw new IllegalStateException("Unable to deserialize object", e);
213                    }
214            }
215    
216            public <T> T looseDeserializeSafe(String json, Class<T> clazz) {
217                    JSONDeserializer<?> jsonDeserializer = createJSONDeserializer();
218    
219                    jsonDeserializer.safeMode(true);
220    
221                    jsonDeserializer.use(null, clazz);
222    
223                    return (T)jsonDeserializer.deserialize(json);
224            }
225    
226            public String looseSerialize(Object object) {
227                    JSONSerializer jsonSerializer = createJSONSerializer();
228    
229                    return jsonSerializer.serialize(object);
230            }
231    
232            public String looseSerialize(
233                    Object object, JSONTransformer jsonTransformer, Class<?> clazz) {
234    
235                    JSONSerializer jsonSerializer = createJSONSerializer();
236    
237                    jsonSerializer.transform(jsonTransformer, clazz);
238    
239                    return jsonSerializer.serialize(object);
240            }
241    
242            public String looseSerialize(Object object, String... includes) {
243                    JSONSerializer jsonSerializer = createJSONSerializer();
244    
245                    jsonSerializer.include(includes);
246    
247                    return jsonSerializer.serialize(object);
248            }
249    
250            public String looseSerializeDeep(Object object) {
251                    JSONSerializer jsonSerializer = createJSONSerializer();
252    
253                    return jsonSerializer.serializeDeep(object);
254            }
255    
256            public String looseSerializeDeep(
257                    Object object, JSONTransformer jsonTransformer, Class<?> clazz) {
258    
259                    JSONSerializer jsonSerializer = createJSONSerializer();
260    
261                    jsonSerializer.transform(jsonTransformer, clazz);
262    
263                    return jsonSerializer.serializeDeep(object);
264            }
265    
266            public String serialize(Object object) {
267                    try {
268                            return _jsonSerializer.toJSON(object);
269                    }
270                    catch (MarshallException me) {
271                            if (_log.isWarnEnabled()) {
272                                    _log.warn(me, me);
273                            }
274    
275                            throw new IllegalStateException("Unable to serialize oject", me);
276                    }
277            }
278    
279            public String serializeException(Exception exception) {
280                    JSONObject jsonObject = createJSONObject();
281    
282                    String message = null;
283    
284                    if (exception instanceof InvocationTargetException) {
285                            Throwable cause = exception.getCause();
286    
287                            message = cause.toString();
288                    }
289                    else {
290                            message = exception.getMessage();
291                    }
292    
293                    if (Validator.isNull(message)) {
294                            message = exception.toString();
295                    }
296    
297                    jsonObject.put("exception", message);
298    
299                    return jsonObject.toString();
300            }
301    
302            public String serializeThrowable(Throwable throwable) {
303                    if (throwable instanceof Exception) {
304                            return serializeException((Exception)throwable);
305                    }
306    
307                    JSONObject jsonObject = createJSONObject();
308    
309                    String message = throwable.getMessage();
310    
311                    if (Validator.isNull(message)) {
312                            message = throwable.toString();
313                    }
314    
315                    jsonObject.put("throwable", message);
316    
317                    return jsonObject.toString();
318            }
319    
320            private static final String _NULL_JSON = "{}";
321    
322            private static Log _log = LogFactoryUtil.getLog(JSONFactoryImpl.class);
323    
324            private org.jabsorb.JSONSerializer _jsonSerializer;
325    
326    }