Skip to content

Commit

Permalink
Update CSV and JSON encoders.
Browse files Browse the repository at this point in the history
  • Loading branch information
gk-brown committed Oct 13, 2024
1 parent 6b50ef5 commit d9adf73
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 90 deletions.
40 changes: 25 additions & 15 deletions kilo-client/src/main/java/org/httprpc/kilo/io/CSVEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,25 +162,35 @@ private void encode(Iterable<? extends Map<String, ?>> 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()));
}
}
176 changes: 101 additions & 75 deletions kilo-client/src/main/java/org/httprpc/kilo/io/JSONEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,128 +64,154 @@ 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");

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");

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 {
Expand Down

0 comments on commit d9adf73

Please sign in to comment.