public abstract Object getField(Object json, String field);
/**
- * @return String value of specified field from specified JSON object,
+ * @return value of a string field from specified JSON object,
+ * or string representation of a numeric field,
* or <code>null</code> if field does not exist
*/
public abstract String getString(Object json, String field);
public static String getString(Map obj, String field) {
Object value = obj.get(field);
- if (value != null) {
- return (String) value;
+ if (value instanceof String || value instanceof Number) {
+ return value.toString();
}
return null;
}
dependency = new DependencyUnmarshaller().toModel(loadFile("/dependencies/single.json"));
assertThat(dependency.getId(), is("1649"));
+ assertThat(dependency.getFromId(), is(33L));
assertThat(dependency.getFromKey(), is("org.apache.shiro:shiro-core:org.apache.shiro.authc.pam"));
+ assertThat(dependency.getToId(), is(45L));
assertThat(dependency.getToKey(), is("org.apache.shiro:shiro-core:org.apache.shiro.realm"));
assertThat(dependency.getUsage(), is("USES"));
assertThat(dependency.getWeight(), is(5));
assertThat(JsonUtils.getString(obj, "three"), nullValue());
}
+ @Test
+ public void getNumberAsString() {
+ JSONObject obj = (JSONObject) JSONValue.parse("{\"one\": 1, \"two\": 2}");
+ assertThat(JsonUtils.getString(obj, "one"), is("1"));
+ assertThat(JsonUtils.getString(obj, "two"), is("2"));
+ }
+
@Test
public void getDateField() {
JSONObject obj = (JSONObject) JSONValue.parse("{\"foo\": \"2009-12-25\", \"two\": \"2\"}");
[
{
- "id":"1649",
+ "id": 1649,
"fi": 33,
"ti": 45,
"fk":"org.apache.shiro:shiro-core:org.apache.shiro.authc.pam",
"w":5
},
{
- "id":"1686",
+ "id": 1686,
"fi": 333,
"ti": 453,
"fk":"org.apache.shiro:shiro-core:org.apache.shiro.realm",
"w":3
},
{
- "id":"1690",
+ "id": 1690,
"fi": 334,
"ti": 454,
"fk":"org.apache.shiro:shiro-core:org.apache.shiro.realm",
"w":2
},
{
- "id":"1693",
+ "id": 1693,
"fi": 335,
"ti": 455,
"fk":"org.apache.shiro:shiro-core:org.apache.shiro.realm",
"w":3
},
{
- "id":"1697",
+ "id": 1697,
"fi": 336,
"ti": 456,
"fk":"org.apache.shiro:shiro-core:org.apache.shiro.realm",
"w":16
},
{
- "id":"1714",
+ "id": 1714,
"fi": 337,
"ti": 457,
"fk":"org.apache.shiro:shiro-core:org.apache.shiro.realm",
"w":6
},
{
- "id":"1721",
+ "id": 1721,
"fi": 3338,
"ti": 4258,
"fk":"org.apache.shiro:shiro-core:org.apache.shiro.realm",
"w":9
},
{
- "id":"1731",
+ "id": 1731,
"fi": 343,
"ti": 4445,
"fk":"org.apache.shiro:shiro-core:org.apache.shiro.realm",
"w":3
},
{
- "id":"1735",
+ "id": 1735,
"fi": 3443,
"ti": 455,
"fk":"org.apache.shiro:shiro-core:org.apache.shiro.realm.ldap",
"w":1
},
{
- "id":"1775",
+ "id": 1775,
"fi": 373,
"ti": 6645,
"fk":"org.apache.shiro:shiro-core:org.apache.shiro.mgt",
"w":2
},
{
- "id":"1886",
+ "id": 1886,
"fi": 339,
"ti": 495,
"fk":"org.apache.shiro:shiro-core:org.apache.shiro.realm.jdbc",
"w":1
},
{
- "id":"1962",
+ "id": 1962,
"fi": 373,
"ti": 485,
"fk":"org.apache.shiro:shiro-core:org.apache.shiro.config",
"w":4
},
{
- "id":"1995",
+ "id": 1995,
"fi": 363,
"ti": 445,
"fk":"org.apache.shiro:shiro-core:org.apache.shiro.realm.jndi",
"w":2
},
{
- "id":"2098",
+ "id": 2098,
"fi": 333,
"ti": 415,
"fk":"org.apache.shiro:shiro-core:org.apache.shiro.realm.text",
"w":1
},
{
- "id":"2119",
+ "id": 2119,
"fi": 332,
"ti": 451,
"fk":"org.apache.shiro:shiro-core:org.apache.shiro.authz",
[
{
- "id":"1649",
+ "id": 1649,
"fi": 33,
"ti": 45,
"fk":"org.apache.shiro:shiro-core:org.apache.shiro.authc.pam",