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