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