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