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