use of com.fasterxml.jackson.databind.deser.DeserializationProblemHandler in project jackson-databind by FasterXML.
the class ObjectReaderTest method testMiscSettings.
public void testMiscSettings() throws Exception {
ObjectReader r = MAPPER.reader();
assertSame(MAPPER.getFactory(), r.getFactory());
JsonFactory f = new JsonFactory();
r = r.with(f);
assertSame(f, r.getFactory());
assertSame(r, r.with(f));
assertNotNull(r.getTypeFactory());
assertNull(r.getInjectableValues());
r = r.withAttributes(Collections.emptyMap());
ContextAttributes attrs = r.getAttributes();
assertNotNull(attrs);
assertNull(attrs.getAttribute("abc"));
assertSame(r, r.withoutAttribute("foo"));
ObjectReader newR = r.forType(MAPPER.constructType(String.class));
assertNotSame(r, newR);
assertSame(newR, newR.forType(String.class));
DeserializationProblemHandler probH = new DeserializationProblemHandler() {
};
newR = r.withHandler(probH);
assertNotSame(r, newR);
assertSame(newR, newR.withHandler(probH));
r = newR;
}
use of com.fasterxml.jackson.databind.deser.DeserializationProblemHandler in project jocean-http by isdom.
the class DefaultSignalClient method toRESP.
@SuppressWarnings("unchecked")
private <RESP> RESP toRESP(final FullHttpResponse fullresp, final Class<?> respType, final Class<?> bodyType) {
if (null != fullresp) {
try {
final byte[] bytes = Nettys.dumpByteBufAsBytes(fullresp.content());
if (LOG.isTraceEnabled()) {
try {
LOG.trace("receive signal response: {}", new String(bytes, CharsetUtil.UTF_8));
} catch (Exception e) {
// decode bytes as UTF-8 error, just ignore
}
}
if (null != respType) {
return DefaultSignalClient.<RESP>convertResponseTo(fullresp, bytes, respType);
}
if (null != bodyType) {
if (bodyType.equals(String.class)) {
return (RESP) new String(bytes, CharsetUtil.UTF_8);
} else {
// try need decode as xml
final Consumes consumes = bodyType.getAnnotation(Consumes.class);
if (null != consumes) {
final Collection<String> mimeTypes = Arrays.asList(consumes.value());
if (mimeTypes.contains(MediaType.APPLICATION_XML)) {
final XmlMapper mapper = new XmlMapper();
mapper.addHandler(new DeserializationProblemHandler() {
@Override
public boolean handleUnknownProperty(final DeserializationContext ctxt, final JsonParser p, final JsonDeserializer<?> deserializer, final Object beanOrClass, final String propertyName) throws IOException {
LOG.warn("UnknownProperty [{}], just skip", propertyName);
p.skipChildren();
return true;
}
});
try {
return (RESP) mapper.readValue(bytes, bodyType);
} catch (Exception e) {
LOG.warn("exception when parse xml, detail: {}", ExceptionUtils.exception2detail(e));
return null;
}
}
}
// try need decode as json
return JSON.<RESP>parseObject(bytes, bodyType);
}
} else {
final RESP respObj = (RESP) bytes;
return respObj;
}
} catch (Exception e) {
LOG.warn("exception when parse response {}, detail:{}", fullresp, ExceptionUtils.exception2detail(e));
throw new RuntimeException(e);
}
}
throw new RuntimeException("invalid response");
}
use of com.fasterxml.jackson.databind.deser.DeserializationProblemHandler in project jackson-databind by FasterXML.
the class TestUnknownPropertyDeserialization method testIssue987.
public void testIssue987() throws Exception {
ObjectMapper jsonMapper = new ObjectMapper();
jsonMapper.addHandler(new DeserializationProblemHandler() {
@Override
public boolean handleUnknownProperty(DeserializationContext ctxt, JsonParser p, JsonDeserializer<?> deserializer, Object beanOrClass, String propertyName) throws IOException, JsonProcessingException {
p.skipChildren();
return true;
}
});
String input = "[{\"aProperty\":\"x\",\"unknown\":{\"unknown\":{}}}]";
List<Bean987> deserializedList = jsonMapper.readValue(input, new TypeReference<List<Bean987>>() {
});
assertEquals(1, deserializedList.size());
}
use of com.fasterxml.jackson.databind.deser.DeserializationProblemHandler in project jocean-http by isdom.
the class MessageUtil method unserializeAsXml.
public static <T> T unserializeAsXml(final InputStream is, final Class<T> type) {
final XmlMapper mapper = new XmlMapper();
mapper.addHandler(new DeserializationProblemHandler() {
@Override
public boolean handleUnknownProperty(final DeserializationContext ctxt, final JsonParser p, final JsonDeserializer<?> deserializer, final Object beanOrClass, final String propertyName) throws IOException {
LOG.warn("UnknownProperty [{}], just skip", propertyName);
p.skipChildren();
return true;
}
});
try {
return mapper.readValue(is, type);
} catch (Exception e) {
LOG.warn("exception when parse as xml, detail: {}", ExceptionUtils.exception2detail(e));
return null;
}
}
Aggregations