001
014
015 package com.liferay.portal.json;
016
017 import com.liferay.portal.json.jabsorb.serializer.LiferayJSONSerializer;
018 import com.liferay.portal.json.jabsorb.serializer.LiferaySerializer;
019 import com.liferay.portal.json.jabsorb.serializer.LocaleSerializer;
020 import com.liferay.portal.kernel.json.JSONArray;
021 import com.liferay.portal.kernel.json.JSONDeserializer;
022 import com.liferay.portal.kernel.json.JSONException;
023 import com.liferay.portal.kernel.json.JSONFactory;
024 import com.liferay.portal.kernel.json.JSONObject;
025 import com.liferay.portal.kernel.json.JSONSerializer;
026 import com.liferay.portal.kernel.json.JSONTransformer;
027 import com.liferay.portal.kernel.json.JSONValidator;
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
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 throw new UnsupportedOperationException(
133 "Temporally disabled until alloy-taglib.jar is updated");
134 }
135
136 @Override
137 public JSONArray createJSONArray() {
138 return new JSONArrayImpl();
139 }
140
141 @Override
142 public JSONArray createJSONArray(String json) throws JSONException {
143 return new JSONArrayImpl(json);
144 }
145
146 @Override
147 public <T> JSONDeserializer<T> createJSONDeserializer() {
148 return new JSONDeserializerImpl<>();
149 }
150
151 @Override
152 public JSONObject createJSONObject() {
153 return new JSONObjectImpl();
154 }
155
156 @Override
157 public JSONObject createJSONObject(String json) throws JSONException {
158 return new JSONObjectImpl(json);
159 }
160
161 @Override
162 public JSONSerializer createJSONSerializer() {
163 return new JSONSerializerImpl();
164 }
165
166 @Override
167 public JSONValidator createJSONValidator(String jsonSchema)
168 throws JSONException {
169
170 return new JSONValidatorImpl(jsonSchema);
171 }
172
173 @Override
174 public Object deserialize(JSONObject jsonObj) {
175 return deserialize(jsonObj.toString());
176 }
177
178 @Override
179 public Object deserialize(String json) {
180 try {
181 return _jsonSerializer.fromJSON(json);
182 }
183 catch (Exception e) {
184 if (_log.isWarnEnabled()) {
185 _log.warn(e, e);
186 }
187
188 throw new IllegalStateException("Unable to deserialize object", e);
189 }
190 }
191
192 @Override
193 public String getNullJSON() {
194 return _NULL_JSON;
195 }
196
197 @Override
198 public JSONObject getUnmodifiableJSONObject() {
199 return _unmodifiableJSONObject;
200 }
201
202 @Override
203 public Object looseDeserialize(String json) {
204 try {
205 JSONDeserializer<?> jsonDeserializer = createJSONDeserializer();
206
207 return jsonDeserializer.deserialize(json);
208 }
209 catch (Exception e) {
210 if (_log.isWarnEnabled()) {
211 _log.warn(e, e);
212 }
213
214 throw new IllegalStateException("Unable to deserialize object", e);
215 }
216 }
217
218 @Override
219 public <T> T looseDeserialize(String json, Class<T> clazz) {
220 JSONDeserializer<?> jsonDeserializer = createJSONDeserializer();
221
222 jsonDeserializer.use(null, clazz);
223
224 return (T)jsonDeserializer.deserialize(json);
225 }
226
227 @Override
228 public String looseSerialize(Object object) {
229 JSONSerializer jsonSerializer = createJSONSerializer();
230
231 return jsonSerializer.serialize(object);
232 }
233
234 @Override
235 public String looseSerialize(
236 Object object, JSONTransformer jsonTransformer, Class<?> clazz) {
237
238 JSONSerializer jsonSerializer = createJSONSerializer();
239
240 jsonSerializer.transform(jsonTransformer, clazz);
241
242 return jsonSerializer.serialize(object);
243 }
244
245 @Override
246 public String looseSerialize(Object object, String... includes) {
247 JSONSerializer jsonSerializer = createJSONSerializer();
248
249 jsonSerializer.include(includes);
250
251 return jsonSerializer.serialize(object);
252 }
253
254 @Override
255 public String looseSerializeDeep(Object object) {
256 JSONSerializer jsonSerializer = createJSONSerializer();
257
258 return jsonSerializer.serializeDeep(object);
259 }
260
261 @Override
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 @Override
273 public String serialize(Object object) {
274 try {
275 return _jsonSerializer.toJSON(object);
276 }
277 catch (MarshallException me) {
278 if (_log.isWarnEnabled()) {
279 _log.warn(me, me);
280 }
281
282 throw new IllegalStateException("Unable to serialize object", me);
283 }
284 }
285
286 @Override
287 public String serializeThrowable(Throwable throwable) {
288 JSONObject jsonObject = createJSONObject();
289
290 if (throwable instanceof InvocationTargetException) {
291 throwable = throwable.getCause();
292 }
293
294 String throwableMessage = throwable.getMessage();
295
296 if (Validator.isNull(throwableMessage)) {
297 throwableMessage = throwable.toString();
298 }
299
300 JSONObject errorJSONObject = createJSONObject();
301
302 errorJSONObject.put("message", throwableMessage);
303 errorJSONObject.put("type", ClassUtil.getClassName(throwable));
304
305 jsonObject.put("error", errorJSONObject);
306
307 jsonObject.put("exception", throwableMessage);
308 jsonObject.put("throwable", throwable.toString());
309
310 if (throwable.getCause() == null) {
311 return jsonObject.toString();
312 }
313
314 Throwable rootCauseThrowable = throwable;
315
316 while (rootCauseThrowable.getCause() != null) {
317 rootCauseThrowable = rootCauseThrowable.getCause();
318 }
319
320 JSONObject rootCauseJSONObject = createJSONObject();
321
322 throwableMessage = rootCauseThrowable.getMessage();
323
324 if (Validator.isNull(throwableMessage)) {
325 throwableMessage = rootCauseThrowable.toString();
326 }
327
328 rootCauseJSONObject.put("message", throwableMessage);
329
330 rootCauseJSONObject.put(
331 "type", ClassUtil.getClassName(rootCauseThrowable));
332
333 jsonObject.put("rootCause", rootCauseJSONObject);
334
335 return jsonObject.toString();
336 }
337
338 private static final String _NULL_JSON = "{}";
339
340 private static final Log _log = LogFactoryUtil.getLog(
341 JSONFactoryImpl.class);
342
343 private final org.jabsorb.JSONSerializer _jsonSerializer;
344 private final JSONObject _unmodifiableJSONObject =
345 new UnmodifiableJSONObjectImpl();
346
347 }