Skip to content

Commit 8cb7a08

Browse files
committed
Adds pretty printing and configurable character escapes to the JsonEncoder.
(cherry picking from commit 0b882fe)
1 parent 6b9b7e1 commit 8cb7a08

File tree

1 file changed

+69
-10
lines changed

1 file changed

+69
-10
lines changed

src/main/java/org/culturegraph/mf/stream/converter/JsonEncoder.java

Lines changed: 69 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.io.IOException;
1919
import java.io.StringWriter;
2020

21+
import org.apache.commons.lang.StringEscapeUtils;
2122
import org.culturegraph.mf.exceptions.MetafactureException;
2223
import org.culturegraph.mf.framework.DefaultStreamPipe;
2324
import org.culturegraph.mf.framework.ObjectReceiver;
@@ -30,13 +31,17 @@
3031
import com.fasterxml.jackson.core.JsonGenerationException;
3132
import com.fasterxml.jackson.core.JsonGenerator;
3233
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;
3337

3438
/**
3539
* Serialises an object as JSON. Records and entities are represented
3640
* as objects unless their name ends with []. If the name ends with [],
3741
* an array is created.
38-
*
42+
*
3943
* @author Christoph Böhme
44+
* @author Michael Büchner
4045
*
4146
*/
4247
@Description("Serialises an object as JSON")
@@ -49,7 +54,7 @@ public final class JsonEncoder extends
4954

5055
private final JsonGenerator jsonGenerator;
5156
private final StringWriter writer = new StringWriter();
52-
57+
5358
public JsonEncoder() {
5459
try {
5560
jsonGenerator = new JsonFactory().createGenerator(writer);
@@ -58,14 +63,68 @@ public JsonEncoder() {
5863
throw new MetafactureException(e);
5964
}
6065
}
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+
62121
@Override
63122
public void startRecord(final String id) {
64123
final StringBuffer buffer = writer.getBuffer();
65124
buffer.delete(0, buffer.length());
66125
startGroup(id);
67126
}
68-
127+
69128
@Override
70129
public void endRecord() {
71130
endGroup();
@@ -76,17 +135,17 @@ public void endRecord() {
76135
}
77136
getReceiver().process(writer.toString());
78137
}
79-
138+
80139
@Override
81140
public void startEntity(final String name) {
82141
startGroup(name);
83142
}
84-
143+
85144
@Override
86145
public void endEntity() {
87146
endGroup();
88147
}
89-
148+
90149
@Override
91150
public void literal(final String name, final String value) {
92151
try {
@@ -106,7 +165,7 @@ public void literal(final String name, final String value) {
106165
throw new MetafactureException(e);
107166
}
108167
}
109-
168+
110169
private void startGroup(final String name) {
111170
try {
112171
final JsonStreamContext ctx = jsonGenerator.getOutputContext();
@@ -128,7 +187,7 @@ private void startGroup(final String name) {
128187
throw new MetafactureException(e);
129188
}
130189
}
131-
190+
132191
private void endGroup() {
133192
try {
134193
final JsonStreamContext ctx = jsonGenerator.getOutputContext();
@@ -144,5 +203,5 @@ private void endGroup() {
144203
throw new MetafactureException(e);
145204
}
146205
}
147-
206+
148207
}

0 commit comments

Comments
 (0)