jodd.json.JsonParser.map()方法的使用及代码示例

x33g5p2x  于2022-01-22 转载在 其他  
字(14.9k)|赞(0)|评价(0)|浏览(127)

本文整理了Java中jodd.json.JsonParser.map()方法的一些代码示例,展示了JsonParser.map()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JsonParser.map()方法的具体详情如下:
包路径:jodd.json.JsonParser
类名称:JsonParser
方法名:map

JsonParser.map介绍

[英]Maps a class to JSONs root.
[中]将类映射到JSONs根。

代码示例

代码示例来源:origin: oblac/jodd

/**
 * Parses input JSON to a list with specified key and value types.
 */
public <K, V> Map<K, V> parseAsMap(
  final String string, final Class<K> keyType, final Class<V> valueType) {
  return new JsonParser()
    .map(JsonParser.KEYS, keyType)
    .map(JsonParser.VALUES, valueType)
    .parse(string);
}

代码示例来源:origin: oblac/jodd

/**
 * Parses input JSON to a list with specified component type.
 */
public <T> List<T> parseAsList(final String string, final Class<T> componentType) {
  return new JsonParser()
    .map(JsonParser.VALUES, componentType)
    .parse(string);
}

代码示例来源:origin: oblac/jodd

@Test
void testKeys() {
  JsonParsers.forEachParser(jsonParser -> {
    String json = "{\"123\" : \"name\"}";
    Map<Long, String> map = jsonParser.map("keys", Long.class).parse(json);
    assertEquals(1, map.size());
    assertEquals("name", map.get(Long.valueOf(123)));
  });
  JsonParsers.forEachParser(jsonParser -> {
    String json = "{\"eee\" : {\"123\" : \"name\"}}";
    Map<String, Map<Long, String>> map2 = jsonParser.map("values.keys", Long.class).parse(json);
    assertEquals(1, map2.size());
    Map<Long, String> map = map2.get("eee");
    assertEquals(1, map.size());
    assertEquals("name", map.get(Long.valueOf(123)));
  });
}

代码示例来源:origin: oblac/jodd

@Test
void testNullDeserialization() {
  JsonParsers.forEachParser(jsonParser -> {
    String input = "{\"property\": null, \"property2\":5, \"property3\":\"abc\"}";
    JsonParser deserializer = jsonParser;
    deserializer.map(null, HashMap.class);
    Map<String, Object> result = deserializer.parse(input);
    assertNotNull(result);
    // fails on this line, because the first property is not deserialized
    assertEquals(3, result.size());
    assertTrue(result.containsKey("property"));
    assertNull(result.get("property"), "the value should be null");
  });
}

代码示例来源:origin: oblac/jodd

@Test
void testCustomMap() {
  JsonParsers.forEachParser(jsonParser -> {
    String json = "{\"userId\" : 123, \"name\": 456}";
    Map<String, Integer> map = jsonParser.parse(json);
    assertEquals(2, map.size());
    assertEquals(Integer.valueOf(123), map.get("userId"));
    assertEquals(Integer.valueOf(456), map.get("name"));
    Map<String, Long> map2 = JsonParser
      .create()
      .map(JsonParser.VALUES, Long.class)
      .parse(json);
    assertEquals(2, map2.size());
    assertEquals(Long.valueOf(123), map2.get("userId"));
    assertEquals(Long.valueOf(456), map2.get("name"));
  });
  JsonParsers.forEachParser(jsonParser -> {
    String json = "{\"123\" : \"hey\", \"456\": \"man\"}";
    Map<Long, String> map3 = jsonParser
      .map(JsonParser.KEYS, Long.class)
      .parse(json);
    assertEquals(2, map3.size());
    assertEquals("hey", map3.get(Long.valueOf(123)));
    assertEquals("man", map3.get(Long.valueOf(456)));
  });
}

代码示例来源:origin: oblac/jodd

@Test
void testMapWithEmbeddedObject2() {
  JsonParsers.forEachParser(jsonParser -> {
    Map<String, Pair<Phone, Network>> complex = jsonParser
      .map("values", Pair.class)
      .map("values.first", Phone.class)
      .map("values.second", Network.class)
      .parse("{\"1\": { \"first\": { \"areaCode\": \"404\" }, \"second\": {\"name\": \"Jodd\"} } }");
    assertNotNull(complex);
    assertEquals(1, complex.size());
    assertTrue(complex.containsKey("1"));
    assertNotNull(complex.get("1"));
    assertEquals(Pair.class, complex.get("1").getClass());
    assertEquals(Phone.class, complex.get("1").getFirst().getClass());
    assertEquals(Network.class, complex.get("1").getSecond().getClass());
    assertEquals("404", complex.get("1").getFirst().getAreaCode());
    assertEquals("Jodd", complex.get("1").getSecond().getName());
  });
}

代码示例来源:origin: oblac/jodd

.map(null, ArrayList.class)
.map("values", Date.class)
.parse(json);

代码示例来源:origin: oblac/jodd

@Test
void testListDeserializationNoClass2() {
  JsonParsers.forEachParser(jsonParser -> {
    Person modesty = creator.createModesty();
    Person igor = creator.createJodder();
    Person pedro = creator.createPedro();
    List<Person> list = new ArrayList<>(3);
    list.add(modesty);
    list.add(igor);
    list.add(pedro);
    String json = new JsonSerializer().serialize(list);
    List<Person> people = jsonParser.map("values", Person.class).parse(json);
    assertEquals(3, list.size());
    assertEquals(modesty.getFirstname(), list.get(0).getFirstname());
    assertEquals(igor.getFirstname(), list.get(1).getFirstname());
    assertEquals(pedro.getFirstname(), list.get(2).getFirstname());
  });
}

代码示例来源:origin: oblac/jodd

@Test
void testParseCatalogAsMap() {
  JsonParsers.forEachParser(jsonParser -> {
    String json = loadJSON("citm_catalog");
    Map catalog = jsonParser
      .map("values.keys", Long.class)
      .map("venueNames.keys", String.class)
      .useAltPaths()
      .parse(json);
    String newJson = new JsonSerializer().deep(true).serialize(catalog);
    Catalog jsonCatalog = jsonParser.parse(newJson, Catalog.class);
    assertCatalog(jsonCatalog);
  });
}

代码示例来源:origin: oblac/jodd

@Test
void testComplexMaps() {
  JsonParsers.forEachParser(jsonParser -> {
    String json = null;
    try {
      json = FileUtil.readString(new File(dataRoot, "complexMaps.json"));
    } catch (IOException e) {
      fail(e);
    }
    User user = jsonParser
      .map("inters.values", InterImpl.class)
      .parse(json, User.class);
    assertNotNull(user);
    assertEquals("Mak", user.getName());
    Map<String, Bar> bars = user.getBars();
    assertEquals(2, bars.size());
    assertEquals(12300, bars.get("123").getAmount().intValue());
    assertEquals(45600, bars.get("456").getAmount().intValue());
    Map<String, Inter> inters = user.getInters();
    assertEquals(3, inters.size());
  });
}

代码示例来源:origin: oblac/jodd

@Test
void testListDeserializationNoClass() {
  JsonParser.Defaults.classMetadataName = "class";
  JsonSerializer.Defaults.classMetadataName = "class";
  JsonParsers.forEachParser(jsonParser -> {
    Person modesty = creator.createModesty();
    Person igor = creator.createJodder();
    Person pedro = creator.createPedro();
    List<Person> list = new ArrayList<>(3);
    list.add(modesty);
    list.add(igor);
    list.add(pedro);
    String json = new JsonSerializer().exclude("*.class").serialize(list);
    List<Person> people = jsonParser.map("values", Person.class).parse(json);
    assertEquals(3, list.size());
    assertEquals(modesty.getFirstname(), list.get(0).getFirstname());
    assertEquals(igor.getFirstname(), list.get(1).getFirstname());
    assertEquals(pedro.getFirstname(), list.get(2).getFirstname());
  });
}

代码示例来源:origin: oblac/jodd

@Test
void testListSerialization() {
  JsonParser.Defaults.classMetadataName = "class";
  JsonSerializer.Defaults.classMetadataName = "class";
  Person modesty = creator.createModesty();
  Person igor = creator.createJodder();
  Person pedro = creator.createPedro();
  List<Person> list = new ArrayList<>(3);
  list.add(modesty);
  list.add(igor);
  list.add(pedro);
  JsonParsers.forEachParser(jsonParser -> {
    String json = new JsonSerializer().serialize(list);
    List<Person> people = jsonParser.parse(json);
    assertTrue(people instanceof List);
    json = new JsonSerializer().exclude("*.class").serialize(list);
    people = jsonParser.map("values", Person.class).parse(json);
    assertEquals(3, people.size());
    assertEquals(Person.class, people.get(0).getClass());
  });
  JsonParsers.forEachParser(jsonParser -> {
    String json = new JsonSerializer().exclude("*.class").serialize(list);
    List<Map> peopleMap = jsonParser.parse(json);
    assertEquals(3, peopleMap.size());
    assertTrue(peopleMap.get(0) instanceof Map);
  });
}

代码示例来源:origin: oblac/jodd

@Test
void testNoClassHints() {
  JsonParsers.forEachParser(jsonParser -> {
    Hero superman = creator.createSuperman();
    String json = new JsonSerializer().exclude("*.class").serialize(superman);
    Hero jsonSuperMan = jsonParser
      .map(Hero.class)
      .map("lair", SecretLair.class)
      .map("secretIdentity", SecretIdentity.class)
      .parse(json);
    assertNotNull(jsonSuperMan);
    assertEquals("Super Man", jsonSuperMan.getName());
    assertNotNull(jsonSuperMan.getIdentity());
    assertEquals("Clark Kent", jsonSuperMan.getIdentity().getName());
    assertNotNull(jsonSuperMan.getLair());
    assertEquals("Fortress of Solitude", jsonSuperMan.getLair().getName());
  });
}

代码示例来源:origin: oblac/jodd

@Test
void testSimpleObject() {
  JsonParsers.forEachParser(jsonParser -> {
    Foo foo = jsonParser
      .map("inter", InterImpl.class)
      .parse(
        "{" +
          "\"aaa\": 123," +
          "\"name\": \"jodd\"," +
          "\"id\": \"173\"," +
          "\"bar\": {" +
          "\"amount\" : \"-23\"" +
          "}," +
          "\"inter\": {" +
          "\"sign\" : \"W\"" +
          "}" +
          "}", Foo.class);
    assertNotNull(foo);
    assertEquals("jodd", foo.name);
    assertEquals(173, foo.getId());
    assertNotNull(foo.bar);
    assertEquals(-23, foo.bar.getAmount().intValue());
  });
}

代码示例来源:origin: oblac/jodd

@Test
void testClassInArrayOrMapParse() {
  final String json = "{\"userId\" : 123, \"name\":\"Joe\"}";
  JsonParsers.forEachParser(jsonParser -> {
    User user = jsonParser.parse(json, User.class);
    assertEquals(123, user.getId());
    assertNull(user.getName());
  });
  JsonParsers.forEachParser(jsonParser -> {
    List<User> users = jsonParser.map(JsonParser.VALUES, User.class).parse("[" + json + "]");
    assertEquals(1, users.size());
    User user = users.get(0);
    assertEquals(123, user.getId());
    assertNull(user.getName());
  });
  JsonParsers.forEachParser(jsonParser -> {
    Map<String, Object> map = jsonParser.map(JsonParser.VALUES, User.class).parse("{ \"user\":" + json + "}");
    assertEquals(1, map.size());
    User user = (User) map.get("user");
    assertEquals(123, user.getId());
    assertNull(user.getName());
  });
  JsonParsers.forEachParser(jsonParser -> {
    UserHolder userHolder = jsonParser.parse("{ \"user\":" + json + "}", UserHolder.class);
    assertNotNull(userHolder);
    User user = userHolder.getUser();
    assertEquals(123, user.getId());
    assertNull(user.getName());
  });
}

代码示例来源:origin: oblac/jodd

@Test
void testNoClassHintsForCollections() {
  JsonParser.Defaults.classMetadataName = "class";
  JsonSerializer.Defaults.classMetadataName = "class";
  JsonParsers.forEachParser(jsonParser -> {
    Hero superman = creator.createSuperman();
    String json = new JsonSerializer()
      .include("powers")        // redudant
      .include("powers.class")
      .withSerializer("powers.class", new SimpleClassnameTransformer())
      .exclude("*.class")
      .serialize(superman);
    int count = StringUtil.count(json, "***");
    assertEquals(4, count);
    json = StringUtil.remove(json, "***");
    Hero jsonSuperMan = jsonParser
      .map("lair", SecretLair.class)
      .map("secretIdentity", SecretIdentity.class)
      .parse(json, Hero.class);
    assertEquals("Fortress of Solitude", jsonSuperMan.getLair().getName());
    assertHeroHasSuperPowers(jsonSuperMan);
  });
}

代码示例来源:origin: oblac/jodd

@Test
void testGenericTypeDeserialization2() {
  JsonParsers.forEachParser(jsonParser -> {
    Pair<Hero, Villian> archenemies = new Pair<>(creator.createSuperman(), creator.createLexLuthor());
    String json = new JsonSerializer()
      .serialize(archenemies);
    Pair<Hero, Villian> deserialArchEnemies = jsonParser
      .map("first", Hero.class)
      .map("second", Villian.class)
      .parse(json, Pair.class);
    assertEquals(archenemies.getFirst().getClass(), deserialArchEnemies.getFirst().getClass());
    assertEquals(archenemies.getSecond().getClass(), deserialArchEnemies.getSecond().getClass());
    assertEquals(archenemies.getFirst().getIdentity(), deserialArchEnemies.getFirst().getIdentity());
    assertEquals(archenemies.getFirst().getLair(), deserialArchEnemies.getFirst().getLair());
    assertEquals(archenemies.getFirst().getName(), deserialArchEnemies.getFirst().getName());
    assertEquals(archenemies.getSecond().getName(), deserialArchEnemies.getSecond().getName());
    assertEquals(archenemies.getSecond().getLair(), deserialArchEnemies.getSecond().getLair());
  });
}

代码示例来源:origin: oblac/jodd

@Test
void testGenericTypeDeserialization() {
  JsonParser.Defaults.classMetadataName = "class";
  JsonSerializer.Defaults.classMetadataName = "class";
  JsonParsers.forEachParser(jsonParser -> {
    Pair<Hero, Villian> archenemies = new Pair<>(creator.createSuperman(), creator.createLexLuthor());
    String json = new JsonSerializer()
      .exclude("*.class")
      .serialize(archenemies);
    Pair<Hero, Villian> deserialArchEnemies = jsonParser
      .map("first", Hero.class)
      .map("second", Villian.class)
      .parse(json, Pair.class);
    assertEquals(archenemies.getFirst().getClass(), deserialArchEnemies.getFirst().getClass());
    assertEquals(archenemies.getSecond().getClass(), deserialArchEnemies.getSecond().getClass());
    assertEquals(archenemies.getFirst().getIdentity(), deserialArchEnemies.getFirst().getIdentity());
    assertEquals(archenemies.getFirst().getLair(), deserialArchEnemies.getFirst().getLair());
    assertEquals(archenemies.getFirst().getName(), deserialArchEnemies.getFirst().getName());
    assertEquals(archenemies.getSecond().getName(), deserialArchEnemies.getSecond().getName());
    assertEquals(archenemies.getSecond().getLair(), deserialArchEnemies.getSecond().getLair());
  });
}

代码示例来源:origin: oblac/jodd

@Test
void testDeserializeWithPath() {
  JsonParsers.forEachParser(jsonParser -> {
    Person igor = creator.createJodder();
    Map map = new HashMap();
    map.put("person", igor);
    String json = new JsonSerializer().serialize(map);
    map = jsonParser.map("values", Person.class).parse(json);
    Person jsonIgor = (Person) map.get("person");
    assertNotNull(jsonIgor);
    assertEquals(igor.getLastname(), jsonIgor.getLastname());
    assertEquals(igor.getFirstname(), jsonIgor.getFirstname());
    assertEquals(igor.getBirthdate(), jsonIgor.getBirthdate());
    assertEquals(igor.getHome().getState(), jsonIgor.getHome().getState());
    assertEquals(igor.getHome().getStreet(), jsonIgor.getHome().getStreet());
    assertEquals(igor.getHome().getCity(), jsonIgor.getHome().getCity());
    assertEquals(igor.getWork().getCity(), jsonIgor.getWork().getCity());
    assertEquals(jsonIgor, jsonIgor.getWork().getPerson());
  });
}

代码示例来源:origin: oblac/jodd

@Test
void testArrayType() {
  JsonParsers.forEachParser(jsonParser -> {
    Person igor = creator.createJodder();
    Person modesty = creator.createModesty();
    Group group = new Group("brothers", igor, modesty);
    String json = new JsonSerializer().include("people").exclude("*.class").serialize(group);
    Group bro = jsonParser.map(Group.class).parse(json);
    assertNotNull(bro);
    assertEquals("brothers", bro.getGroupName());
    assertEquals(2, bro.getPeople().length);
    assertEquals("Igor", bro.getPeople()[0].getFirstname());
    assertEquals("Modesty", bro.getPeople()[1].getFirstname());
  });
}

相关文章