commit | author | age
|
ca8e1e
|
1 |
/* |
U |
2 |
Transit - Remote procedure calls made simple |
|
3 |
Copyright (c) 2012 Ulrich Hilger |
|
4 |
|
|
5 |
This program is free software: you can redistribute it and/or modify |
|
6 |
it under the terms of the GNU Affero General Public License as published by |
|
7 |
the Free Software Foundation, either version 3 of the License, or |
|
8 |
(at your option) any later version. |
|
9 |
|
|
10 |
This program is distributed in the hope that it will be useful, |
|
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
GNU Affero General Public License for more details. |
|
14 |
|
|
15 |
You should have received a copy of the GNU Affero General Public License |
|
16 |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 |
*/ |
|
18 |
|
|
19 |
package de.uhilger.transit; |
|
20 |
|
|
21 |
import java.io.Writer; |
|
22 |
|
|
23 |
import com.thoughtworks.xstream.XStream; |
|
24 |
import com.thoughtworks.xstream.io.json.JsonHierarchicalStreamDriver; |
|
25 |
import com.thoughtworks.xstream.io.json.JsonWriter; |
|
26 |
import com.thoughtworks.xstream.io.HierarchicalStreamWriter; |
|
27 |
|
|
28 |
/** |
|
29 |
* Java-Objekte in die JavaScript Object Notation (JSON) |
|
30 |
* verwandeln und umgekehrt |
|
31 |
* |
053e6a
|
32 |
* Dieser Wandler erzeugt ein strukturiertes und gut lesbares JSON-Format, |
U |
33 |
* bei dem das Wurzelelement weggelassen wird. Im Falle einer Liste wird |
|
34 |
* damit z.B. der Inhalt ohne umschliessendes |
|
35 |
* <pre>{"List": [ ]}</pre> |
|
36 |
* erezeugt. |
|
37 |
* |
ca8e1e
|
38 |
* <p>Diese Klasse benoetigt die Java-Klassenbibliotheken |
42c48e
|
39 |
* <a href="http://xstream.codehaus.org/" target="_blank">XStream</a> |
U |
40 |
* und <a href="http://jettison.codehaus.org/" target="_blank">Jettison</a> |
ca8e1e
|
41 |
* im Classpath.</p> |
U |
42 |
* |
|
43 |
* @author Copyright (c) Ulrich Hilger, http://uhilger.de |
|
44 |
* @author Published under the terms and conditions of |
|
45 |
* the <a href="http://www.gnu.org/licenses/agpl-3.0" target="_blank">GNU Affero General Public License</a> |
|
46 |
* |
|
47 |
* @version 1, September 16, 2012 |
|
48 |
*/ |
|
49 |
public class JsonFlatWandler extends AbstractWandler { |
|
50 |
|
|
51 |
/** Bezeichnerdes Formats, das dieser Wandler unterstuetzt */ |
|
52 |
public static final String FORMAT_FLATJSON = "FLATJSON"; |
|
53 |
|
|
54 |
protected XStream getXStream() { |
|
55 |
//XStream xstream = new XStream(new JettisonMappedXmlDriver()); |
|
56 |
XStream xstream = new XStream(new JsonHierarchicalStreamDriver() { |
|
57 |
public HierarchicalStreamWriter createWriter(Writer writer) { |
|
58 |
return new JsonWriter(writer, JsonWriter.DROP_ROOT_MODE); |
|
59 |
} |
|
60 |
}); |
|
61 |
return xstream; |
|
62 |
} |
|
63 |
|
|
64 |
/** |
|
65 |
* Das Format ermitteln, das dieser Wandler verarbeitet |
|
66 |
* @return das Format, das dieser Wandler verarbeitet |
|
67 |
*/ |
|
68 |
@Override |
|
69 |
public String getFormat() { |
|
70 |
return FORMAT_FLATJSON; |
|
71 |
} |
|
72 |
|
|
73 |
} |