@SuppressWarnings("resource") @Test public void testReadArray() throws JsonParseException, IOException { ObjectMapper om = new ObjectMapper(); JsonParser jp = om.getFactory().createParser("\"test\""); jp.nextToken(); assertThat(ParserUtil.readArray(jp)).isNull(); jp = om.getFactory().createParser("[1,2,3]"); jp.nextToken(); assertThat(ParserUtil.readArray(jp)).containsExactly(1, 2, 3); jp = om.getFactory().createParser("[[\"k\",\"l\",\"m\"],[\"a\",\"b\",\"c\"]]"); jp.nextToken(); assertThat(ParserUtil.readArray(jp)).containsExactly(Arrays.asList("k", "l", "m"), Arrays.asList("a", "b", "c")); }
@Nullable public static List<Object> readArray(JsonParser jp) throws IOException { if (jp.currentToken() != JsonToken.START_ARRAY) { return null; } List<Object> result = new ArrayList<>(); JsonToken token = jp.nextToken(); while (token != JsonToken.END_ARRAY) { if (token == JsonToken.START_ARRAY) { result.add(readArray(jp)); } else if (token == JsonToken.START_OBJECT) { result.add(readObject(jp)); } else { result.add(getValue(jp)); } token = jp.nextToken(); } return result; }
ParserUtil { @Nullable public static List<Object> readArray(JsonParser jp) throws IOException { if (jp.currentToken() != JsonToken.START_ARRAY) { return null; } List<Object> result = new ArrayList<>(); JsonToken token = jp.nextToken(); while (token != JsonToken.END_ARRAY) { if (token == JsonToken.START_ARRAY) { result.add(readArray(jp)); } else if (token == JsonToken.START_OBJECT) { result.add(readObject(jp)); } else { result.add(getValue(jp)); } token = jp.nextToken(); } return result; } }
ParserUtil { @Nullable public static List<Object> readArray(JsonParser jp) throws IOException { if (jp.currentToken() != JsonToken.START_ARRAY) { return null; } List<Object> result = new ArrayList<>(); JsonToken token = jp.nextToken(); while (token != JsonToken.END_ARRAY) { if (token == JsonToken.START_ARRAY) { result.add(readArray(jp)); } else if (token == JsonToken.START_OBJECT) { result.add(readObject(jp)); } else { result.add(getValue(jp)); } token = jp.nextToken(); } return result; }  }
ParserUtil { @Nullable public static List<Object> readArray(JsonParser jp) throws IOException { if (jp.currentToken() != JsonToken.START_ARRAY) { return null; } List<Object> result = new ArrayList<>(); JsonToken token = jp.nextToken(); while (token != JsonToken.END_ARRAY) { if (token == JsonToken.START_ARRAY) { result.add(readArray(jp)); } else if (token == JsonToken.START_OBJECT) { result.add(readObject(jp)); } else { result.add(getValue(jp)); } token = jp.nextToken(); } return result; }  @Nullable static List<Object> readArray(JsonParser jp); @Nullable static Map<String, Object> readObject(JsonParser jp); }
ParserUtil { @Nullable public static List<Object> readArray(JsonParser jp) throws IOException { if (jp.currentToken() != JsonToken.START_ARRAY) { return null; } List<Object> result = new ArrayList<>(); JsonToken token = jp.nextToken(); while (token != JsonToken.END_ARRAY) { if (token == JsonToken.START_ARRAY) { result.add(readArray(jp)); } else if (token == JsonToken.START_OBJECT) { result.add(readObject(jp)); } else { result.add(getValue(jp)); } token = jp.nextToken(); } return result; }  @Nullable static List<Object> readArray(JsonParser jp); @Nullable static Map<String, Object> readObject(JsonParser jp);  }