View Javadoc

1   /**********************************************************************
2    * JsonToolsAdapter.java
3    * created on 28.02.2007 by netseeker
4    * $Id: JsonToolsAdapter.java,v 1.1 2007/02/28 18:22:33 netseeker Exp $
5    * $Log: JsonToolsAdapter.java,v $
6    * Revision 1.1  2007/02/28 18:22:33  netseeker
7    * *** empty log message ***
8    *
9    *
10   * ====================================================================
11   *
12   *  Copyright 2005-2006 netseeker aka Michael Manske
13   *
14   *  Licensed under the Apache License, Version 2.0 (the "License");
15   *  you may not use this file except in compliance with the License.
16   *  You may obtain a copy of the License at
17   *
18   *      http://www.apache.org/licenses/LICENSE-2.0
19   *
20   *  Unless required by applicable law or agreed to in writing, software
21   *  distributed under the License is distributed on an "AS IS" BASIS,
22   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23   *  See the License for the specific language governing permissions and
24   *  limitations under the License.
25   * ====================================================================
26   *
27   * This file is part of the EJOE framework.
28   * For more information on the author, please see
29   * <http://www.manskes.de/>.
30   *
31   *********************************************************************/
32  package de.netseeker.ejoe.adapter.json;
33  
34  import java.io.InputStream;
35  import java.io.OutputStream;
36  
37  import com.sdicons.json.model.JSONObject;
38  import com.sdicons.json.parser.JSONParser;
39  import com.sdicons.json.serializer.marshall.JSONMarshall;
40  import com.sdicons.json.serializer.marshall.Marshall;
41  
42  import de.netseeker.ejoe.adapter.UTF8StringAdapter;
43  
44  /***
45   * A true multimode SerializeAdapter supporting JSON via Json Tools.
46   * @author netseeker
47   * @since 0.3.9.2
48   * @see <a href="http://jsontools.berlios.de">Json Tools</a>
49   */
50  public class JsonToolsAdapter extends UTF8StringAdapter
51  {
52      /***
53       * 
54       */
55      private static final long serialVersionUID = 1L;
56  
57      /*
58       * (non-Javadoc)
59       * 
60       * @see de.netseeker.ejoe.adapter.SerializeAdapter#read(java.io.InputStream)
61       */
62      public Object read( InputStream in ) throws Exception
63      {
64          JSONParser parser = new JSONParser( in );
65          JSONObject jObj = (JSONObject) parser.nextValue();
66          Marshall marshaller = new JSONMarshall();
67          return marshaller.unmarshall( jObj ).getReference();
68      }
69  
70      /*
71       * (non-Javadoc)
72       * 
73       * @see de.netseeker.ejoe.adapter.SerializeAdapter#write(java.lang.Object, java.io.OutputStream)
74       */
75      public void write( Object obj, OutputStream out ) throws Exception
76      {
77          Marshall marshaller = new JSONMarshall();
78          JSONObject jObj = marshaller.marshall( obj );
79          super.write( jObj.render( false ), out );
80      }
81      
82      /*
83       * (non-Javadoc)
84       * 
85       * @see de.netseeker.ejoe.adapter.BaseAdapter#getContentType()
86       */
87      public String getContentType()
88      {
89          return "application/json";
90      }    
91  
92  }