JsonUtils { public static StringUtils.Pair getParentAndLeafPath(String path) { int pos = path.lastIndexOf('.'); int temp = path.lastIndexOf("['"); if (temp != -1 && temp > pos) { pos = temp - 1; } String right = path.substring(pos + 1); if (right.startsWith("[")) { pos = pos + 1; } String left = path.substring(0, pos == -1 ? 0 : pos); return StringUtils.pair(left, right); } private  JsonUtils(); static DocumentContext toJsonDocStrict(String raw); static DocumentContext toJsonDoc(String raw); static String toStrictJsonString(String raw); static String toJson(Object o); static Map removeCyclicReferences(Map map); static DocumentContext toJsonDoc(Object o); static Object fromJson(String s, String className); static T fromJson(String s, Class<T> clazz); static String toPrettyJsonString(DocumentContext doc); static String escapeValue(String raw); static Object nashornObjectToJavaJSON(Object jsObj); static void removeKeysWithNullValues(Object o); static StringUtils.Pair getParentAndLeafPath(String path); static void removeValueByPath(DocumentContext doc, String path); static void setValueByPath(DocumentContext doc, String path, Object value); static void setValueByPath(DocumentContext doc, String path, Object value, boolean remove); static boolean pathExists(DocumentContext doc, String path); static DocumentContext fromYaml(String raw); static DocumentContext fromCsv(String raw); static String toCsv(List<Map<String, Object>> list); static String buildPath(String parentPath, String key); static DocumentContext emptyJsonObject(); static DocumentContext emptyJsonArray(int length);  }
@Test public void testParsingParentAndLeafName() { assertEquals(StringUtils.pair("", "$"), JsonUtils.getParentAndLeafPath("$")); assertEquals(StringUtils.pair("$", "foo"), JsonUtils.getParentAndLeafPath("$.foo")); assertEquals(StringUtils.pair("$", "['foo']"), JsonUtils.getParentAndLeafPath("$['foo']")); assertEquals(StringUtils.pair("$.foo", "bar"), JsonUtils.getParentAndLeafPath("$.foo.bar")); assertEquals(StringUtils.pair("$.foo", "['bar']"), JsonUtils.getParentAndLeafPath("$.foo['bar']")); assertEquals(StringUtils.pair("$.foo", "bar[0]"), JsonUtils.getParentAndLeafPath("$.foo.bar[0]")); assertEquals(StringUtils.pair("$.foo", "['bar'][0]"), JsonUtils.getParentAndLeafPath("$.foo['bar'][0]")); assertEquals(StringUtils.pair("$.foo[2]", "bar[0]"), JsonUtils.getParentAndLeafPath("$.foo[2].bar[0]")); assertEquals(StringUtils.pair("$.foo[2]", "['bar'][0]"), JsonUtils.getParentAndLeafPath("$.foo[2]['bar'][0]")); assertEquals(StringUtils.pair("$.foo[2]", "bar"), JsonUtils.getParentAndLeafPath("$.foo[2].bar")); assertEquals(StringUtils.pair("$.foo[2]", "['bar']"), JsonUtils.getParentAndLeafPath("$.foo[2]['bar']")); }