Skip to content

Commit

Permalink
Ignore empty lines when deserializing ndjson.
Browse files Browse the repository at this point in the history
"The [Json] parser MAY silently ignore empty lines, e.g. \n\n".
https://github.com/ndjson/ndjson-spec#32-parsing

[Bug]
  • Loading branch information
lemmy committed Feb 9, 2023
1 parent 626a6bd commit e17420d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
8 changes: 4 additions & 4 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@

<classpath>
<pathelement location="${tlc}/tla2tools.jar" />
<pathelement location="${lib}/gson-2.8.6.jar" />
<pathelement location="${lib}/jgrapht-core-1.5.1.jar" />
<pathelement location="${lib}/jungrapht-layout-1.4-SNAPSHOT.jar" />
<pathelement location="${lib}/gson-2.8.6.jar" />
<pathelement location="${lib}/jgrapht-core-1.5.1.jar" />
<pathelement location="${lib}/jungrapht-layout-1.4-SNAPSHOT.jar" />
<pathelement location="${lib}/slf4j-api-1.7.30.jar" />
<pathelement location="${lib}/slf4j-nop-1.7.30.jar" />
<pathelement location="${lib}/commons-lang3-3.12.0.jar" />
Expand All @@ -136,7 +136,7 @@

<!-- Test if trace expressions involving the ShiViz module work correctly. -->
<!-- This test expects TLC to report a liveness violation, which is why -->
<!-- failonerror is off. Instead, the ant records TLC's return value in -->
<!-- failonerror is off. Instead, ant records TLC's return value in -->
<!-- shiviz.return.value, which is cheked in the conditional below. -->
<java classname="tlc2.TLC" fork="true" failonerror="false" resultproperty="shiviz.return.value">
<!-- Tell Java to use a garbage collector which makes TLC happy. -->
Expand Down
9 changes: 7 additions & 2 deletions modules/tlc2/overrides/Json.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,13 @@ public static IValue ndDeserialize(final StringValue path) throws IOException {
try (BufferedReader reader = new BufferedReader(new FileReader(new File(path.val.toString())))) {
String line = reader.readLine();
while (line != null) {
JsonElement node = JsonParser.parseString(line);
values.add(getValue(node));
// Ignore empty lines in the newline delimited Json file.
// see https://github.com/ndjson/ndjson-spec#32-parsing
line = line.trim();
if (!"".equals(line)) {
JsonElement node = JsonParser.parseString(line);
values.add(getValue(node));
}
line = reader.readLine();
}
}
Expand Down
7 changes: 7 additions & 0 deletions tests/JsonTests.ndjson
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@


{"a":3}
[[1,2],"look"]
[[{"b":{"c":[4,5,6]}}]]


6 changes: 6 additions & 0 deletions tests/JsonTests.tla
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,10 @@ RoundTrip ==
/\ JsonSerialize("build/json/test.json", output)
/\ output = JsonDeserialize("build/json/test.json")
ASSUME(RoundTrip)

\* Deserialize existing ndjson with trailing white spaces and (empty) newlines.

ASSUME LET input == <<[a |-> 3], <<<<1, 2>>, "look">>, << <<[b |-> [c |-> <<4, 5, 6>>]]>> >> >>
IN input = ndJsonDeserialize("tests/JsonTests.ndjson")

=============================================================================

0 comments on commit e17420d

Please sign in to comment.