Skip to content

Commit

Permalink
Merge pull request #243 from itchyny/fix-csv-tsv-nan
Browse files Browse the repository at this point in the history
Skip writing NaN on csv and tsv formats
  • Loading branch information
eiiches authored Dec 4, 2022
2 parents 7f547fa + 02f52f1 commit 749ed81
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void apply(final Scope scope, final List<Expression> args, final JsonNode

if (col.isTextual()) {
appendEscaped(row, col.asText());
} else if (col.isNull()) {
} else if (col.isNull() || col.isNumber() && Double.isNaN(col.asDouble())) {
// empty
} else if (col.isBoolean() || col.isNumber()) {
try {
Expand All @@ -53,4 +53,4 @@ public void apply(final Scope scope, final List<Expression> args, final JsonNode

output.emit(TextNode.valueOf(row.toString()), null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public IsNanFunction() {
}

private static boolean test(final JsonNode value) {
return (value.isDouble() || value.isFloat()) && Double.isNaN(value.asDouble());
return value.isNumber() && Double.isNaN(value.asDouble());
}
}
4 changes: 4 additions & 0 deletions jackson-jq/src/test/resources/tests/functions/@csv.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
out:
- "0"

- q: '[0,null,nan,infinite] | @csv'
out:
- "0,,,1.7976931348623157e+308"

- q: '[[range(0; 128)] | implode] | @csv'
out:
- "\"\\0\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\t\n\u000b\f\r\u000e\u000f\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f !\"\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\u007f\""
4 changes: 4 additions & 0 deletions jackson-jq/src/test/resources/tests/functions/@tsv.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
out:
- "0"

- q: '[0,null,nan,infinite] | @tsv'
out:
- "0\t\t\t1.7976931348623157e+308"

- q: '[[range(0; 128)] | implode] | @tsv'
out:
- "\\0\u0001\u0002\u0003\u0004\u0005\u0006\u0007\b\\t\\n\u000b\f\\r\u000e\u000f\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\u007f"

0 comments on commit 749ed81

Please sign in to comment.