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