如何为第三方枚举自定义反序列化器?
How to customize deserializer for third party Enum?
我有以下解串器:
public class HttpStatusDeserializer extends JsonDeserializer<HttpStatus> {
@Override
public HttpStatus deserialize(JsonParser jsonParser, DeserializationContext ctxt) throws IOException, JsonProcessingException {
ObjectCodec oc = jsonParser.getCodec();
JsonNode node = oc.readTree(jsonParser);
return HttpStatus.valueOf(node.asText());
}
}
并注册
public class CustomDeserializerModifier extends BeanDeserializerModifier {
@Override
public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, BeanDescription beanDesc, JsonDeserializer<?> deserializer) {
if (beanDesc.getBeanClass() == HttpStatus.class) {
return new HttpStatusDeserializer();
}
return deserializer;
}
}
mappingJackson2HttpMessageConverter
.setObjectMapper(new ObjectMapper().registerModule(new SimpleModule()
.setSerializerModifier(new CustomSerializerModifier())
.setDeserializerModifier(new CustomDeserializerModifier())));
它不为枚举调用,而是为另一个枚举调用 类。
我怎样才能赢得这场比赛?
问题是这样解决的:
class MyClass{
...
HttpStatus httpStatus = HttpStatus.OK //from org.springframework.http
@JsonIgnore
public HttpStatus getHttpStatus() {
return httpStatus;
}
@JsonProperty(value = "HttpStatus")
@JsonDeserialize(using = HttpStatusDeserializer.class)
public void setHttpStatus(HttpStatus httpStatus) {
this.httpStatus = httpStatus;
}
....
}
和解串器:
public class HttpStatusDeserializer extends JsonDeserializer<HttpStatus> {
@Override
public HttpStatus deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
ObjectCodec oc = jsonParser.getCodec();
JsonNode node = oc.readTree(jsonParser);
return HttpStatus.valueOf(Integer.parseInt(node.asText()));
}
}
我有以下解串器:
public class HttpStatusDeserializer extends JsonDeserializer<HttpStatus> {
@Override
public HttpStatus deserialize(JsonParser jsonParser, DeserializationContext ctxt) throws IOException, JsonProcessingException {
ObjectCodec oc = jsonParser.getCodec();
JsonNode node = oc.readTree(jsonParser);
return HttpStatus.valueOf(node.asText());
}
}
并注册
public class CustomDeserializerModifier extends BeanDeserializerModifier {
@Override
public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, BeanDescription beanDesc, JsonDeserializer<?> deserializer) {
if (beanDesc.getBeanClass() == HttpStatus.class) {
return new HttpStatusDeserializer();
}
return deserializer;
}
}
mappingJackson2HttpMessageConverter
.setObjectMapper(new ObjectMapper().registerModule(new SimpleModule()
.setSerializerModifier(new CustomSerializerModifier())
.setDeserializerModifier(new CustomDeserializerModifier())));
它不为枚举调用,而是为另一个枚举调用 类。
我怎样才能赢得这场比赛?
问题是这样解决的:
class MyClass{
...
HttpStatus httpStatus = HttpStatus.OK //from org.springframework.http
@JsonIgnore
public HttpStatus getHttpStatus() {
return httpStatus;
}
@JsonProperty(value = "HttpStatus")
@JsonDeserialize(using = HttpStatusDeserializer.class)
public void setHttpStatus(HttpStatus httpStatus) {
this.httpStatus = httpStatus;
}
....
}
和解串器:
public class HttpStatusDeserializer extends JsonDeserializer<HttpStatus> {
@Override
public HttpStatus deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
ObjectCodec oc = jsonParser.getCodec();
JsonNode node = oc.readTree(jsonParser);
return HttpStatus.valueOf(Integer.parseInt(node.asText()));
}
}