@Test void fooBarSecondItemFromArray() { assertEquals( JsonPathParser.parse("foo.bar[2]"), Arrays.asList(new PathObjectOrField("foo"), new PathArray("bar", 2)) ); }
public static List<PathElement> parse(String path) { int elemIdx = 0; int idx = 0; int len = path.length(); ArrayList ret = new ArrayList<PathElement>(); while (idx < len) { char ch = path.charAt(idx); idx += 1; if (ch == '.') { String first = path.substring(elemIdx, idx - 1); elemIdx = idx; ret.add(new PathObjectOrField(first)); } else if (ch == '[') { int arrayIdxStart = idx; Integer out = null; while (idx < len && out == null) { int arrayIdxCh = path.charAt(idx); if (arrayIdxCh == ']') { String arrayIdxStr = path.substring(arrayIdxStart, idx); out = Integer.parseInt(arrayIdxStr); } else if (!(arrayIdxCh >= '0' && arrayIdxCh <= '9')) { throw new IllegalArgumentException("Found unexpected non-digit in middle of array index at " + debugPos(idx)); } idx += 1; } if (out == null) { throw new IllegalArgumentException("Could not find ']' to complete array index"); } String first = path.substring(elemIdx, arrayIdxStart - 1); elemIdx = idx; ret.add(new PathArray(first, out)); if (idx < len && path.charAt(idx) == '.') { idx += 1; elemIdx = idx; } } } if (idx != elemIdx) { String first = path.substring(elemIdx, idx); ret.add(new PathObjectOrField(first)); } return Collections.unmodifiableList(ret); }
JsonPathParser { public static List<PathElement> parse(String path) { int elemIdx = 0; int idx = 0; int len = path.length(); ArrayList ret = new ArrayList<PathElement>(); while (idx < len) { char ch = path.charAt(idx); idx += 1; if (ch == '.') { String first = path.substring(elemIdx, idx - 1); elemIdx = idx; ret.add(new PathObjectOrField(first)); } else if (ch == '[') { int arrayIdxStart = idx; Integer out = null; while (idx < len && out == null) { int arrayIdxCh = path.charAt(idx); if (arrayIdxCh == ']') { String arrayIdxStr = path.substring(arrayIdxStart, idx); out = Integer.parseInt(arrayIdxStr); } else if (!(arrayIdxCh >= '0' && arrayIdxCh <= '9')) { throw new IllegalArgumentException("Found unexpected non-digit in middle of array index at " + debugPos(idx)); } idx += 1; } if (out == null) { throw new IllegalArgumentException("Could not find ']' to complete array index"); } String first = path.substring(elemIdx, arrayIdxStart - 1); elemIdx = idx; ret.add(new PathArray(first, out)); if (idx < len && path.charAt(idx) == '.') { idx += 1; elemIdx = idx; } } } if (idx != elemIdx) { String first = path.substring(elemIdx, idx); ret.add(new PathObjectOrField(first)); } return Collections.unmodifiableList(ret); } }
JsonPathParser { public static List<PathElement> parse(String path) { int elemIdx = 0; int idx = 0; int len = path.length(); ArrayList ret = new ArrayList<PathElement>(); while (idx < len) { char ch = path.charAt(idx); idx += 1; if (ch == '.') { String first = path.substring(elemIdx, idx - 1); elemIdx = idx; ret.add(new PathObjectOrField(first)); } else if (ch == '[') { int arrayIdxStart = idx; Integer out = null; while (idx < len && out == null) { int arrayIdxCh = path.charAt(idx); if (arrayIdxCh == ']') { String arrayIdxStr = path.substring(arrayIdxStart, idx); out = Integer.parseInt(arrayIdxStr); } else if (!(arrayIdxCh >= '0' && arrayIdxCh <= '9')) { throw new IllegalArgumentException("Found unexpected non-digit in middle of array index at " + debugPos(idx)); } idx += 1; } if (out == null) { throw new IllegalArgumentException("Could not find ']' to complete array index"); } String first = path.substring(elemIdx, arrayIdxStart - 1); elemIdx = idx; ret.add(new PathArray(first, out)); if (idx < len && path.charAt(idx) == '.') { idx += 1; elemIdx = idx; } } } if (idx != elemIdx) { String first = path.substring(elemIdx, idx); ret.add(new PathObjectOrField(first)); } return Collections.unmodifiableList(ret); } private  JsonPathParser(); }
JsonPathParser { public static List<PathElement> parse(String path) { int elemIdx = 0; int idx = 0; int len = path.length(); ArrayList ret = new ArrayList<PathElement>(); while (idx < len) { char ch = path.charAt(idx); idx += 1; if (ch == '.') { String first = path.substring(elemIdx, idx - 1); elemIdx = idx; ret.add(new PathObjectOrField(first)); } else if (ch == '[') { int arrayIdxStart = idx; Integer out = null; while (idx < len && out == null) { int arrayIdxCh = path.charAt(idx); if (arrayIdxCh == ']') { String arrayIdxStr = path.substring(arrayIdxStart, idx); out = Integer.parseInt(arrayIdxStr); } else if (!(arrayIdxCh >= '0' && arrayIdxCh <= '9')) { throw new IllegalArgumentException("Found unexpected non-digit in middle of array index at " + debugPos(idx)); } idx += 1; } if (out == null) { throw new IllegalArgumentException("Could not find ']' to complete array index"); } String first = path.substring(elemIdx, arrayIdxStart - 1); elemIdx = idx; ret.add(new PathArray(first, out)); if (idx < len && path.charAt(idx) == '.') { idx += 1; elemIdx = idx; } } } if (idx != elemIdx) { String first = path.substring(elemIdx, idx); ret.add(new PathObjectOrField(first)); } return Collections.unmodifiableList(ret); } private  JsonPathParser(); static List<PathElement> parse(String path); }
JsonPathParser { public static List<PathElement> parse(String path) { int elemIdx = 0; int idx = 0; int len = path.length(); ArrayList ret = new ArrayList<PathElement>(); while (idx < len) { char ch = path.charAt(idx); idx += 1; if (ch == '.') { String first = path.substring(elemIdx, idx - 1); elemIdx = idx; ret.add(new PathObjectOrField(first)); } else if (ch == '[') { int arrayIdxStart = idx; Integer out = null; while (idx < len && out == null) { int arrayIdxCh = path.charAt(idx); if (arrayIdxCh == ']') { String arrayIdxStr = path.substring(arrayIdxStart, idx); out = Integer.parseInt(arrayIdxStr); } else if (!(arrayIdxCh >= '0' && arrayIdxCh <= '9')) { throw new IllegalArgumentException("Found unexpected non-digit in middle of array index at " + debugPos(idx)); } idx += 1; } if (out == null) { throw new IllegalArgumentException("Could not find ']' to complete array index"); } String first = path.substring(elemIdx, arrayIdxStart - 1); elemIdx = idx; ret.add(new PathArray(first, out)); if (idx < len && path.charAt(idx) == '.') { idx += 1; elemIdx = idx; } } } if (idx != elemIdx) { String first = path.substring(elemIdx, idx); ret.add(new PathObjectOrField(first)); } return Collections.unmodifiableList(ret); } private  JsonPathParser(); static List<PathElement> parse(String path);  }