diff --git a/kilo-client/src/main/java/org/httprpc/kilo/io/CSVEncoder.java b/kilo-client/src/main/java/org/httprpc/kilo/io/CSVEncoder.java index 9384bf1d6..ea2f78d5d 100644 --- a/kilo-client/src/main/java/org/httprpc/kilo/io/CSVEncoder.java +++ b/kilo-client/src/main/java/org/httprpc/kilo/io/CSVEncoder.java @@ -162,25 +162,35 @@ private void encode(Iterable> records, Writer writer) t private void encode(Object value, Writer writer) throws IOException { if (value == null) { - writer.write(""); - } else if (value instanceof CharSequence text) { - writer.write('"'); - - for (int i = 0, n = text.length(); i < n; i++) { - var c = text.charAt(i); - - if (c == '"') { - writer.append("\"\""); - } else { - writer.append(c); - } - } + return; + } - writer.write('"'); + if (value instanceof CharSequence text) { + encode(text, writer); } else if (value instanceof Date date) { - writer.write(String.valueOf(date.getTime())); + encode(date, writer); } else { writer.write(value.toString()); } } + + private void encode(CharSequence text, Writer writer) throws IOException { + writer.write('"'); + + for (int i = 0, n = text.length(); i < n; i++) { + var c = text.charAt(i); + + if (c == '"') { + writer.append("\"\""); + } else { + writer.append(c); + } + } + + writer.write('"'); + } + + private void encode(Date date, Writer writer) throws IOException { + writer.write(String.valueOf(date.getTime())); + } } diff --git a/kilo-client/src/main/java/org/httprpc/kilo/io/JSONEncoder.java b/kilo-client/src/main/java/org/httprpc/kilo/io/JSONEncoder.java index 21fbacf8f..46923cc26 100644 --- a/kilo-client/src/main/java/org/httprpc/kilo/io/JSONEncoder.java +++ b/kilo-client/src/main/java/org/httprpc/kilo/io/JSONEncoder.java @@ -64,71 +64,85 @@ private void encode(Object value, Writer writer) throws IOException { if (value == null) { writer.append(null); } else if (value instanceof CharSequence text) { - writer.write("\""); - - for (int i = 0, n = text.length(); i < n; i++) { - var c = text.charAt(i); - - if (c == '"' || c == '\\') { - writer.write("\\" + c); - } else if (c == '\b') { - writer.write("\\b"); - } else if (c == '\f') { - writer.write("\\f"); - } else if (c == '\n') { - writer.write("\\n"); - } else if (c == '\r') { - writer.write("\\r"); - } else if (c == '\t') { - writer.write("\\t"); - } else if (Character.isISOControl(c)) { - writer.write(String.format("\\u%04x", (int)c)); - } else { - writer.write(c); - } - } - - writer.write("\""); + encode(text, writer); } else if (value instanceof Float number) { if (number.isNaN() || number.isInfinite()) { throw new IllegalArgumentException("Invalid float value."); } - writer.write(value.toString()); + encode(number, writer); } else if (value instanceof Double number) { if (number.isNaN() || number.isInfinite()) { throw new IllegalArgumentException("Invalid double value."); } - writer.write(value.toString()); - } else if (value instanceof Number || value instanceof Boolean) { - writer.write(value.toString()); + encode(number, writer); + } else if (value instanceof Number number) { + encode(number, writer); } else if (value instanceof Date date) { - writer.write(String.valueOf(date.getTime())); + encode(date, writer); + } else if (value instanceof Boolean flag) { + encode(flag, writer); } else if (value instanceof Iterable iterable) { - writer.write("["); + encode(iterable, writer); + } else if (value instanceof Map map) { + encode(map, writer); + } else { + encode(value.toString(), writer); + } + } + + private void encode(CharSequence text, Writer writer) throws IOException { + writer.write("\""); + + for (int i = 0, n = text.length(); i < n; i++) { + var c = text.charAt(i); + + if (c == '"' || c == '\\') { + writer.write("\\" + c); + } else if (c == '\b') { + writer.write("\\b"); + } else if (c == '\f') { + writer.write("\\f"); + } else if (c == '\n') { + writer.write("\\n"); + } else if (c == '\r') { + writer.write("\\r"); + } else if (c == '\t') { + writer.write("\\t"); + } else if (Character.isISOControl(c)) { + writer.write(String.format("\\u%04x", (int)c)); + } else { + writer.write(c); + } + } - depth++; + writer.write("\""); + } - var i = 0; + private void encode(Number number, Writer writer) throws IOException { + writer.write(number.toString()); + } - for (var element : iterable) { - if (i > 0) { - writer.write(","); - } + private void encode(Date date, Writer writer) throws IOException { + writer.write(String.valueOf(date.getTime())); + } - if (!compact) { - writer.write("\n"); + private void encode(Boolean flag, Writer writer) throws IOException { + writer.write(flag.toString()); + } - indent(writer); - } + private void encode(Iterable iterable, Writer writer) throws IOException { + writer.write("["); - encode(element, writer); + depth++; - i++; - } + var i = 0; - depth--; + for (var element : iterable) { + if (i > 0) { + writer.write(","); + } if (!compact) { writer.write("\n"); @@ -136,45 +150,39 @@ private void encode(Object value, Writer writer) throws IOException { indent(writer); } - writer.write("]"); - } else if (value instanceof Map map) { - writer.write("{"); - - depth++; + encode(element, writer); - var i = 0; - - for (var entry : map.entrySet()) { - var key = entry.getKey(); + i++; + } - if (!(key instanceof String)) { - throw new IllegalArgumentException("Invalid key."); - } + depth--; - if (i > 0) { - writer.write(","); - } + if (!compact) { + writer.write("\n"); - if (!compact) { - writer.write("\n"); + indent(writer); + } - indent(writer); - } + writer.write("]"); + } - encode(key, writer); + private void encode(Map map, Writer writer) throws IOException { + writer.write("{"); - writer.write(":"); + depth++; - if (!compact) { - writer.write(" "); - } + var i = 0; - encode(entry.getValue(), writer); + for (var entry : map.entrySet()) { + var key = entry.getKey(); - i++; + if (!(key instanceof String)) { + throw new IllegalArgumentException("Invalid key."); } - depth--; + if (i > 0) { + writer.write(","); + } if (!compact) { writer.write("\n"); @@ -182,10 +190,28 @@ private void encode(Object value, Writer writer) throws IOException { indent(writer); } - writer.write("}"); - } else { - encode(value.toString(), writer); + encode(key, writer); + + writer.write(":"); + + if (!compact) { + writer.write(" "); + } + + encode(entry.getValue(), writer); + + i++; } + + depth--; + + if (!compact) { + writer.write("\n"); + + indent(writer); + } + + writer.write("}"); } private void indent(Writer writer) throws IOException {