1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.json;
16  
17  import com.liferay.portal.kernel.json.JSONArray;
18  import com.liferay.portal.kernel.json.JSONException;
19  import com.liferay.portal.kernel.json.JSONFactory;
20  import com.liferay.portal.kernel.json.JSONObject;
21  import com.liferay.portal.kernel.log.Log;
22  import com.liferay.portal.kernel.log.LogFactoryUtil;
23  
24  import org.jabsorb.JSONSerializer;
25  import org.jabsorb.serializer.MarshallException;
26  import org.jabsorb.serializer.UnmarshallException;
27  
28  /**
29   * <a href="JSONFactoryImpl.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Brian Wing Shun Chan
32   */
33  public class JSONFactoryImpl implements JSONFactory {
34  
35      public JSONFactoryImpl() {
36          _serializer = new JSONSerializer();
37  
38           try {
39               _serializer.registerDefaultSerializers();
40           }
41           catch (Exception e) {
42               _log.error(e, e);
43           }
44      }
45  
46      public JSONArray createJSONArray() {
47          return new JSONArrayImpl();
48      }
49  
50      public JSONArray createJSONArray(String json) throws JSONException {
51          return new JSONArrayImpl(json);
52      }
53  
54      public JSONObject createJSONObject() {
55          return new JSONObjectImpl();
56      }
57  
58      public JSONObject createJSONObject(String json) throws JSONException {
59          return new JSONObjectImpl(json);
60      }
61  
62      public Object deserialize(JSONObject jsonObj) {
63          return deserialize(jsonObj.toString());
64      }
65  
66      public Object deserialize(String json) {
67          try {
68              return _serializer.fromJSON(json);
69          }
70          catch (UnmarshallException ue) {
71               _log.error(ue, ue);
72  
73              throw new IllegalStateException("Unable to deserialize oject", ue);
74          }
75      }
76  
77      public String serialize(Object obj) {
78          try {
79              return _serializer.toJSON(obj);
80          }
81          catch (MarshallException me) {
82              _log.error(me, me);
83  
84              throw new IllegalStateException("Unable to serialize oject", me);
85          }
86      }
87  
88      private static Log _log = LogFactoryUtil.getLog(JSONFactoryImpl.class);
89  
90      private JSONSerializer _serializer;
91  
92  }