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

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

本文整理了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

/**
 * Adds a list of included path queries.
 */
public JsonSerializer include(final String... includes) {
  for (String include : includes) {
    include(include);
  }
  return this;
}

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

jsonSerializer.include("array");
jsonSerializer.include("list.name");
jsonSerializer.include("list.value");

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

@Test
void testSimpleShallowWithListInMap() {
  JsonSerializer serializer = new JsonSerializer();
  Map wrapper = new HashMap();
  wrapper.put("name", "Joe Blow");
  wrapper.put("people", people);
  String peopleJson = serializer.serialize(wrapper);
  assertFalse(peopleJson.contains("["));
  serializer.include("people.*");
  peopleJson = serializer.serialize(wrapper);
  assertTrue(peopleJson.contains("["));
}

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

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

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

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

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

@Test
void testAnnotations() {
  HashMap<String, TestClass3> map = new HashMap<>();
  map.put("String1", new TestClass3());
  TestClass2 testElement = new TestClass2();
  testElement.setMapOfJustice(map);
  String json = new JsonSerializer().serialize(testElement);
  assertAttributeMissing("mapOfJustice", json);
  assertAttributeMissing("name", json);
  assertEquals(-1, json.indexOf("testName2"));
  json = new JsonSerializer().include("mapOfJustice").serialize(testElement);
  assertAttribute("mapOfJustice", json);
  // make sure the name property value is missing!  assertAttributeMissing( "name", json )
  // conflicts since mapOfJustice contains an object with name in it
  assertEquals(-1, json.indexOf("testName2"));
}

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

@Test
void testConvertPersonToMap2() {
  Person jodder = new DataCreator().createJodder();
  final Map<String, Object> target = new HashMap<>();
  JsonContext jsonContext = new JsonSerializer()
      .include("phones")
      .excludeTypes(Address.class)
      .createJsonContext(null);
  BeanSerializer beanSerializer = new BeanSerializer(jsonContext, jodder) {
    @Override
    protected void onSerializableProperty(String propertyName, Class propertyType, Object value) {
      target.put(propertyName, value);
    }
  };
  beanSerializer.serialize();
  assertEquals(5, target.size());
  assertSame(jodder.getBirthdate(), target.get("birthdate"));
  assertSame(jodder.getFirstBaseBallGame(), target.get("firstBaseBallGame"));
  assertSame(jodder.getLastname(), target.get("lastname"));
  assertSame(jodder.getFirstname(), target.get("firstname"));
  assertSame(jodder.getPhones(), target.get("phones"));
}

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

@Test
void testWildcards() {
  JsonSerializer.Defaults.classMetadataName = "class";
  JsonSerializer serializer = new JsonSerializer();
  String json = serializer.include("phones").exclude("*.class").serialize(jodder);
  assertAttributeMissing("class", json);
  assertAttribute("phones", json);
  assertAttributeMissing("hobbies", json);
}

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

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

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

@Test
void testTransient() {
  TestClass2 testElement = new TestClass2();
  String json = new JsonSerializer().serialize(testElement);
  assertAttributeMissing("description", json);
  json = new JsonSerializer().include("description").serialize(testElement);
  assertAttribute("description", json);
}

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

@Test
void testDeserializeInterfaces() {
  JsonParsers.forEachParser(jsonParser -> {
    Hero superman = creator.createSuperman();
    String json = new JsonSerializer().include("powers").setClassMetadataName("class").serialize(superman);
    Hero jsonSuperMan = jsonParser.setClassMetadataName("class").parse(json, Hero.class);
    assertNotNull(jsonSuperMan);
    assertEquals(4, jsonSuperMan.getPowers().size());
    assertHeroHasSuperPowers(jsonSuperMan);
  });
}

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

@Test
void testNoHintsButClassesForCollection() {
  JsonParser.Defaults.classMetadataName = "class";
  JsonSerializer.Defaults.classMetadataName = "class";
  JsonParsers.forEachParser(jsonParser -> {
    Hero superman = creator.createSuperman();
    String json = new JsonSerializer()
      .exclude("*.class")
      .include("powers.class")
      .serialize(superman);
    Hero jsonSuperMan = jsonParser.parse(json, Hero.class);
    assertHeroHasSuperPowers(jsonSuperMan);
  });
}

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

@Test
void testDeepSerializationWithIncludeOverrides() {
  JsonSerializer serializer = new JsonSerializer();
  String peopleJson = serializer.include("people.hobbies").deep(true).serialize(network);
  assertAttribute("firstname", peopleJson);
  assertStringValue("Igor", peopleJson);
  assertAttribute("hobbies", peopleJson);
  assertStringValue("read", peopleJson);
  assertStringValue("run", peopleJson);
  assertStringValue("code", peopleJson);
}

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

@Test
void testSubClassDeserialize() {
  JsonParsers.forEachParser(jsonParser -> {
    Employee dilbert = creator.createDilbert();
    String json = new JsonSerializer().include("phones", "hobbies").serialize(dilbert);
    Person jsonDilbert = jsonParser.parse(json, Employee.class);
    assertNotNull(jsonDilbert);
    assertTrue(jsonDilbert instanceof Employee);
    assertEquals(dilbert.getCompany(), ((Employee) jsonDilbert).getCompany());
  });
}

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

@Test
void testExclude() {
  String json = new JsonSerializer().serialize(jodder);
  assertAttribute("firstname", json);
  assertAttributeMissing("number", json);
  assertAttributeMissing("exchange", json);
  assertAttributeMissing("areaCode", json);
  json = new JsonSerializer().include("phones").serialize(jodder);
  assertAttribute("firstname", json);
  assertAttribute("number", json);
  assertAttribute("exchange", json);
  assertAttribute("areaCode", json);
  json = new JsonSerializer().include("phones").exclude("phones.areaCode").serialize(jodder);
  assertAttribute("firstname", json);
  assertAttribute("number", json);
  assertAttribute("exchange", json);
  assertAttributeMissing("areaCode", json);
}

代码示例来源: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 testDeepIncludes() {
  JsonSerializer serializer = new JsonSerializer();
  String peopleJson = serializer.include("people.hobbies").serialize(network);
  assertAttribute("name", peopleJson);
  assertStringValue("My Network", peopleJson);
  assertAttribute("firstname", peopleJson);
  assertStringValue("Igor", peopleJson);
  assertStringValue("Modesty", peopleJson);
  assertAttribute("lastname", peopleJson);
  assertStringValue("Spasic", peopleJson);
  assertAttribute("hobbies", peopleJson);
  assertStringValue("read", peopleJson);
}

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

@Test
void testMixedWildcards() {
  JsonSerializer serializer = new JsonSerializer();
  serializer.include("firstname", "lastname").exclude("*");
  String json = serializer.serialize(jodder);
  assertAttribute("firstname", json);
  assertStringValue("Igor", json);
  assertAttribute("lastname", json);
  assertStringValue("Spasic", json);
  assertAttributeMissing("class", json);
  assertAttributeMissing("phones", json);
  assertAttributeMissing("birthdate", json);
  serializer = new JsonSerializer();
  serializer.include("firstname", "lastname", "phones.areaCode", "phones.exchange", "phones.number").exclude("*");
  json = serializer.serialize(jodder);
  assertAttribute("firstname", json);
  assertStringValue("Igor", json);
  assertAttribute("lastname", json);
  assertStringValue("Spasic", json);
  assertAttributeMissing("class", json);
  assertAttribute("phones", json);
  assertAttributeMissing("birthdate", json);
}

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

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

代码示例来源: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());
  });
}

相关文章