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 JSONObject getUnmodifiableJSONObject() {
194                    return _unmodifiableJSONObject;
195            }
196    
197            @Override
198            public Object looseDeserialize(String json) {
199                    try {
200                            JSONDeserializer<?> jsonDeserializer = createJSONDeserializer();
201    
202                            return jsonDeserializer.deserialize(json);
203                    }
204                    catch (Exception e) {
205                            if (_log.isWarnEnabled()) {
206                                    _log.warn(e, e);
207                            }
208    
209                            throw new IllegalStateException("Unable to deserialize object", e);
210                    }
211            }
212    
213            @Override
214            public <T> T looseDeserialize(String json, Class<T> clazz) {
215                    JSONDeserializer<?> jsonDeserializer = createJSONDeserializer();
216    
217                    jsonDeserializer.use(null, clazz);
218    
219                    return (T)jsonDeserializer.deserialize(json);
220            }
221    
222            @Override
223            public Object looseDeserializeSafe(String json) {
224                    try {
225                            JSONDeserializer<?> jsonDeserializer = createJSONDeserializer();
226    
227                            jsonDeserializer.safeMode(true);
228    
229                            return jsonDeserializer.deserialize(json);
230                    }
231                    catch (Exception e) {
232                            if (_log.isWarnEnabled()) {
233                                    _log.warn(e, e);
234                            }
235    
236                            throw new IllegalStateException("Unable to deserialize object", e);
237                    }
238            }
239    
240            @Override
241            public <T> T looseDeserializeSafe(String json, Class<T> clazz) {
242                    JSONDeserializer<?> jsonDeserializer = createJSONDeserializer();
243    
244                    jsonDeserializer.safeMode(true);
245    
246                    jsonDeserializer.use(null, clazz);
247    
248                    return (T)jsonDeserializer.deserialize(json);
249            }
250    
251            @Override
252            public String looseSerialize(Object object) {
253                    JSONSerializer jsonSerializer = createJSONSerializer();
254    
255                    return jsonSerializer.serialize(object);
256            }
257    
258            @Override
259            public String looseSerialize(
260                    Object object, JSONTransformer jsonTransformer, Class<?> clazz) {
261    
262                    JSONSerializer jsonSerializer = createJSONSerializer();
263    
264                    jsonSerializer.transform(jsonTransformer, clazz);
265    
266                    return jsonSerializer.serialize(object);
267            }
268    
269            @Override
270            public String looseSerialize(Object object, String... includes) {
271                    JSONSerializer jsonSerializer = createJSONSerializer();
272    
273                    jsonSerializer.include(includes);
274    
275                    return jsonSerializer.serialize(object);
276            }
277    
278            @Override
279            public String looseSerializeDeep(Object object) {
280                    JSONSerializer jsonSerializer = createJSONSerializer();
281    
282                    return jsonSerializer.serializeDeep(object);
283            }
284    
285            @Override
286            public String looseSerializeDeep(
287                    Object object, JSONTransformer jsonTransformer, Class<?> clazz) {
288    
289                    JSONSerializer jsonSerializer = createJSONSerializer();
290    
291                    jsonSerializer.transform(jsonTransformer, clazz);
292    
293                    return jsonSerializer.serializeDeep(object);
294            }
295    
296            @Override
297            public String serialize(Object object) {
298                    try {
299                            return _jsonSerializer.toJSON(object);
300                    }
301                    catch (MarshallException me) {
302                            if (_log.isWarnEnabled()) {
303                                    _log.warn(me, me);
304                            }
305    
306                            throw new IllegalStateException("Unable to serialize oject", me);
307                    }
308            }
309    
310            @Override
311            public String serializeException(Exception exception) {
312                    JSONObject jsonObject = createJSONObject();
313    
314                    String message = null;
315    
316                    if (exception instanceof InvocationTargetException) {
317                            Throwable cause = exception.getCause();
318    
319                            message = cause.toString();
320                    }
321                    else {
322                            message = exception.getMessage();
323                    }
324    
325                    if (Validator.isNull(message)) {
326                            message = exception.toString();
327                    }
328    
329                    jsonObject.put("exception", message);
330    
331                    return jsonObject.toString();
332            }
333    
334            @Override
335            public String serializeThrowable(Throwable throwable) {
336                    if (throwable instanceof Exception) {
337                            return serializeException((Exception)throwable);
338                    }
339    
340                    JSONObject jsonObject = createJSONObject();
341    
342                    String message = throwable.getMessage();
343    
344                    if (Validator.isNull(message)) {
345                            message = throwable.toString();
346                    }
347    
348                    jsonObject.put("throwable", message);
349    
350                    return jsonObject.toString();
351            }
352    
353            private static final String _NULL_JSON = "{}";
354    
355            private static Log _log = LogFactoryUtil.getLog(JSONFactoryImpl.class);
356    
357            private org.jabsorb.JSONSerializer _jsonSerializer;
358            private JSONObject _unmodifiableJSONObject =
359                    new UnmodifiableJSONObjectImpl();
360    
361    }