You can customize how SnackJson handles specific types and formatting using the Options object.
Options options = Options.of();
// Add a type checker (for Feature.Read_AutoType)
options.addChecker(clzName -> {
if (clzName.startsWith("com.ok.xxx.")) return TypeChecker.ALLOW;
if (clzName.startsWith("com.no.yyy.")) return TypeChecker.DENY;
return TypeChecker.SKIP;
});
// Add custom encoder/decoder
options.addEncoder(Date.class, (ctx, value, target) -> {
target.setValue(DateUtil.format(value, "yyyy-MM-dd"));
});
options.addDecoder(Date.class, ...);
// Add/Remove features
options.addFeature(Feature.Write_PrettyFormat);
options.removeFeature(Feature.Write_PrettyFormat);
// Configure date format for Feature.Write_UseDateFormat
options.addFeature(Feature.Write_UseDateFormat);
options.dateFormat("yyyy-MM");
// Apply options
String json = ONode.ofBean(orderModel, options).toJson();
Options options = Options.of();
//添加检测器(for Feature.Read_AutoType)
options.addChecker(clzName -> {
//通过
if (clzName.startsWith("com.ok.xxx.")) {
return TypeChecker.ALLOW;
}
//拒绝
if (clzName.startsWith("com.no.yyy.")) {
return TypeChecker.DENY;
}
//跳过
return TypeChecker.SKIP;
});
//添加编码器
options.addEncoder(Date.class, (ctx, value, target) -> {
target.setValue(DateUtil.format(data, "yyyy-MM-dd"));
});
//添加解码器
options.addDecoder(Date.class, ...);
//添加创建器(接管类实例化)
options.addCreator(...);
//添加特性
options.addFeature(Feature.Write_PrettyFormat);
//移除特性
options.removeFeature(Feature.Write_PrettyFormat);
//设置日期格式附
options.addFeature(Feature.Write_UseDateFormat); //使用日期格式
options.dateFormat("yyyy-MM");
//..
String json = ONode.ofBean(orderModel, options).toJson();