Java Jackson自定义反序列化程序会破坏默认反序列化程序

Java Jackson自定义反序列化程序会破坏默认反序列化程序,java,json,serialization,jackson,Java,Json,Serialization,Jackson,我目前正在实现JSON到POJO的解析 我的模型类如下所示: public class InvoiceModel { @JsonDeserialize(using=MeteorDateDeserializer.class) Date date; String userId; String customerName; ArrayList<PaymentModel> payments; [...getters, setters...] }

我目前正在实现JSON到POJO的解析

我的模型类如下所示:

public class InvoiceModel {
    @JsonDeserialize(using=MeteorDateDeserializer.class)
    Date date;
    String userId;
    String customerName;
    ArrayList<PaymentModel> payments;
    [...getters, setters...]
}
{
  "date": {
    "$date": 1453812396858
  },
  "userId": "igxL4tNwR58xuuJbE",
  "customerName": "S04",
  "payments": [
    {
      "value": 653.5,
      "paymentMethod": "Cash",
      "userId": "igxL4tNwR58xuuJbE",
      "date": {
        "$date": 1453812399033
      }
    }
  ]
}
public class MeteorDateDeserializer extends org.codehaus.jackson.map.JsonDeserializer<Date> {
    @Override
    public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
        // parse the "$date" field found by traversing the given tokens of jsonParser 
        while(!jsonParser.isClosed()){
            JsonToken jsonToken = jsonParser.nextToken();
            if(JsonToken.FIELD_NAME.equals(jsonToken)){
                String fieldName = jsonParser.getCurrentName();
                jsonToken = jsonParser.nextToken();
                if("$date".equals(fieldName)){
                    long timeStamp = jsonParser.getLongValue();
                    return new java.util.Date(timeStamp);
                }
            }
        }

        return null;
    }
}
尽管可以省略付款字段。不管是谁,这个问题其实并不重要。你看,解析日期的格式有点“奇怪”,因为它被封装在“date”对象的“$date”属性中。但这不在我的控制范围之内

为了解决这个问题,我编写了一个自定义JSON反序列化程序,用于此日期属性。看起来是这样的:

public class InvoiceModel {
    @JsonDeserialize(using=MeteorDateDeserializer.class)
    Date date;
    String userId;
    String customerName;
    ArrayList<PaymentModel> payments;
    [...getters, setters...]
}
{
  "date": {
    "$date": 1453812396858
  },
  "userId": "igxL4tNwR58xuuJbE",
  "customerName": "S04",
  "payments": [
    {
      "value": 653.5,
      "paymentMethod": "Cash",
      "userId": "igxL4tNwR58xuuJbE",
      "date": {
        "$date": 1453812399033
      }
    }
  ]
}
public class MeteorDateDeserializer extends org.codehaus.jackson.map.JsonDeserializer<Date> {
    @Override
    public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
        // parse the "$date" field found by traversing the given tokens of jsonParser 
        while(!jsonParser.isClosed()){
            JsonToken jsonToken = jsonParser.nextToken();
            if(JsonToken.FIELD_NAME.equals(jsonToken)){
                String fieldName = jsonParser.getCurrentName();
                jsonToken = jsonParser.nextToken();
                if("$date".equals(fieldName)){
                    long timeStamp = jsonParser.getLongValue();
                    return new java.util.Date(timeStamp);
                }
            }
        }

        return null;
    }
}
其中s2是JSON字符串

我的杰克逊版本是1.9.7

,我想我找到了。问题的根源可能是在反序列化程序运行后,
JsonParser
的内部状态。问题的根源可能是反序列化程序运行后
JsonParser
的内部状态。