Skip to content

Commit ca71d19

Browse files
committed
Add support for compact JSON output.
1 parent 5af5aaa commit ca71d19

File tree

3 files changed

+50
-11
lines changed

3 files changed

+50
-11
lines changed

httprpc/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ plugins {
2020
}
2121

2222
group = 'org.httprpc'
23-
version = '5.9.4'
23+
version = '5.9.5'
2424

2525
repositories {
2626
mavenCentral()

httprpc/src/main/java/org/httprpc/WebService.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,11 +512,22 @@ protected String getKey(String name) {
512512
protected void encodeResult(HttpServletRequest request, HttpServletResponse response, Object result) throws IOException {
513513
response.setContentType(String.format("application/json;charset=%s", UTF_8));
514514

515-
JSONEncoder jsonEncoder = new JSONEncoder();
515+
JSONEncoder jsonEncoder = new JSONEncoder(isCompact());
516516

517517
jsonEncoder.writeValue(BeanAdapter.adapt(result), response.getOutputStream());
518518
}
519519

520+
/**
521+
* Enables or disables compact output.
522+
*
523+
* @return
524+
* <tt>true</tt> if the encoded output should be compact; <tt>false</tt>,
525+
* otherwise.
526+
*/
527+
protected boolean isCompact() {
528+
return false;
529+
}
530+
520531
private void describeService(HttpServletRequest request, HttpServletResponse response) throws IOException {
521532
response.setContentType(String.format("text/html;charset=%s", UTF_8));
522533

httprpc/src/main/java/org/httprpc/io/JSONEncoder.java

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,28 @@
2929
* JSON encoder.
3030
*/
3131
public class JSONEncoder {
32+
private boolean compact;
33+
3234
private int depth = 0;
3335

36+
/**
37+
* Constructs a new JSON encoder.
38+
*/
39+
public JSONEncoder() {
40+
this(false);
41+
}
42+
43+
/**
44+
* Constructs a new JSON encoder.
45+
*
46+
* @param compact
47+
* <tt>true</tt> if the encoded output should be compact; <tt>false</tt>,
48+
* otherwise.
49+
*/
50+
public JSONEncoder(boolean compact) {
51+
this.compact = compact;
52+
}
53+
3454
/**
3555
* Writes a value to an output stream.
3656
*
@@ -114,9 +134,11 @@ private void encodeValue(Object value, Writer writer) throws IOException {
114134
writer.append(",");
115135
}
116136

117-
writer.append("\n");
137+
if (!compact) {
138+
writer.append("\n");
118139

119-
indent(writer);
140+
indent(writer);
141+
}
120142

121143
encodeValue(element, writer);
122144

@@ -125,9 +147,11 @@ private void encodeValue(Object value, Writer writer) throws IOException {
125147

126148
depth--;
127149

128-
writer.append("\n");
150+
if (!compact) {
151+
writer.append("\n");
129152

130-
indent(writer);
153+
indent(writer);
154+
}
131155

132156
writer.append("]");
133157
} else if (value instanceof Map<?, ?>) {
@@ -142,15 +166,17 @@ private void encodeValue(Object value, Writer writer) throws IOException {
142166
writer.append(",");
143167
}
144168

145-
writer.append("\n");
146-
147169
Object key = entry.getKey();
148170

149171
if (key == null) {
150172
continue;
151173
}
152174

153-
indent(writer);
175+
if (!compact) {
176+
writer.append("\n");
177+
178+
indent(writer);
179+
}
154180

155181
encodeValue(key.toString(), writer);
156182

@@ -163,9 +189,11 @@ private void encodeValue(Object value, Writer writer) throws IOException {
163189

164190
depth--;
165191

166-
writer.append("\n");
192+
if (!compact) {
193+
writer.append("\n");
167194

168-
indent(writer);
195+
indent(writer);
196+
}
169197

170198
writer.append("}");
171199
} else {

0 commit comments

Comments
 (0)