Example usage for com.fasterxml.jackson.dataformat.xml XmlMapper readValue

List of usage examples for com.fasterxml.jackson.dataformat.xml XmlMapper readValue

Introduction

In this page you can find the example usage for com.fasterxml.jackson.dataformat.xml XmlMapper readValue.

Prototype

@Override
@SuppressWarnings("unchecked")
public <T> T readValue(JsonParser jp, Class<T> valueType)
        throws IOException, JsonParseException, JsonMappingException 

Source Link

Document

Method to deserialize JSON content into a non-container type (it can be an array type, however): typically a bean, array or a wrapper type (like java.lang.Boolean ).

Usage

From source file:com.ning.billing.recurly.model.TestXmlMapper.java

@Test(groups = "fast", description = "See https://github.com/FasterXML/jackson-dataformat-xml/issues/76")
public void testCollection() throws Exception {
    final XmlMapper xmlMapper = new XmlMapper();
    xmlMapper.setSerializerProvider(new RecurlyXmlSerializerProvider());
    final SimpleModule m = new SimpleModule("module", new Version(1, 0, 0, null, null, null));
    m.addSerializer(Values.class, new ValuesSerializer());
    xmlMapper.registerModule(m);//from  w ww . jav a  2 s. c om

    final Values values = xmlMapper
            .readValue(
                    "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "  <values type=\"array\">\n"
                            + "    <value>Hi!</value>" + "    <value>Salut!</value>" + "  </values>",
                    Values.class);
    Assert.assertEquals(values.size(), 2, values.toString());
    Assert.assertEquals(values.get(0), "Hi!");
    Assert.assertEquals(values.get(1), "Salut!");

    // Test we can re-serialize
    final String valueAsString = xmlMapper.writeValueAsString(values);
    final Values values2 = xmlMapper.readValue(valueAsString, Values.class);
    Assert.assertEquals(values2, values, valueAsString);
}

From source file:com.msopentech.odatajclient.engine.data.Deserializer.java

public static Edmx toMetadata(final InputStream input) {
    try {/*  w w  w .  java  2s  . c om*/
        final XmlMapper xmlMapper = new XmlMapper(
                new XmlFactory(new InputFactoryImpl(), new OutputFactoryImpl()), new JacksonXmlModule());
        xmlMapper.addHandler(new DeserializationProblemHandler() {

            @Override
            public boolean handleUnknownProperty(final DeserializationContext ctxt, final JsonParser jp,
                    final JsonDeserializer<?> deserializer, final Object beanOrClass, final String propertyName)
                    throws IOException, JsonProcessingException {

                // 1. special handling of AbstractAnnotatedEdm's fields
                if (beanOrClass instanceof AbstractAnnotatedEdm
                        && AbstractAnnotatedEdmUtils.isAbstractAnnotatedProperty(propertyName)) {

                    AbstractAnnotatedEdmUtils.parseAnnotatedEdm((AbstractAnnotatedEdm) beanOrClass, jp);
                } // 2. skip any other unknown property
                else {
                    ctxt.getParser().skipChildren();
                }

                return true;
            }
        });
        return xmlMapper.readValue(input, Edmx.class);
    } catch (Exception e) {
        throw new IllegalArgumentException("Could not parse as Edmx document", e);
    }
}

From source file:com.gnip.test.YouTubeEDCSerDeTest.java

@Test
public void Tests() throws Exception {
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, Boolean.FALSE);
    mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, Boolean.TRUE);
    mapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, Boolean.TRUE);

    InputStream is = YouTubeEDCSerDeTest.class.getResourceAsStream("/YoutubeEDC.xml");
    if (is == null)
        System.out.println("null");
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);
    XmlMapper xmlMapper = new XmlMapper();
    xmlMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, Boolean.FALSE);
    xmlMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, Boolean.TRUE);
    xmlMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, Boolean.TRUE);

    ObjectMapper jsonMapper = new ObjectMapper();

    try {//from www.j  a v  a 2s  .c  o m
        while (br.ready()) {
            String line = br.readLine();
            //LOGGER.debug(line);

            Object activityObject = xmlMapper.readValue(line, Object.class);

            String jsonObject = jsonMapper.writeValueAsString(activityObject);

            //LOGGER.debug(jsonObject);
        }
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail();
    }
}

From source file:com.gnip.test.YoutubeEDCAsActivityTest.java

@Test
@Ignore//from  www.ja v  a  2  s . c om
public void Tests() throws Exception {
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, Boolean.FALSE);
    mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, Boolean.TRUE);
    mapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, Boolean.TRUE);

    InputStream is = YoutubeEDCAsActivityTest.class.getResourceAsStream("/YoutubeEDC.xml");
    if (is == null)
        System.out.println("null");
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);
    XmlMapper xmlMapper = new XmlMapper();
    xmlMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, Boolean.FALSE);
    xmlMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, Boolean.TRUE);
    xmlMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, Boolean.TRUE);

    ObjectMapper jsonMapper = new ObjectMapper();

    try {
        while (br.ready()) {
            String line = br.readLine();
            //LOGGER.debug(line);

            Object activityObject = xmlMapper.readValue(line, Object.class);

            String jsonString = jsonMapper.writeValueAsString(activityObject);

            JSONObject jsonObject = new JSONObject(jsonString);

            JSONObject fixedObject = GnipActivityFixer.fix(jsonObject);

            Activity activity = jsonMapper.readValue(fixedObject.toString(), Activity.class);

            //LOGGER.debug(des);
        }
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail();
    }
}

From source file:org.apache.streams.gnip.flickr.test.FlickrEDCAsActivityTest.java

@Ignore
@Test//w  w  w .j av a  2  s.c  o m
public void Tests() throws Exception {
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, Boolean.FALSE);
    mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, Boolean.TRUE);
    mapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, Boolean.TRUE);

    InputStream is = FlickrEDCAsActivityTest.class.getResourceAsStream("/FlickrEDC.xml");
    if (is == null)
        System.out.println("null");
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);
    XmlMapper xmlMapper = new XmlMapper();
    xmlMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, Boolean.FALSE);
    xmlMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, Boolean.TRUE);
    xmlMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, Boolean.TRUE);

    ObjectMapper jsonMapper = new ObjectMapper();

    try {
        while (br.ready()) {
            String line = br.readLine();
            //LOGGER.debug(line);

            Object activityObject = xmlMapper.readValue(line, Object.class);

            String jsonString = jsonMapper.writeValueAsString(activityObject);

            JSONObject jsonObject = new JSONObject(jsonString);

            JSONObject fixedObject = GnipActivityFixer.fix(jsonObject);

            Activity activity = null;
            try {
                activity = jsonMapper.readValue(fixedObject.toString(), Activity.class);
            } catch (Exception e) {
                LOGGER.error(jsonObject.toString());
                LOGGER.error(fixedObject.toString());
                e.printStackTrace();
                Assert.fail();
            }
            //LOGGER.debug(des);
        }
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail();
    }
}

From source file:org.apache.streams.gnip.flickr.test.FlickrEDCSerDeTest.java

@Test
public void Tests() throws Exception {
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, Boolean.FALSE);
    mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, Boolean.TRUE);
    mapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, Boolean.TRUE);

    InputStream is = FlickrEDCSerDeTest.class.getResourceAsStream("/FlickrEDC.xml");
    if (is == null)
        System.out.println("null");
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);
    XmlMapper xmlMapper = new XmlMapper();
    xmlMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, Boolean.FALSE);
    xmlMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, Boolean.TRUE);
    xmlMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, Boolean.TRUE);
    xmlMapper.configure(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, Boolean.FALSE);

    ObjectMapper jsonMapper = new ObjectMapper();

    try {/*from   w  ww.j  a va 2  s.  c  om*/
        while (br.ready()) {
            String line = br.readLine();
            //LOGGER.debug(line);

            Object activityObject = xmlMapper.readValue(line, Object.class);

            String jsonObject = jsonMapper.writeValueAsString(activityObject);

            //LOGGER.debug(jsonObject);
        }
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail();
    }
}