View Javadoc

1   /**********************************************************************
2    * JsonLibAdapter.java
3    * created on 05.11.2006 by netseeker
4    * $Id: JsonLibAdapter.java,v 1.4 2007/11/17 10:59:17 netseeker Exp $
5    * $Log: JsonLibAdapter.java,v $
6    * Revision 1.4  2007/11/17 10:59:17  netseeker
7    * *** empty log message ***
8    *
9    * Revision 1.3  2007/03/25 15:03:23  netseeker
10   * *** empty log message ***
11   *
12   * Revision 1.2  2007/03/22 21:01:34  netseeker
13   * *** empty log message ***
14   *
15   * Revision 1.1  2007/02/11 15:42:20  netseeker
16   * *** empty log message ***
17   *
18   * Revision 1.3  2006/11/06 11:02:44  netseeker
19   * *** empty log message ***
20   *
21   * Revision 1.2  2006/11/05 17:13:09  netseeker
22   * added source documentation
23   *
24   * Revision 1.1  2006/11/05 16:33:37  netseeker
25   * changed adapter connection handling
26   * added support for JSON with different JSON-adapters (XStream,JSON-lib,MyJSON)
27   *
28   *
29   * ====================================================================
30   *
31   *  Copyright 2005-2006 netseeker aka Michael Manske
32   *
33   *  Licensed under the Apache License, Version 2.0 (the "License");
34   *  you may not use this file except in compliance with the License.
35   *  You may obtain a copy of the License at
36   *
37   *      http://www.apache.org/licenses/LICENSE-2.0
38   *
39   *  Unless required by applicable law or agreed to in writing, software
40   *  distributed under the License is distributed on an "AS IS" BASIS,
41   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
42   *  See the License for the specific language governing permissions and
43   *  limitations under the License.
44   * ====================================================================
45   *
46   * This file is part of the EJOE framework.
47   * For more information on the author, please see
48   * <http://www.manskes.de/>.
49   *
50   *********************************************************************/
51  package de.netseeker.ejoe.adapter.json;
52  
53  import java.io.InputStream;
54  import java.io.OutputStream;
55  
56  import net.sf.json.JSON;
57  import net.sf.json.JSONObject;
58  import net.sf.json.JSONSerializer;
59  import net.sf.json.util.JSONUtils;
60  import de.netseeker.ejoe.adapter.UTF8StringAdapter;
61  
62  /***
63   * Multimode SerializeAdapter supporting JSON via the JSON-lib. By default this adapter is write-only: It is able to
64   * serialize Objects to JSON but does just return JSON strings on read operations. The read mode can be switched but
65   * then Objects according to what JSON-lib creates will be returned.
66   * 
67   * @author netseeker
68   * @since 0.3.9.1
69   * @see <a href="http://json-lib.sourceforge.net/">JSON-lib</a>
70   */
71  public class JsonLibAdapter extends UTF8StringAdapter
72  {
73      /***
74       * 
75       */
76      private static final long serialVersionUID = 1L;
77  
78      private boolean           _writeOnly;
79  
80      /***
81       * Creates a write-only instance of this adapter. It will write Object instances to JSON but will just return JSON
82       * string representations on read operations.
83       */
84      public JsonLibAdapter()
85      {
86          this( true );
87      }
88  
89      /***
90       * Creates either a write-only or a mixed-mode instance of this adapter. In write-only mode it will write Object
91       * instances to JSON but will just return JSON string representations on read operations. In mixed-mode Objects
92       * according to what JSON-lib creates will be returned on read operations.
93       * 
94       * @param writeOnly whether to uses write-only mode or not
95       */
96      public JsonLibAdapter(boolean writeOnly)
97      {
98          _writeOnly = writeOnly;
99      }
100 
101     /*
102      * (non-Javadoc)
103      * 
104      * @see de.netseeker.ejoe.adapter.SerializeAdapter#read(java.io.InputStream)
105      */
106     public Object read( InputStream in ) throws Exception
107     {
108         String json = (String) super.read( in );
109         if ( _writeOnly )
110         {
111             return json;
112         }
113         else
114         {
115             Object result = null;
116 
117             if ( json != null )
118             {
119                 JSONObject jsonObject = JSONObject.fromObject( json );
120 
121                 if ( !jsonObject.isNullObject() )
122                 {
123                     if ( jsonObject.has( "uniqueName" ) )
124                     {
125                         result = JSONObject.toBean( jsonObject, Class.forName( jsonObject.getString( "uniqueName" ) ) );
126                     }
127                     else
128                     {
129                         result = JSONSerializer.toJava( jsonObject );
130                     }
131                 }
132             }
133 
134             return result;
135         }
136     }
137 
138     /*
139      * (non-Javadoc)
140      * 
141      * @see de.netseeker.ejoe.adapter.SerializeAdapter#write(java.lang.Object, java.io.OutputStream)
142      */
143     public void write( Object obj, OutputStream out ) throws Exception
144     {
145         JSON json = null;
146         
147         if( JSONUtils.isObject( obj ))
148         {
149             json = JSONObject.fromObject( obj );            
150         }
151         else
152         {
153            json = JSONSerializer.toJSON( obj );
154         }
155         
156         super.write( json, out );
157     }
158 
159     /*
160      * (non-Javadoc)
161      * 
162      * @see de.netseeker.ejoe.adapter.BaseAdapter#getContentType()
163      */
164     public String getContentType()
165     {
166         return "application/json";
167     }
168 }