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