13
13
* See the License for the specific language governing permissions and
14
14
* limitations under the License.
15
15
*/
16
- package org .metafacture .json ;
17
16
18
- import java .io .IOException ;
19
- import java .io .StringWriter ;
17
+ package org .metafacture .json ;
20
18
21
19
import org .metafacture .framework .FluxCommand ;
22
20
import org .metafacture .framework .MetafactureException ;
36
34
import com .fasterxml .jackson .core .io .SerializedString ;
37
35
import com .fasterxml .jackson .core .util .DefaultPrettyPrinter ;
38
36
37
+ import java .io .IOException ;
38
+ import java .io .StringWriter ;
39
+
39
40
/**
40
41
* Serialises an object as JSON. Records and entities are represented
41
42
* as objects unless their name ends with []. If the name ends with [],
@@ -54,14 +55,18 @@ public final class JsonEncoder extends
54
55
55
56
public static final String ARRAY_MARKER = "[]" ;
56
57
58
+ private static final char ESCAPE_CHAR_LOW = 0x20 ;
59
+ private static final char ESCAPE_CHAR_HIGH = 0x7f ;
60
+
57
61
private final JsonGenerator jsonGenerator ;
58
62
private final StringWriter writer = new StringWriter ();
59
63
60
64
public JsonEncoder () {
61
65
try {
62
66
jsonGenerator = new JsonFactory ().createGenerator (writer );
63
67
jsonGenerator .setRootValueSeparator (null );
64
- } catch (final IOException e ) {
68
+ }
69
+ catch (final IOException e ) {
65
70
throw new MetafactureException (e );
66
71
}
67
72
}
@@ -126,7 +131,8 @@ public void endRecord() {
126
131
endGroup ();
127
132
try {
128
133
jsonGenerator .flush ();
129
- } catch (final IOException e ) {
134
+ }
135
+ catch (final IOException e ) {
130
136
throw new MetafactureException (e );
131
137
}
132
138
getReceiver ().process (writer .toString ());
@@ -151,10 +157,12 @@ public void literal(final String name, final String value) {
151
157
}
152
158
if (value == null ) {
153
159
jsonGenerator .writeNull ();
154
- } else {
160
+ }
161
+ else {
155
162
jsonGenerator .writeString (value );
156
163
}
157
- } catch (final JsonGenerationException e ) {
164
+ }
165
+ catch (final JsonGenerationException e ) {
158
166
throw new MetafactureException (e );
159
167
}
160
168
catch (final IOException e ) {
@@ -170,13 +178,15 @@ private void startGroup(final String name) {
170
178
jsonGenerator .writeFieldName (name .substring (0 , name .length () - ARRAY_MARKER .length ()));
171
179
}
172
180
jsonGenerator .writeStartArray ();
173
- } else {
181
+ }
182
+ else {
174
183
if (ctx .inObject ()) {
175
184
jsonGenerator .writeFieldName (name );
176
185
}
177
186
jsonGenerator .writeStartObject ();
178
187
}
179
- } catch (final JsonGenerationException e ) {
188
+ }
189
+ catch (final JsonGenerationException e ) {
180
190
throw new MetafactureException (e );
181
191
}
182
192
catch (final IOException e ) {
@@ -189,45 +199,63 @@ private void endGroup() {
189
199
final JsonStreamContext ctx = jsonGenerator .getOutputContext ();
190
200
if (ctx .inObject ()) {
191
201
jsonGenerator .writeEndObject ();
192
- } else if (ctx .inArray ()) {
202
+ }
203
+ else if (ctx .inArray ()) {
193
204
jsonGenerator .writeEndArray ();
194
205
}
195
- } catch (final JsonGenerationException e ) {
206
+ }
207
+ catch (final JsonGenerationException e ) {
196
208
throw new MetafactureException (e );
197
209
}
198
210
catch (final IOException e ) {
199
211
throw new MetafactureException (e );
200
212
}
201
213
}
202
214
203
- private String escapeChar (char ch ) {
215
+ private String escapeChar (final char ch ) {
204
216
final String namedEscape = namedEscape (ch );
205
- if (namedEscape != null ) {
206
- return namedEscape ;
207
- }
208
- if (ch < 0x20 || 0x7f < ch ) {
209
- return unicodeEscape (ch );
210
- }
211
- return Character .toString (ch );
217
+ return namedEscape != null ? namedEscape : (ch < ESCAPE_CHAR_LOW || ESCAPE_CHAR_HIGH < ch ) ? unicodeEscape (ch ) : Character .toString (ch );
212
218
}
213
219
214
- private String namedEscape (char ch ) {
215
- switch (ch ) {
216
- case '\b' : return "\\ b" ;
217
- case '\n' : return "\\ n" ;
218
- case '\t' : return "\\ t" ;
219
- case '\f' : return "\\ f" ;
220
- case '\r' : return "\\ r" ;
221
- case '\'' : return "\\ '" ;
222
- case '\\' : return "\\ \\ " ;
223
- case '"' : return "\\ \" " ;
224
- case '/' : return "\\ /" ;
220
+ private String namedEscape (final char ch ) {
221
+ final String result ;
222
+
223
+ switch (ch ) {
224
+ case '\b' :
225
+ result = "\\ b" ;
226
+ break ;
227
+ case '\n' :
228
+ result = "\\ n" ;
229
+ break ;
230
+ case '\t' :
231
+ result = "\\ t" ;
232
+ break ;
233
+ case '\f' :
234
+ result = "\\ f" ;
235
+ break ;
236
+ case '\r' :
237
+ result = "\\ r" ;
238
+ break ;
239
+ case '\'' :
240
+ result = "\\ '" ;
241
+ break ;
242
+ case '\\' :
243
+ result = "\\ \\ " ;
244
+ break ;
245
+ case '"' :
246
+ result = "\\ \" " ;
247
+ break ;
248
+ case '/' :
249
+ result = "\\ /" ;
250
+ break ;
225
251
default :
226
- return null ;
252
+ result = null ;
227
253
}
254
+
255
+ return result ;
228
256
}
229
257
230
- private String unicodeEscape (char ch ) {
258
+ private String unicodeEscape (final char ch ) {
231
259
return String .format ("\\ u%4H" , ch ).replace (' ' , '0' );
232
260
}
233
261
0 commit comments