Skip to content

Commit

Permalink
handle unsigned long values (#506)
Browse files Browse the repository at this point in the history
  • Loading branch information
scottf authored Jul 1, 2021
1 parent 9a2a882 commit c5490f8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/main/java/io/nats/client/support/JsonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public abstract class JsonUtils {

private static final String STRING_RE = "\\s*\"(.+?)\"";
private static final String BOOLEAN_RE = "\\s*(true|false)";
private static final String NUMBER_RE = "\\s*(\\d+)";
private static final String NUMBER_RE = "\\s*(-?\\d+)";
private static final String STRING_ARRAY_RE = "\\s*\\[\\s*(\".+?\")\\s*\\]";
private static final String BEFORE_FIELD_RE = "\"";
private static final String AFTER_FIELD_RE = "\"\\s*:\\s*";
Expand Down Expand Up @@ -413,16 +413,38 @@ public static void readInt(String json, Pattern pattern, IntConsumer c) {

public static long readLong(String json, Pattern pattern, long dflt) {
Matcher m = pattern.matcher(json);
return m.find() ? Long.parseLong(m.group(1)) : dflt;
return m.find() ? safeParseLong(m.group(1), dflt) : dflt;
}

public static void readLong(String json, Pattern pattern, LongConsumer c) {
Matcher m = pattern.matcher(json);
if (m.find()) {
c.accept(Long.parseLong(m.group(1)));
Long l = safeParseLong(m.group(1));
if (l != null) {
c.accept(l);
}
}
}

public static Long safeParseLong(String s) {
try {
return Long.parseLong(s);
}
catch (Exception e1) {
try {
return Long.parseUnsignedLong(s);
}
catch (Exception e2) {
return null;
}
}
}

public static long safeParseLong(String s, long dflt) {
Long l = safeParseLong(s);
return l == null ? dflt : l;
}

public static ZonedDateTime readDate(String json, Pattern pattern) {
Matcher m = pattern.matcher(json);
return m.find() ? DateTimeUtils.parseDateTime(m.group(1)) : null;
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/io/nats/client/support/JsonUtilsTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,24 @@ public void testParseDateTime() {
assertEquals(-62135596800L, DateTimeUtils.parseDateTime("anything-not-valid").toEpochSecond());
}

@Test
public void testInteger() {
Pattern RE = JsonUtils.buildPattern("num", JsonUtils.FieldType.jsonNumber);
assertEquals(-1, JsonUtils.readLong("\"num\":-1", RE, 0));
assertEquals(12345678, JsonUtils.readLong("\"num\":12345678", RE, 0));
assertEquals(2147483647, JsonUtils.readLong("\"num\":2147483647", RE, 0));
}

@Test
public void testLong() {
Pattern RE = JsonUtils.buildPattern("num", JsonUtils.FieldType.jsonNumber);
assertEquals(-1, JsonUtils.readLong("\"num\":-1", RE, 0));
assertEquals(12345678, JsonUtils.readLong("\"num\":12345678", RE, 0));
assertEquals(9223372036854775807L, JsonUtils.readLong("\"num\":9223372036854775807", RE, 0));
assertEquals(-1, JsonUtils.readLong("\"num\":18446744073709551615", RE, 0));
assertEquals(-2, JsonUtils.readLong("\"num\":18446744073709551614", RE, 0));
}

@Test
public void testReads() {
String json = "\"yes\": true, \"no\": false";
Expand Down

0 comments on commit c5490f8

Please sign in to comment.