18
18
import java .io .IOException ;
19
19
import java .io .StringWriter ;
20
20
21
+ import org .apache .commons .lang .StringEscapeUtils ;
21
22
import org .culturegraph .mf .exceptions .MetafactureException ;
22
23
import org .culturegraph .mf .framework .DefaultStreamPipe ;
23
24
import org .culturegraph .mf .framework .ObjectReceiver ;
30
31
import com .fasterxml .jackson .core .JsonGenerationException ;
31
32
import com .fasterxml .jackson .core .JsonGenerator ;
32
33
import com .fasterxml .jackson .core .JsonStreamContext ;
34
+ import com .fasterxml .jackson .core .SerializableString ;
35
+ import com .fasterxml .jackson .core .io .CharacterEscapes ;
36
+ import com .fasterxml .jackson .core .io .SerializedString ;
33
37
34
38
/**
35
39
* Serialises an object as JSON. Records and entities are represented
36
40
* as objects unless their name ends with []. If the name ends with [],
37
41
* an array is created.
38
- *
42
+ *
39
43
* @author Christoph Böhme
44
+ * @author Michael Büchner
40
45
*
41
46
*/
42
47
@ Description ("Serialises an object as JSON" )
@@ -49,7 +54,7 @@ public final class JsonEncoder extends
49
54
50
55
private final JsonGenerator jsonGenerator ;
51
56
private final StringWriter writer = new StringWriter ();
52
-
57
+
53
58
public JsonEncoder () {
54
59
try {
55
60
jsonGenerator = new JsonFactory ().createGenerator (writer );
@@ -58,14 +63,68 @@ public JsonEncoder() {
58
63
throw new MetafactureException (e );
59
64
}
60
65
}
61
-
66
+
67
+ public void setPrettyPrinting (final boolean prettyPrinting ) {
68
+ if (prettyPrinting ) {
69
+ jsonGenerator .useDefaultPrettyPrinter ();
70
+ } else {
71
+ jsonGenerator .setPrettyPrinter (null );
72
+ }
73
+ }
74
+
75
+ public boolean getPrettyPrinting () {
76
+ return jsonGenerator .getPrettyPrinter () != null ;
77
+ }
78
+
79
+ /**
80
+ * By default JSON output does only have escaping where it is strictly
81
+ * necessary. This is recommended in the most cases. Nevertheless it can
82
+ * be sometimes useful to have some more escaping. With this method it is
83
+ * possible to use {@link StringEscapeUtils#escapeJavaScript(String)}.
84
+ *
85
+ * @param escapeCharacters an array which defines which characters should be
86
+ * escaped and how it will be done. See
87
+ * {@link CharacterEscapes}. In most cases this should
88
+ * be null. Use like this:
89
+ * <pre>{@code int[] esc = CharacterEscapes.standardAsciiEscapesForJSON();
90
+ * // and force escaping of a few others:
91
+ * esc['\''] = CharacterEscapes.ESCAPE_STANDARD;
92
+ * JsonEncoder.useEscapeJavaScript(esc);
93
+ * }</pre>
94
+ */
95
+ public void setJavaScriptEscapeChars (final int [] escapeCharacters ) {
96
+
97
+ final CharacterEscapes ce = new CharacterEscapes () {
98
+ private static final long serialVersionUID = 1L ;
99
+
100
+ @ Override
101
+ public int [] getEscapeCodesForAscii () {
102
+ if (escapeCharacters == null ) {
103
+ return CharacterEscapes .standardAsciiEscapesForJSON ();
104
+ }
105
+
106
+ return escapeCharacters ;
107
+ }
108
+
109
+ @ Override
110
+ public SerializableString getEscapeSequence (final int ch ) {
111
+ final String chString = Character .toString ((char ) ch );
112
+ final String jsEscaped = StringEscapeUtils .escapeJavaScript (chString );
113
+ return new SerializedString (jsEscaped );
114
+ }
115
+
116
+ };
117
+
118
+ jsonGenerator .setCharacterEscapes (ce );
119
+ }
120
+
62
121
@ Override
63
122
public void startRecord (final String id ) {
64
123
final StringBuffer buffer = writer .getBuffer ();
65
124
buffer .delete (0 , buffer .length ());
66
125
startGroup (id );
67
126
}
68
-
127
+
69
128
@ Override
70
129
public void endRecord () {
71
130
endGroup ();
@@ -76,17 +135,17 @@ public void endRecord() {
76
135
}
77
136
getReceiver ().process (writer .toString ());
78
137
}
79
-
138
+
80
139
@ Override
81
140
public void startEntity (final String name ) {
82
141
startGroup (name );
83
142
}
84
-
143
+
85
144
@ Override
86
145
public void endEntity () {
87
146
endGroup ();
88
147
}
89
-
148
+
90
149
@ Override
91
150
public void literal (final String name , final String value ) {
92
151
try {
@@ -106,7 +165,7 @@ public void literal(final String name, final String value) {
106
165
throw new MetafactureException (e );
107
166
}
108
167
}
109
-
168
+
110
169
private void startGroup (final String name ) {
111
170
try {
112
171
final JsonStreamContext ctx = jsonGenerator .getOutputContext ();
@@ -128,7 +187,7 @@ private void startGroup(final String name) {
128
187
throw new MetafactureException (e );
129
188
}
130
189
}
131
-
190
+
132
191
private void endGroup () {
133
192
try {
134
193
final JsonStreamContext ctx = jsonGenerator .getOutputContext ();
@@ -144,5 +203,5 @@ private void endGroup() {
144
203
throw new MetafactureException (e );
145
204
}
146
205
}
147
-
206
+
148
207
}
0 commit comments