jodd.json.JsonSerializer类的使用及代码示例

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

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

JsonSerializer介绍

[英]JSON serializer.
[中]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

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

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

/**
 * Serializes object into provided appendable.
 */
public void serialize(final Object source, final Appendable target) {
  JsonContext jsonContext = createJsonContext(target);
  jsonContext.serialize(source);
}

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

@Test
void testClassMetaData() {
  String json = JsonSerializer
    .create()
    .withClassMetadata(true)
    .serialize(new Foo());
  assertTrue(json.contains("\"__class\":\"" + Foo.class.getName() + "\""));
  json = JsonSerializer.create().withClassMetadata(false).serialize(123);
  assertEquals("123", json);
  json = JsonSerializer.create().withClassMetadata(true).serialize(123);
  assertEquals("123", json);
}

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

@Test
void testDeepSerializationCycles() {
  JsonSerializer serializer = new JsonSerializer();
  String json = serializer.deep(true).serialize(people);
  assertAttribute("zipcode", json);
  assertEquals(2, occurs(pedroZip.getZipcode(), json));
  assertAttributeMissing("person", json);
}

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

@Test
  void testBooleanField() {
    Active active = new Active();

    String json = JsonSerializer.create().serialize(active);

    System.out.println(json);

    assertTrue(json.contains("\"active\":true"));
    assertTrue(json.contains("\"inactive\":3"));

  }
}

代码示例来源: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 testExcludeAll() {
  JsonSerializer serializer = new JsonSerializer();
  String json = serializer.exclude("*").serialize(jodder);
  assertEquals("{}", json);
}

代码示例来源: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 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 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: com.liferay.launchpad/api-client

@Override
public String serialize(Object object, boolean deep) {
  return JsonSerializer
    .create()
    .deep(deep)
    .use(PodMultiMap.class, POD_MULTI_MAP_JSON_SERIALIZER)
    .use(long.class, LONG_TO_STRING_JSON_SERIALIZER)
    .use(Long.class, LONG_TO_STRING_JSON_SERIALIZER)
    .serialize(object);
}

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

@Test
void testJsonValueContextArray2() {
  JsonParsers.forEachParser(jsonParser -> {
    Object[] array = new Object[]{new Surfer(), "two", "three"};
    JsonSerializer jsonSerializer = new JsonSerializer()
      .withSerializer(String.class, new MyTypeJsonSerializer2());
    String json = jsonSerializer.serialize(array);
    List<String> list = jsonParser.parse(json);
    assertEquals(3, list.size());
    assertEquals("TWO", list.get(1));
  });
}

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

@Test
void testAnnNameWithClass() {
  JsonParsers.forEachParser(jsonParser -> {
    Location location = new Location();
    location.setLatitude(65);
    location.setLongitude(12);
    String json = new JsonSerializer().setClassMetadataName("class").serialize(location);
    assertEquals("{\"class\":\"" + Location.class.getName() + "\",\"lat\":65,\"lng\":12}", json);
    Location jsonLocation = jsonParser.setClassMetadataName("class").parse(json, Location.class);
    assertEquals(location.getLatitude(), jsonLocation.getLatitude());
    assertEquals(location.getLongitude(), jsonLocation.getLongitude());
  });
}

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

@Test
void testSerializeStringEscapes() {
  JsonParsers.forEachParser(jsonParser -> {
    String path = "/foo/bar";
    String json = JsonSerializer
      .create()
      .strictStringEncoding(true)
      .serialize(path);
    assertEquals("\"\\/foo\\/bar\"", json);
    String path2 = jsonParser.parse(json);
    assertEquals(path, path2);
  });
}

代码示例来源: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 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 testEscapeChars() {
  JsonParsers.forEachParser(jsonParser -> {
    String json = "\"1\\\" 2\\\\ 3\\/ 4\\b 5\\f 6\\n 7\\r 8\\t\"";
    String str = jsonParser.parse(json);
    assertEquals("1\" 2\\ 3/ 4\b 5\f 6\n 7\r 8\t", str);
    String jsonStr = new JsonSerializer().strictStringEncoding(true).serialize(str);
    assertEquals(json, jsonStr);
  });
}

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

/**
 * Serializes the object, but returns the {@link CharSequence}.
 */
public CharSequence serializeToCharSequence(final Object source) {
  FastCharBuffer fastCharBuffer = new FastCharBuffer();
  serialize(source, fastCharBuffer);
  return fastCharBuffer;
}

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

相关文章