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

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

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

JsonSerializer.serialize介绍

[英]Serializes object into source.
[中]将对象序列化为源。

代码示例

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

/**
 * Returns JSON serialized by default {@link JsonSerializer}.
 */
@Override
public String toString() {
  return JsonSerializer.create().deep(true).serialize(map);
}

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

@Test
void testCollection() {
  JsonSerializer serializer = new JsonSerializer();
  String colorsJson = serializer.serialize(colors.values());
  for (Object o : colors.entrySet()) {
    Map.Entry entry = (Map.Entry) o;
    assertAttributeMissing(entry.getKey().toString(), colorsJson);
    assertStringValue(entry.getValue().toString(), colorsJson);
  }
  assertTrue(colorsJson.startsWith("["));
  assertTrue(colorsJson.endsWith("]"));
}

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

@Test
void testSerializeCalendar() throws Exception {
  final Calendar input = Calendar.getInstance(TimeZone.getTimeZone("Europe/Berlin"), Locale.GERMANY);
  input.clear();
  input.setTimeInMillis(1512166354000L);
  final String expected_json = "1512166354000";
  final String actual_json = new JsonSerializer().serialize(input);
  // asserts
  assertNotNull(actual_json);
  assertEquals(expected_json, actual_json);
}

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

private void assertSerializedTo(String original, String expected) {
  JsonSerializer serializer = new JsonSerializer();
  String json = serializer.serialize(original);
  assertEquals(expected, json);
}

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

@Test
void testChar() {
  Character character = Character.valueOf('J');
  String json = new JsonSerializer().serialize(character);
  assertEquals("\"J\"", json);
}

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

@Test
void testClass() {
  String json = new JsonSerializer().serialize(JsonSerializerTest.class);
  assertEquals("\"" + JsonSerializerTest.class.getName() + "\"", json);
}

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

@Test
void testGeneralMapDeserialization2() {
  JsonParsers.forEachParser(jsonParser -> {
    String json = new JsonSerializer().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

@Test
void testCopyOnWriteList() {
  CopyOnWriteArrayList<Person> people = new CopyOnWriteArrayList<>();
  people.add(jodder);
  people.add(modesty);
  String json = new JsonSerializer().serialize(people);
  assertAttribute("firstname", json);
  assertStringValue("Igor", json);
  assertStringValue("Modesty", json);
}

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

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

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

@Test
public void testDouble_NaN() {
  JsonParsers.forEachParser(jsonParser -> {
    String json = JsonSerializer.create().serialize(Double.NaN);
    assertEquals("\"NaN\"", json);
    Double d = jsonParser.parse(json, Double.class);
    assertTrue(d.isNaN());
  });
}

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

@Test
@EnabledOnOs(value = {OS.WINDOWS})
void testFiles_on_windows() {
  FileMan fileMan = new FileMan();
  File userHome = new File(SystemUtil.info().getHomeDir());
  fileMan.setFile(userHome);
  final String json = JsonSerializer.create().serialize(fileMan);
  // C:\Users\xxxx will be user home on windows hsost;  char '\' is escpaed in json therefore the execution of "String#replace"
  final String userhome_escpaed = SystemUtil.info().getHomeDir().replace("\\", "\\\\");
  assertTrue(json.contains(userhome_escpaed));
}

代码示例来源: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 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 testToString() {
  jsonArray.add("foo").add(123);
  assertEquals(JsonSerializer.create().serialize(jsonArray), jsonArray.toString());
}

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

@Test
void testCircularDependenciesArray() {
  WhiteBar[] whiteBars = new WhiteBar[1];
  WhiteBar white = new WhiteBar();
  white.setSum(1);
  white.setBars(whiteBars);
  whiteBars[0] = white;
  String json = new JsonSerializer().serialize(whiteBars);
  assertEquals("[{\"sum\":1}]", json);
}

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

@Test
void testSerializeSuperClass() {
  JsonSerializer serializer = new JsonSerializer();
  String json = serializer.serialize(dilbert);
  assertAttribute("company", json);
  assertStringValue("Initech", json);
  assertAttribute("firstname", json);
  assertStringValue("Dilbert", json);
}

代码示例来源: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 testPoint() {
  JsonParser.Defaults.classMetadataName = "__class";
  JsonSerializer.Defaults.classMetadataName = "__class";
  JsonParsers.forEachParser(jsonParser -> {
    String json = new JsonSerializer().serialize(new Point2D.Float(1.0f, 2.0f));
    Point2D.Float point = jsonParser.parse(json);
    assertEquals(1.0f, point.x, DELTA);
    assertEquals(2.0f, point.y, DELTA);
  });
}

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

相关文章