Java源码示例:io.vavr.jackson.datatype.VavrModule
示例1
public static void initMapper(TypeSpec.Builder builder, String name, VavrModule.Settings settings) {
Modifier[] mods = new Modifier[] { Modifier.PRIVATE, Modifier.STATIC, Modifier.FINAL };
if (settings != null) {
builder.addField(FieldSpec.builder(ClassName.get(VavrModule.Settings.class), name + "_SETTINGS", mods)
.initializer("new $T()\n .useOptionInPlainFormat($L).deserializeNullAsEmptyCollection($L)",
ClassName.get(VavrModule.Settings.class),
settings.useOptionInPlainFormat(), settings.deserializeNullAsEmptyCollection())
.build());
builder.addField(FieldSpec.builder(ClassName.get(VavrModule.class), name + "_MODULE", mods)
.initializer("new $T($L)", ClassName.get(VavrModule.class), name + "_SETTINGS")
.build());
} else {
builder.addField(FieldSpec.builder(ClassName.get(VavrModule.class), name + "_MODULE", mods)
.initializer("new $T()", ClassName.get(VavrModule.class))
.build());
}
builder.addField(FieldSpec.builder(ClassName.get(ObjectMapper.class), name, mods)
.initializer("new $T().registerModule($L)", ClassName.get(ObjectMapper.class), name + "_MODULE")
.build());
}
示例2
@Test
public void itShouldSerializeVavrOptionYearMonthAsStringWithoutJsonFormat() throws IOException {
// Given an instance with io.vavr.control.Option
MyVavrOptionalClassWithoutFormat obj = new MyVavrOptionalClassWithoutFormat();
obj.operatingMonth = Option.of(YearMonth.of(2019, 12));
// When serializing the instance using object mapper
// with Java Time Module and VAVR Module
ObjectMapper objectMapper = mapper();
objectMapper.registerModule(new JavaTimeModule());
objectMapper.registerModule(new VavrModule());
String json = objectMapper.writeValueAsString(obj);
// Then serialization is successful
Assertions.assertEquals("{\"operatingMonth\":[2019,12]}", json);
MyVavrOptionalClassWithoutFormat obj2 = objectMapper.readValue(json, MyVavrOptionalClassWithoutFormat.class);
// And deserialization is successful
Assertions.assertEquals(Option.of(YearMonth.of(2019, 12)), obj2.operatingMonth);
}
示例3
@Test
public void itShouldSerializeVavrOptionYearMonthAsString() throws IOException {
// Given an instance with io.vavr.control.Option
MyVavrOptionClassWithFormat obj = new MyVavrOptionClassWithFormat();
obj.operatingMonth = Option.of(YearMonth.of(2019, 12));
// When serializing the instance using object mapper
// with Java Time Module and VAVR Module
ObjectMapper objectMapper = mapper();
objectMapper.registerModule(new JavaTimeModule());
objectMapper.registerModule(new VavrModule());
String json = objectMapper.writeValueAsString(obj);
// Then serialization is failed
Assertions.assertEquals("{\"operatingMonth\":\"12-2019\"}", json);
MyVavrOptionClassWithFormat obj2 = objectMapper.readValue(json, MyVavrOptionClassWithFormat.class);
// And deserialization is failed
Assertions.assertEquals(Option.of(YearMonth.of(2019, 12)), obj2.operatingMonth);
}
示例4
@Test
void test4() throws IOException {
VavrModule.Settings settings = new VavrModule.Settings();
settings.deserializeNullAsEmptyCollection(true);
ObjectMapper mapper = mapper(settings);
Set<?> restored = mapper.readValue("null", typeReference());
Assertions.assertTrue(restored.isEmpty());
}
示例5
@Test
void test4() throws IOException {
VavrModule.Settings settings = new VavrModule.Settings();
settings.deserializeNullAsEmptyCollection(true);
ObjectMapper mapper = mapper(settings);
Seq<?> restored = (Seq<?>) mapper.readValue("null", clz());
Assertions.assertTrue(restored.isEmpty());
}
示例6
public static <T> void json_roundtrip_test(T value, Class<T> valueType) throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new VavrModule());
String asString = mapper.writeValueAsString(value);
assertNotNull(asString);
final T value_decoded = mapper.readValue(asString, valueType);
assertEquals(value, value_decoded);
}
示例7
@BeforeEach
public void before() {
mapper = new ObjectMapper();
SimpleModule module = new VavrModule()
.addKeyDeserializer(MyComparable.class, new KeyDeserializer() {
@Override
public Object deserializeKey(String key, DeserializationContext ctxt) {
return new MyComparable(Integer.parseInt(key));
}
});
mapper.registerModule(module);
}
示例8
@Test
void itShouldSerializeVavrListWithVavrModule() throws Exception {
MyVavrClass myClass = new MyVavrClass();
myClass.dates = List.of(new Date(1591221600000L), new Date(1591308000000L));
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new VavrModule());
String json = mapper.writeValueAsString(myClass);
assertEquals("{\"dates\":[\"2020-06-04\",\"2020-06-05\"]}", json);
}
示例9
@Test
void itShouldSerializeVavrListWithVavrModuleAndJavaTimeModule() throws Exception {
MyVavrClass myClass = new MyVavrClass();
myClass.dates = List.of(new Date(1591221600000L), new Date(1591308000000L));
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new VavrModule());
mapper.registerModule(new JavaTimeModule());
String json = mapper.writeValueAsString(myClass);
assertEquals("{\"dates\":[\"2020-06-04\",\"2020-06-05\"]}", json);
}
示例10
public static Schema parseJson(Reader in) throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.registerModule(new VavrModule());
return mapper.readValue(in, Schema.class);
}
示例11
public VavrSerializers(VavrModule.Settings settings) {
this.settings = settings;
}
示例12
public VavrDeserializers(VavrModule.Settings settings) {
this.settings = settings;
}