@SuppressWarnings({ "resource", "unchecked" }) @Test public void testReadObject() throws JsonParseException, IOException { ObjectMapper om = new ObjectMapper(); JsonParser jp = om.getFactory().createParser("\"test\""); jp.nextToken(); assertThat(ParserUtil.readArray(jp)).isNull(); jp = om.getFactory().createParser("{\"key1\":1,\"key2\":2}"); jp.nextToken(); assertThat(ParserUtil.readObject(jp)).containsOnly(MapEntry.entry("key1", 1), MapEntry.entry("key2", 2)); jp = om.getFactory().createParser("{\"keyA\":1.1,\"keyB\":2.2}"); jp.nextToken(); assertThat(ParserUtil.readObject(jp)).containsOnly(MapEntry.entry("keyA", 1.1), MapEntry.entry("keyB", 2.2)); jp = om.getFactory().createParser("{\"k1\":\"one\",\"k2\":\"two\"}"); jp.nextToken(); assertThat(ParserUtil.readObject(jp)).containsOnly(MapEntry.entry("k1", "one"), MapEntry.entry("k2", "two")); jp = om.getFactory().createParser("{\"k1\":true,\"k2\":false, \"k3\":null}"); jp.nextToken(); assertThat(ParserUtil.readObject(jp)).containsOnly(MapEntry.entry("k1", true), MapEntry.entry("k2", false), MapEntry.entry("k3", null)); jp = om.getFactory().createParser( "{\"o1\":{\"a1\":1,\"a2\":2},\"o2\":{\"b1\":11,\"b2\":22},\"o3\":{\"c1\":111,\"c2\":222}}"); jp.nextToken(); Map<String, Object> m = ParserUtil.readObject(jp); assertThat(m).containsKeys("o1", "o2", "o3"); assertThat((Map<String, Object>) m.get("o1")) .containsOnly(MapEntry.entry("a1", 1), MapEntry.entry("a2", 2)); assertThat((Map<String, Object>) m.get("o2")) .containsOnly(MapEntry.entry("b1", 11), MapEntry.entry("b2", 22)); assertThat((Map<String, Object>) m.get("o3")) .containsOnly(MapEntry.entry("c1", 111), MapEntry.entry("c2", 222)); }
@Nullable public static Map<String, Object> readObject(JsonParser jp) throws IOException { if (jp.currentToken() != JsonToken.START_OBJECT) { return null; } Map<String, Object> result = new HashMap<>(); JsonToken token = jp.nextToken(); while (token != JsonToken.END_OBJECT) { String key = jp.getText(); token = jp.nextToken(); if (token == JsonToken.START_ARRAY) { result.put(key, readArray(jp)); } else if (token == JsonToken.START_OBJECT) { result.put(key, readObject(jp)); } else { result.put(key, getValue(jp)); } token = jp.nextToken(); } return result; }
ParserUtil { @Nullable public static Map<String, Object> readObject(JsonParser jp) throws IOException { if (jp.currentToken() != JsonToken.START_OBJECT) { return null; } Map<String, Object> result = new HashMap<>(); JsonToken token = jp.nextToken(); while (token != JsonToken.END_OBJECT) { String key = jp.getText(); token = jp.nextToken(); if (token == JsonToken.START_ARRAY) { result.put(key, readArray(jp)); } else if (token == JsonToken.START_OBJECT) { result.put(key, readObject(jp)); } else { result.put(key, getValue(jp)); } token = jp.nextToken(); } return result; } }
ParserUtil { @Nullable public static Map<String, Object> readObject(JsonParser jp) throws IOException { if (jp.currentToken() != JsonToken.START_OBJECT) { return null; } Map<String, Object> result = new HashMap<>(); JsonToken token = jp.nextToken(); while (token != JsonToken.END_OBJECT) { String key = jp.getText(); token = jp.nextToken(); if (token == JsonToken.START_ARRAY) { result.put(key, readArray(jp)); } else if (token == JsonToken.START_OBJECT) { result.put(key, readObject(jp)); } else { result.put(key, getValue(jp)); } token = jp.nextToken(); } return result; }  }
ParserUtil { @Nullable public static Map<String, Object> readObject(JsonParser jp) throws IOException { if (jp.currentToken() != JsonToken.START_OBJECT) { return null; } Map<String, Object> result = new HashMap<>(); JsonToken token = jp.nextToken(); while (token != JsonToken.END_OBJECT) { String key = jp.getText(); token = jp.nextToken(); if (token == JsonToken.START_ARRAY) { result.put(key, readArray(jp)); } else if (token == JsonToken.START_OBJECT) { result.put(key, readObject(jp)); } else { result.put(key, 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 Map<String, Object> readObject(JsonParser jp) throws IOException { if (jp.currentToken() != JsonToken.START_OBJECT) { return null; } Map<String, Object> result = new HashMap<>(); JsonToken token = jp.nextToken(); while (token != JsonToken.END_OBJECT) { String key = jp.getText(); token = jp.nextToken(); if (token == JsonToken.START_ARRAY) { result.put(key, readArray(jp)); } else if (token == JsonToken.START_OBJECT) { result.put(key, readObject(jp)); } else { result.put(key, getValue(jp)); } token = jp.nextToken(); } return result; }  @Nullable static List<Object> readArray(JsonParser jp); @Nullable static Map<String, Object> readObject(JsonParser jp);  }