* 测试自定义转换器,整体感觉稍显复杂。这里是将Money和Long互转.
*/
@Test
public void customConverter() {
JsonMapper newMapper = JsonMapper.nonEmptyMapper();
SimpleModule moneyModule = new SimpleModule("MoneyModule");
moneyModule.addSerializer(new MoneySerializer());
moneyModule.addDeserializer(Money.class, new MoneyDeserializer());
newMapper.getMapper().registerModule(moneyModule);
// tojson
User user = new User();
user.setName("foo");
user.setSalary(new Money(1.2));
String jsonString = newMapper.toJson(user);
assertThat(jsonString).isEqualTo("{\"name\":\"foo\",\"salary\":\"1.2\"}");
// from
User resultUser = newMapper.fromJson(jsonString, User.class);
assertThat(resultUser.getSalary().value).isEqualTo(1.2);
}