JSONHelpers { public static JsonType getAttributeType(String json, String attribute) { try { Object o = JsonPath.read(json, attribute); if (o == null) { return JsonType.NULL; } else if (o instanceof String) { return JsonType.STRING; } else if (o instanceof Integer || o instanceof Double) { return JsonType.INTEGER; } else if (o instanceof JSONArray) { return JsonType.ARRAY; } else if (o instanceof LinkedHashMap) { return JsonType.OBJECT; } else if (o instanceof Boolean) { return JsonType.BOOLEAN; } else { LOGGER.info("Unsupported JSON type."); return null; } } catch (InvalidJsonException e) { LOGGER.info("JSON was not valid, e.g. the body was empty.", e); return null; } catch (InvalidPathException e) { LOGGER.info("Could not parse the JSON to get the type of an attribute.", e); return null; } catch (IllegalArgumentException e) { LOGGER.info("Wrong arguments passed.", e); return null; } } private  JSONHelpers(); static String getAttributeValue(String json, String attribute); static JsonType getAttributeType(String json, String attribute);  }
@Test public void shouldGetTheTypeStringCorrectly() { String json = "{\"field\": \"stringggg\"}"; assertEquals(JSONHelpers.getAttributeType(json, "field"), JsonType.STRING); }
@Test public void shouldGetTheTypeNumberCorrectly1() { String json = "{\"field\": 1}"; assertEquals(JSONHelpers.getAttributeType(json, "field"), JsonType.INTEGER); }
@Test public void shouldGetTheTypeNumberCorrectly2() { String json = "{\"field\": 1.2}"; assertEquals(JSONHelpers.getAttributeType(json, "field"), JsonType.INTEGER); }
@Test public void shouldGetTheTypeTrueCorrectly() { String json = "{\"field\": true}"; assertEquals(JSONHelpers.getAttributeType(json, "field"), JsonType.BOOLEAN); }
@Test public void shouldGetTheTypeFalseCorrectly() { String json = "{\"field\": false}"; assertEquals(JSONHelpers.getAttributeType(json, "field"), JsonType.BOOLEAN); }
@Test public void shouldGetTheTypeObjectCorrectly1() { String json = "{\"field\": {}}"; assertEquals(JSONHelpers.getAttributeType(json, "field"), JsonType.OBJECT); }
@Test public void shouldGetTheTypeArrayCorrectly1() { String json = "{\"field\": []}"; assertEquals(JSONHelpers.getAttributeType(json, "field"), JsonType.ARRAY); }
@Test public void shouldGetTheTypeNullCorrectly1() { String json = "{\"field\": null}"; assertEquals(JSONHelpers.getAttributeType(json, "field"), JsonType.NULL); }
@Test public void shouldReturnCorrectTypeIfJSONIsNotStrictJSONOnGetType() { assertThat(JSONHelpers.getAttributeType("{field: Test}", "field"), is(equalTo(JsonType.STRING))); }
@Test public void shouldReturnNullIfJSONIsEmptyOnGetType() { assertNull(JSONHelpers.getAttributeType("", "$.field")); }
@Test public void shouldReturnNullIfJSONIsNotEvenCloseToProperJSONOnGetType() { assertNull(JSONHelpers.getAttributeType("this is not real json", "field")); }
@Test public void shouldReturnNullIfJSONIsInvalidOnGetType() { assertNull(JSONHelpers.getAttributeType("{\"foo\": \"bar\" \"field\": \"Test\"}", "$.field")); }
@Test public void shouldReturnNullIfJSONPathIsInvalidOnGetType() { assertNull(JSONHelpers.getAttributeType(JSON, "!=field")); }