diff --git a/src/main/java/io/nats/client/support/JsonUtils.java b/src/main/java/io/nats/client/support/JsonUtils.java index cab9b3a6a..1c9143b80 100644 --- a/src/main/java/io/nats/client/support/JsonUtils.java +++ b/src/main/java/io/nats/client/support/JsonUtils.java @@ -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*"; @@ -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; diff --git a/src/test/java/io/nats/client/support/JsonUtilsTests.java b/src/test/java/io/nats/client/support/JsonUtilsTests.java index 378810946..0d5056da5 100644 --- a/src/test/java/io/nats/client/support/JsonUtilsTests.java +++ b/src/test/java/io/nats/client/support/JsonUtilsTests.java @@ -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";