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