Extend SimpleJsonParser to try Long and Double format
This commit is contained in:
parent
9f241a9e82
commit
1531329da0
|
@ -66,7 +66,6 @@ public class SimpleJsonParser implements JsonParser {
|
|||
private List<Object> parseListInternal(String json) {
|
||||
List<Object> list = new ArrayList<Object>();
|
||||
json = trimLeadingCharacter(trimTrailingCharacter(json, ']'), '[');
|
||||
;
|
||||
for (String value : tokenize(json)) {
|
||||
list.add(parseInternal(value));
|
||||
}
|
||||
|
@ -83,6 +82,18 @@ public class SimpleJsonParser implements JsonParser {
|
|||
if (json.startsWith("\"")) {
|
||||
return trimTrailingCharacter(trimLeadingCharacter(json, '"'), '"');
|
||||
}
|
||||
try {
|
||||
return Long.valueOf(json);
|
||||
}
|
||||
catch (NumberFormatException e) {
|
||||
// ignore
|
||||
}
|
||||
try {
|
||||
return Double.valueOf(json);
|
||||
}
|
||||
catch (NumberFormatException e) {
|
||||
// ignore
|
||||
}
|
||||
return json;
|
||||
}
|
||||
|
||||
|
|
|
@ -41,7 +41,15 @@ public class SimpleJsonParserTests {
|
|||
Map<String, Object> map = this.parser.parseMap("{\"foo\":\"bar\",\"spam\":1}");
|
||||
assertEquals(2, map.size());
|
||||
assertEquals("bar", map.get("foo"));
|
||||
assertEquals("1", map.get("spam").toString());
|
||||
assertEquals(1L, ((Number) map.get("spam")).longValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoubleValie() {
|
||||
Map<String, Object> map = this.parser.parseMap("{\"foo\":\"bar\",\"spam\":1.23}");
|
||||
assertEquals(2, map.size());
|
||||
assertEquals("bar", map.get("foo"));
|
||||
assertEquals(1.23d, map.get("spam"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
Loading…
Reference in New Issue