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

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

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

JsonSerializer.exclude介绍

[英]Adds exclude path query.
[中]添加排除路径查询。

代码示例

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

/**
 * Adds a list of excluded path queries.
 */
public JsonSerializer exclude(final String... excludes) {
  for (String exclude : excludes) {
    exclude(exclude);
  }
  return this;
}

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

json = new JsonSerializer().exclude("split").include("phones").excludeTypes("*Stream").serialize(surfer);
assertAttribute("exchange", json);
json = new JsonSerializer().exclude("split").include("phones").exclude("phones.exchange").serialize(surfer);

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

@Test
void testGeneralMapDeserialization() {
  JsonParser.Defaults.classMetadataName = "class";
  JsonSerializer.Defaults.classMetadataName = "class";
  JsonParsers.forEachParser(jsonParser -> {
    String json = new JsonSerializer().exclude("*.class").serialize(creator.createJodder());
    Map<String, Object> deserialized = jsonParser.parse(json);
    assertEquals("Igor", deserialized.get("firstname"));
    assertEquals("Spasic", deserialized.get("lastname"));
    assertTrue(Map.class.isAssignableFrom(deserialized.get("work").getClass()));
    assertTrue(Map.class.isAssignableFrom(deserialized.get("home").getClass()));
  });
}

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

jsonSerializer.exclude("list");        // not applied
jsonSerializer.include("array");

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

@Test
void testExcludeAll() {
  JsonSerializer serializer = new JsonSerializer();
  String json = serializer.exclude("*").serialize(jodder);
  assertEquals("{}", json);
}

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

@Test
void testDates() {
  JsonSerializer serializer = new JsonSerializer();
  String peopleJson = serializer.exclude("home", "work").serialize(jodder);
  assertAttribute("firstname", peopleJson);
  assertStringValue("Igor", peopleJson);
  assertNumber(jodder.getBirthdate().getTime(), peopleJson);
  assertStringValueMissing("java.util.Date", peopleJson);
}

代码示例来源: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 testExcludingPublicFields() {
  Spiderman spiderman = new Spiderman();
  String json = new JsonSerializer().exclude("superpower").serialize(spiderman);
  assertAttributeMissing("superpower", json);
  assertAttribute("spideySense", json);
}

代码示例来源: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 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 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 testDeepSerializationWithExcludes() {
  JsonSerializer serializer = new JsonSerializer();
  String peopleJson = serializer.exclude("people.work").deep(true).serialize(network);
  assertAttribute("firstname", peopleJson);
  assertStringValue("Igor", peopleJson);
  assertAttributeMissing("work", peopleJson);
  assertStringValue("173 Hackers Drive", peopleJson);
  assertAttribute("home", peopleJson);
  assertAttribute("phones", peopleJson);
}

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

@Test
void testListOfObjects() {
  JsonSerializer.Defaults.classMetadataName = "class";
  JsonSerializer serializer = new JsonSerializer();
  String peopleJson = serializer.serialize(people);
  assertStringValue(Person.class.getName(), peopleJson);
  assertAttribute("firstname", peopleJson);
  assertStringValue("Igor", peopleJson);
  assertStringValue("Modesty", peopleJson);
  assertAttribute("lastname", peopleJson);
  assertStringValue("Spasic", peopleJson);
  assertStringValue(Address.class.getName(), peopleJson);
  assertStringValue("Pedro", peopleJson);
  assertStringValue("Sanchez", peopleJson);
  serializer = new JsonSerializer().exclude("home", "work");
  peopleJson = serializer.serialize(people);
  assertStringValue(Person.class.getName(), peopleJson);
  assertAttribute("firstname", peopleJson);
  assertStringValue("Igor", peopleJson);
  assertStringValue("Modesty", peopleJson);
  assertAttribute("lastname", peopleJson);
  assertStringValue("Spasic", peopleJson);
  assertStringValueMissing(Address.class.getName(), peopleJson);
}

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

jdrSerializer.exclude("home", "work");
String modestyJson = jdrSerializer.serialize(modesty);
assertStringValue(Person.class.getName(), modestyJson);
assertAttributeMissing("person", modestyJson);
serializer.exclude("home.zipcode", "work.zipcode");
assertStringValueMissing("PAGER", json2);
serializer.include("hobbies").exclude(true, "phones.areaCode", "phones.exchange", "phones.number");

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

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

相关文章