jodd.json.JsonSerializer.include()方法的使用及代码示例

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

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

JsonSerializer.include介绍

[英]Adds include path query.
[中]添加包含路径查询。

代码示例

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

  1. /**
  2. * Adds a list of included path queries.
  3. */
  4. public JsonSerializer include(final String... includes) {
  5. for (String include : includes) {
  6. include(include);
  7. }
  8. return this;
  9. }

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

  1. jsonSerializer.include("array");
  2. jsonSerializer.include("list.name");
  3. jsonSerializer.include("list.value");

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

  1. @Test
  2. void testSimpleShallowWithListInMap() {
  3. JsonSerializer serializer = new JsonSerializer();
  4. Map wrapper = new HashMap();
  5. wrapper.put("name", "Joe Blow");
  6. wrapper.put("people", people);
  7. String peopleJson = serializer.serialize(wrapper);
  8. assertFalse(peopleJson.contains("["));
  9. serializer.include("people.*");
  10. peopleJson = serializer.serialize(wrapper);
  11. assertTrue(peopleJson.contains("["));
  12. }

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

  1. @Test
  2. void testDeserializeWithIncludes() {
  3. JsonParsers.forEachParser(jsonParser -> {
  4. Person igor = creator.createJodder();
  5. String json = new JsonSerializer().include("phones", "hobbies").serialize(igor);
  6. Person jsonIgor = jsonParser.parse(json, Person.class);
  7. assertEquals(2, jsonIgor.getPhones().size());
  8. assertEquals(0, jsonIgor.getHobbies().size());
  9. });
  10. }

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

  1. @Test
  2. void testSettersWithoutGettersAreMissing() {
  3. Friend friend = new Friend("Nugget", "Donkey Rider", "Slim");
  4. String json = new JsonSerializer().include("*").serialize(friend);
  5. assertAttribute("nicknames", json);
  6. assertAttributeMissing("nicknamesAsArray", json);
  7. }

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

  1. @Test
  2. void testAnnotations() {
  3. HashMap<String, TestClass3> map = new HashMap<>();
  4. map.put("String1", new TestClass3());
  5. TestClass2 testElement = new TestClass2();
  6. testElement.setMapOfJustice(map);
  7. String json = new JsonSerializer().serialize(testElement);
  8. assertAttributeMissing("mapOfJustice", json);
  9. assertAttributeMissing("name", json);
  10. assertEquals(-1, json.indexOf("testName2"));
  11. json = new JsonSerializer().include("mapOfJustice").serialize(testElement);
  12. assertAttribute("mapOfJustice", json);
  13. // make sure the name property value is missing! assertAttributeMissing( "name", json )
  14. // conflicts since mapOfJustice contains an object with name in it
  15. assertEquals(-1, json.indexOf("testName2"));
  16. }

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

  1. @Test
  2. void testConvertPersonToMap2() {
  3. Person jodder = new DataCreator().createJodder();
  4. final Map<String, Object> target = new HashMap<>();
  5. JsonContext jsonContext = new JsonSerializer()
  6. .include("phones")
  7. .excludeTypes(Address.class)
  8. .createJsonContext(null);
  9. BeanSerializer beanSerializer = new BeanSerializer(jsonContext, jodder) {
  10. @Override
  11. protected void onSerializableProperty(String propertyName, Class propertyType, Object value) {
  12. target.put(propertyName, value);
  13. }
  14. };
  15. beanSerializer.serialize();
  16. assertEquals(5, target.size());
  17. assertSame(jodder.getBirthdate(), target.get("birthdate"));
  18. assertSame(jodder.getFirstBaseBallGame(), target.get("firstBaseBallGame"));
  19. assertSame(jodder.getLastname(), target.get("lastname"));
  20. assertSame(jodder.getFirstname(), target.get("firstname"));
  21. assertSame(jodder.getPhones(), target.get("phones"));
  22. }

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

  1. @Test
  2. void testWildcards() {
  3. JsonSerializer.Defaults.classMetadataName = "class";
  4. JsonSerializer serializer = new JsonSerializer();
  5. String json = serializer.include("phones").exclude("*.class").serialize(jodder);
  6. assertAttributeMissing("class", json);
  7. assertAttribute("phones", json);
  8. assertAttributeMissing("hobbies", json);
  9. }

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

  1. @Test
  2. void testDeserializeInterfaces2() {
  3. JsonParsers.forEachParser(jsonParser -> {
  4. Hero superman = creator.createSuperman();
  5. String json = new JsonSerializer().include("powers").withClassMetadata(true).serialize(superman);
  6. Hero jsonSuperMan = jsonParser.withClassMetadata(true).parse(json, Hero.class);
  7. assertNotNull(jsonSuperMan);
  8. assertEquals(4, jsonSuperMan.getPowers().size());
  9. assertHeroHasSuperPowers(jsonSuperMan);
  10. });
  11. }

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

  1. @Test
  2. void testTransient() {
  3. TestClass2 testElement = new TestClass2();
  4. String json = new JsonSerializer().serialize(testElement);
  5. assertAttributeMissing("description", json);
  6. json = new JsonSerializer().include("description").serialize(testElement);
  7. assertAttribute("description", json);
  8. }

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

  1. @Test
  2. void testDeserializeInterfaces() {
  3. JsonParsers.forEachParser(jsonParser -> {
  4. Hero superman = creator.createSuperman();
  5. String json = new JsonSerializer().include("powers").setClassMetadataName("class").serialize(superman);
  6. Hero jsonSuperMan = jsonParser.setClassMetadataName("class").parse(json, Hero.class);
  7. assertNotNull(jsonSuperMan);
  8. assertEquals(4, jsonSuperMan.getPowers().size());
  9. assertHeroHasSuperPowers(jsonSuperMan);
  10. });
  11. }

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

  1. @Test
  2. void testNoHintsButClassesForCollection() {
  3. JsonParser.Defaults.classMetadataName = "class";
  4. JsonSerializer.Defaults.classMetadataName = "class";
  5. JsonParsers.forEachParser(jsonParser -> {
  6. Hero superman = creator.createSuperman();
  7. String json = new JsonSerializer()
  8. .exclude("*.class")
  9. .include("powers.class")
  10. .serialize(superman);
  11. Hero jsonSuperMan = jsonParser.parse(json, Hero.class);
  12. assertHeroHasSuperPowers(jsonSuperMan);
  13. });
  14. }

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

  1. @Test
  2. void testDeepSerializationWithIncludeOverrides() {
  3. JsonSerializer serializer = new JsonSerializer();
  4. String peopleJson = serializer.include("people.hobbies").deep(true).serialize(network);
  5. assertAttribute("firstname", peopleJson);
  6. assertStringValue("Igor", peopleJson);
  7. assertAttribute("hobbies", peopleJson);
  8. assertStringValue("read", peopleJson);
  9. assertStringValue("run", peopleJson);
  10. assertStringValue("code", peopleJson);
  11. }

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

  1. @Test
  2. void testSubClassDeserialize() {
  3. JsonParsers.forEachParser(jsonParser -> {
  4. Employee dilbert = creator.createDilbert();
  5. String json = new JsonSerializer().include("phones", "hobbies").serialize(dilbert);
  6. Person jsonDilbert = jsonParser.parse(json, Employee.class);
  7. assertNotNull(jsonDilbert);
  8. assertTrue(jsonDilbert instanceof Employee);
  9. assertEquals(dilbert.getCompany(), ((Employee) jsonDilbert).getCompany());
  10. });
  11. }

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

  1. @Test
  2. void testExclude() {
  3. String json = new JsonSerializer().serialize(jodder);
  4. assertAttribute("firstname", json);
  5. assertAttributeMissing("number", json);
  6. assertAttributeMissing("exchange", json);
  7. assertAttributeMissing("areaCode", json);
  8. json = new JsonSerializer().include("phones").serialize(jodder);
  9. assertAttribute("firstname", json);
  10. assertAttribute("number", json);
  11. assertAttribute("exchange", json);
  12. assertAttribute("areaCode", json);
  13. json = new JsonSerializer().include("phones").exclude("phones.areaCode").serialize(jodder);
  14. assertAttribute("firstname", json);
  15. assertAttribute("number", json);
  16. assertAttribute("exchange", json);
  17. assertAttributeMissing("areaCode", json);
  18. }

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

  1. @Test
  2. void testNoClassHintsForCollections() {
  3. JsonParser.Defaults.classMetadataName = "class";
  4. JsonSerializer.Defaults.classMetadataName = "class";
  5. JsonParsers.forEachParser(jsonParser -> {
  6. Hero superman = creator.createSuperman();
  7. String json = new JsonSerializer()
  8. .include("powers") // redudant
  9. .include("powers.class")
  10. .withSerializer("powers.class", new SimpleClassnameTransformer())
  11. .exclude("*.class")
  12. .serialize(superman);
  13. int count = StringUtil.count(json, "***");
  14. assertEquals(4, count);
  15. json = StringUtil.remove(json, "***");
  16. Hero jsonSuperMan = jsonParser
  17. .map("lair", SecretLair.class)
  18. .map("secretIdentity", SecretIdentity.class)
  19. .parse(json, Hero.class);
  20. assertEquals("Fortress of Solitude", jsonSuperMan.getLair().getName());
  21. assertHeroHasSuperPowers(jsonSuperMan);
  22. });
  23. }

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

  1. @Test
  2. void testDeepIncludes() {
  3. JsonSerializer serializer = new JsonSerializer();
  4. String peopleJson = serializer.include("people.hobbies").serialize(network);
  5. assertAttribute("name", peopleJson);
  6. assertStringValue("My Network", peopleJson);
  7. assertAttribute("firstname", peopleJson);
  8. assertStringValue("Igor", peopleJson);
  9. assertStringValue("Modesty", peopleJson);
  10. assertAttribute("lastname", peopleJson);
  11. assertStringValue("Spasic", peopleJson);
  12. assertAttribute("hobbies", peopleJson);
  13. assertStringValue("read", peopleJson);
  14. }

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

  1. @Test
  2. void testMixedWildcards() {
  3. JsonSerializer serializer = new JsonSerializer();
  4. serializer.include("firstname", "lastname").exclude("*");
  5. String json = serializer.serialize(jodder);
  6. assertAttribute("firstname", json);
  7. assertStringValue("Igor", json);
  8. assertAttribute("lastname", json);
  9. assertStringValue("Spasic", json);
  10. assertAttributeMissing("class", json);
  11. assertAttributeMissing("phones", json);
  12. assertAttributeMissing("birthdate", json);
  13. serializer = new JsonSerializer();
  14. serializer.include("firstname", "lastname", "phones.areaCode", "phones.exchange", "phones.number").exclude("*");
  15. json = serializer.serialize(jodder);
  16. assertAttribute("firstname", json);
  17. assertStringValue("Igor", json);
  18. assertAttribute("lastname", json);
  19. assertStringValue("Spasic", json);
  20. assertAttributeMissing("class", json);
  21. assertAttribute("phones", json);
  22. assertAttributeMissing("birthdate", json);
  23. }

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

  1. @Test
  2. void testSetIncludes() {
  3. JsonSerializer serializer = new JsonSerializer();
  4. serializer.include("people.hobbies", "phones", "home", "people.resume");
  5. assertEquals(4, serializer.rules.totalRules());
  6. assertEquals("[people.hobbies]", serializer.rules.getRule(0).toString());
  7. assertEquals("[phones]", serializer.rules.getRule(1).toString());
  8. assertEquals("[home]", serializer.rules.getRule(2).toString());
  9. assertEquals("[people.resume]", serializer.rules.getRule(3).toString());
  10. }

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

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

相关文章