springboot学习(二) Jackson @JsonCompent自定义序列化方式

@JsonComponent
public class DateJsonConvert {
    public static class Serializer extends JsonSerializer {
        @Override
        public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
//            jsonGenerator.writeStartObject();
            jsonGenerator.writeObject(com.iscas.det.utils.DateUtils.format(date,"yyyy-MM"));
//            jsonGenerator.writeStringField(
//                    "favoriteColor",
//                    getColorAsWebColor(user.getFavoriteColor()));
//            jsonGenerator.writeEndObject();
        }
    }
    public static class DeSerializer extends JsonDeserializer {

        @Override
        public Model1 deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
            TreeNode treeNode = jsonParser.getCodec().readTree(jsonParser);
            TextNode textNode
                    = (TextNode) treeNode.get("date");
            try {
                System.out.println(textNode.asText());
                Model1 model1 = new Model1();
                model1.setDate(com.iscas.det.utils.DateUtils.parse(textNode.asText(), "yyyy-MM-dd HH:mm:ss"));
                 return model1;
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return null;
        }
    }

}
 
  

springboot学习 上一篇 https://blog.csdn.net/u011943534/article/details/55505301

springboot学习 下一篇 https://blog.csdn.net/u011943534/article/details/80785937

你可能感兴趣的:(java,spring,boot,Jackson)