Search in sources :

Example 41 with XmlMapper

use of com.fasterxml.jackson.dataformat.xml.XmlMapper in project oap by oaplatform.

the class Binder method initialize.

private static ObjectMapper initialize(ObjectMapper mapper, boolean defaultTyping, boolean nonNullInclusion, boolean skipInputNulls) {
    if (mapper instanceof XmlMapper) {
        ((XmlMapper) mapper).setDefaultUseWrapper(false);
        ((XmlMapper) mapper).configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true);
    }
    AnnotationIntrospector introspector = new JacksonAnnotationIntrospector();
    mapper.getDeserializationConfig().with(introspector);
    mapper.getSerializationConfig().with(introspector);
    mapper.registerModule(new AfterburnerModule());
    mapper.registerModule(new Jdk8Module().configureAbsentsAsNulls(true));
    mapper.registerModule(new JodaModule());
    mapper.registerModule(new ExtModule());
    mapper.registerModule(new JavaTimeModule());
    mapper.registerModule(new ParameterNamesModule(JsonCreator.Mode.DEFAULT));
    mapper.enable(DeserializationFeature.USE_LONG_FOR_INTS);
    mapper.enable(JsonParser.Feature.ALLOW_SINGLE_QUOTES);
    mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    mapper.disable(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS);
    mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
    mapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
    mapper.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES);
    mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
    // todo remove after kernel cleanup
    mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
    if (skipInputNulls)
        mapper.setDefaultSetterInfo(JsonSetter.Value.forValueNulls(Nulls.SKIP));
    if (!nonNullInclusion)
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    modules.forEach(mapper::registerModule);
    if (defaultTyping)
        mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
    return mapper;
}
Also used : JacksonAnnotationIntrospector(com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector) AfterburnerModule(com.fasterxml.jackson.module.afterburner.AfterburnerModule) Jdk8Module(com.fasterxml.jackson.datatype.jdk8.Jdk8Module) ParameterNamesModule(com.fasterxml.jackson.module.paramnames.ParameterNamesModule) JodaModule(com.fasterxml.jackson.datatype.joda.JodaModule) JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) JacksonAnnotationIntrospector(com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector) AnnotationIntrospector(com.fasterxml.jackson.databind.AnnotationIntrospector) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper) ExtModule(oap.json.ext.ExtModule)

Example 42 with XmlMapper

use of com.fasterxml.jackson.dataformat.xml.XmlMapper in project tutorials by eugenp.

the class SimpleBeanForCapitalizedFields method whenJavaSerializedToXmlFile_thenCorrect.

@Test
public void whenJavaSerializedToXmlFile_thenCorrect() throws IOException {
    XmlMapper xmlMapper = new XmlMapper();
    xmlMapper.writeValue(new File("target/simple_bean.xml"), new SimpleBean());
    File file = new File("target/simple_bean.xml");
    assertNotNull(file);
}
Also used : File(java.io.File) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper) Test(org.junit.Test)

Example 43 with XmlMapper

use of com.fasterxml.jackson.dataformat.xml.XmlMapper in project tutorials by eugenp.

the class SimpleBeanForCapitalizedFields method whenJavaGotFromXmlStrWithCapitalElem_thenCorrect.

@Test
public void whenJavaGotFromXmlStrWithCapitalElem_thenCorrect() throws IOException {
    XmlMapper xmlMapper = new XmlMapper();
    SimpleBeanForCapitalizedFields value = xmlMapper.readValue("<SimpleBeanForCapitalizedFields><X>1</X><y>2</y></SimpleBeanForCapitalizedFields>", SimpleBeanForCapitalizedFields.class);
    assertTrue(value.getX() == 1 && value.getY() == 2);
}
Also used : XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper) Test(org.junit.Test)

Example 44 with XmlMapper

use of com.fasterxml.jackson.dataformat.xml.XmlMapper in project tutorials by eugenp.

the class SimpleBeanForCapitalizedFields method whenJavaGotFromXmlStr_thenCorrect.

@Test
public void whenJavaGotFromXmlStr_thenCorrect() throws IOException {
    XmlMapper xmlMapper = new XmlMapper();
    SimpleBean value = xmlMapper.readValue("<SimpleBean><x>1</x><y>2</y></SimpleBean>", SimpleBean.class);
    assertTrue(value.getX() == 1 && value.getY() == 2);
}
Also used : XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper) Test(org.junit.Test)

Example 45 with XmlMapper

use of com.fasterxml.jackson.dataformat.xml.XmlMapper in project alluxio by Alluxio.

the class TestCase method run.

/**
 * Runs the test case.
 */
public void run() throws Exception {
    String expected = "";
    if (mExpectedResult != null) {
        switch(mOptions.getContentType()) {
            case TestCaseOptions.JSON_CONTENT_TYPE:
                {
                    ObjectMapper mapper = new ObjectMapper();
                    if (mOptions.isPrettyPrint()) {
                        expected = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(mExpectedResult);
                    } else {
                        expected = mapper.writeValueAsString(mExpectedResult);
                    }
                    break;
                }
            case TestCaseOptions.XML_CONTENT_TYPE:
                {
                    XmlMapper mapper = new XmlMapper();
                    if (mOptions.isPrettyPrint()) {
                        expected = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(mExpectedResult);
                    } else {
                        expected = mapper.writeValueAsString(mExpectedResult);
                    }
                    break;
                }
            default:
                throw new InvalidArgumentException("Invalid content type in TestCaseOptions!");
        }
    }
    String result = call();
    Assert.assertEquals(mEndpoint, expected, result);
}
Also used : InvalidArgumentException(alluxio.exception.status.InvalidArgumentException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper)

Aggregations

XmlMapper (com.fasterxml.jackson.dataformat.xml.XmlMapper)62 IOException (java.io.IOException)19 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)15 Test (org.junit.Test)15 InputStream (java.io.InputStream)9 File (java.io.File)8 HttpResponse (org.apache.http.HttpResponse)8 JsonNode (com.fasterxml.jackson.databind.JsonNode)7 JacksonXmlModule (com.fasterxml.jackson.dataformat.xml.JacksonXmlModule)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 Test (org.junit.jupiter.api.Test)5 BadRequestException (ninja.exceptions.BadRequestException)4 EntityReferences (org.apache.cloudstack.backup.veeam.api.EntityReferences)4 Ref (org.apache.cloudstack.backup.veeam.api.Ref)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 AfterburnerModule (com.fasterxml.jackson.module.afterburner.AfterburnerModule)2 FileInputStream (java.io.FileInputStream)2