Skip to content

Commit

Permalink
fix bug when reading decimals that start with 0.
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysdh540 committed May 16, 2024
1 parent 18db7ff commit 1a3dd64
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
5 changes: 3 additions & 2 deletions src/main/java/dev/nolij/zson/ZsonParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ private static Number parseNumber(Reader input, char start) throws IOException {
}

case '0' -> {
input.mark(1);
int c = input.read();
if (c == 'x' || c == 'X') {
StringBuilder hexValueBuilder = new StringBuilder();
Expand All @@ -306,6 +307,7 @@ private static Number parseNumber(Reader input, char start) throws IOException {

throw unexpectedEOF();
} else {
input.reset();
return parseDecimal('0', input);
}
}
Expand All @@ -315,8 +317,7 @@ private static Number parseNumber(Reader input, char start) throws IOException {
}

private static Number parseDecimal(char c, Reader input) throws IOException {
StringBuilder stringValueBuilder = new StringBuilder();
stringValueBuilder.append(c);
StringBuilder stringValueBuilder = new StringBuilder().append(c);

int ch;
input.mark(1);
Expand Down
14 changes: 7 additions & 7 deletions src/test/java/ZsonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ public void testReadWrite() {

@Test
public void testInvalidRead() {
assertThrows(IllegalArgumentException.class, () -> {
ZsonParser.parseString("wow look such invalid");
});
assertThrows(IllegalArgumentException.class, () -> ZsonParser.parseString("wow look such invalid"));
}

@Test
Expand Down Expand Up @@ -99,9 +97,11 @@ public void testRead() {

Map<String, ZsonValue> map = ZsonParser.parseString(json);

assertEquals(1, ((List<Object>) map.get("arr").value).get(0));
assertEquals(2, ((List<Object>) map.get("arr").value).get(1));
assertEquals(3, ((List<Object>) map.get("arr").value).get(2));
@SuppressWarnings("unchecked")
List<Object> arr = (List<Object>) map.get("arr").value;
assertEquals(1, arr.get(0));
assertEquals(2, arr.get(1));
assertEquals(3, arr.get(2));

Map<String, ZsonValue> obj = Zson.object(
Zson.entry("a", 1),
Expand All @@ -120,7 +120,7 @@ public void testRead() {
assertEquals(Double.POSITIVE_INFINITY, map.get("inf").value);
assertEquals(Double.NEGATIVE_INFINITY, map.get("neginf").value);
assertTrue(Double.isNaN((Double) map.get("nan").value));
// assertEquals("wow look \na multiline string", map.get("multiline-string").value);
assertEquals("wow look \na multiline string", map.get("multiline-string").value);
}

@Test
Expand Down

0 comments on commit 1a3dd64

Please sign in to comment.