Search in sources :

Example 1 with DeserializationProblemHandler

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;
}
Also used : DeserializationProblemHandler(com.fasterxml.jackson.databind.deser.DeserializationProblemHandler) ContextAttributes(com.fasterxml.jackson.databind.cfg.ContextAttributes)

Example 2 with DeserializationProblemHandler

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");
}
Also used : IOException(java.io.IOException) TransportException(org.jocean.http.TransportException) IOException(java.io.IOException) ErrorDataEncoderException(io.netty.handler.codec.http.multipart.HttpPostRequestEncoder.ErrorDataEncoderException) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper) DeserializationProblemHandler(com.fasterxml.jackson.databind.deser.DeserializationProblemHandler) Consumes(javax.ws.rs.Consumes) DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 3 with DeserializationProblemHandler

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());
}
Also used : DeserializationProblemHandler(com.fasterxml.jackson.databind.deser.DeserializationProblemHandler)

Example 4 with DeserializationProblemHandler

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;
    }
}
Also used : DeserializationProblemHandler(com.fasterxml.jackson.databind.deser.DeserializationProblemHandler) DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) HttpObject(io.netty.handler.codec.http.HttpObject) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper) JsonParser(com.fasterxml.jackson.core.JsonParser)

Aggregations

DeserializationProblemHandler (com.fasterxml.jackson.databind.deser.DeserializationProblemHandler)4 JsonParser (com.fasterxml.jackson.core.JsonParser)2 DeserializationContext (com.fasterxml.jackson.databind.DeserializationContext)2 XmlMapper (com.fasterxml.jackson.dataformat.xml.XmlMapper)2 IOException (java.io.IOException)2 ContextAttributes (com.fasterxml.jackson.databind.cfg.ContextAttributes)1 HttpObject (io.netty.handler.codec.http.HttpObject)1 ErrorDataEncoderException (io.netty.handler.codec.http.multipart.HttpPostRequestEncoder.ErrorDataEncoderException)1 URISyntaxException (java.net.URISyntaxException)1 Consumes (javax.ws.rs.Consumes)1 TransportException (org.jocean.http.TransportException)1