Search in sources :

Example 91 with SimpleModule

use of com.fasterxml.jackson.databind.module.SimpleModule in project jackson-databind by FasterXML.

the class AbstracTypeMapping1186Test method testDeserializeMyContainer.

public void testDeserializeMyContainer() throws Exception {
    Module module = new SimpleModule().addAbstractTypeMapping(IContainer.class, MyContainer.class);
    final ObjectMapper mapper = new ObjectMapper().registerModule(module);
    String json = "{\"ts\": [ { \"msg\": \"hello\"} ] }";
    final Object o = mapper.readValue(json, mapper.getTypeFactory().constructParametricType(IContainer.class, MyObject.class));
    assertEquals(MyContainer.class, o.getClass());
    MyContainer<?> myc = (MyContainer<?>) o;
    assertEquals(1, myc.ts.size());
    Object value = myc.ts.get(0);
    assertEquals(MyObject.class, value.getClass());
}
Also used : SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 92 with SimpleModule

use of com.fasterxml.jackson.databind.module.SimpleModule in project jackson-databind by FasterXML.

the class TypeResolverTest method testSubtypeResolution.

public static void testSubtypeResolution() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    SimpleAbstractTypeResolver resolver = new SimpleAbstractTypeResolver();
    resolver.addMapping(Map.class, MyMap.class);
    SimpleModule basicModule = new SimpleModule();
    basicModule.setAbstractTypes(resolver);
    mapper.registerModule(basicModule);
    String value = "{\"z\": {\"zz\": {\"a\": 42}}}";
    A a = mapper.readValue(value, A.class);
    Map map = a.getMap();
    assertEquals(MyMap.class, map.getClass());
    Object ob = map.get("zz");
    assertEquals(B.class, ob.getClass());
}
Also used : SimpleAbstractTypeResolver(com.fasterxml.jackson.databind.module.SimpleAbstractTypeResolver) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 93 with SimpleModule

use of com.fasterxml.jackson.databind.module.SimpleModule in project jackson-databind by FasterXML.

the class TestMixinMerging method testDisappearingMixins515.

/*
    /**********************************************************
    /* Unit tests
    /**********************************************************
     */
// for [Issue#515]
public void testDisappearingMixins515() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    mapper.disable(MapperFeature.ALLOW_FINAL_FIELDS_AS_MUTATORS).disable(MapperFeature.AUTO_DETECT_FIELDS).disable(MapperFeature.AUTO_DETECT_GETTERS).disable(MapperFeature.AUTO_DETECT_IS_GETTERS).disable(MapperFeature.INFER_PROPERTY_MUTATORS);
    SimpleModule module = new SimpleModule("Test");
    module.setMixInAnnotation(Person.class, PersonMixin.class);
    mapper.registerModule(module);
    assertEquals("{\"city\":\"Seattle\"}", mapper.writeValueAsString(new PersonImpl()));
}
Also used : SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 94 with SimpleModule

use of com.fasterxml.jackson.databind.module.SimpleModule in project retrofit by square.

the class JacksonConverterFactoryTest method setUp.

@Before
public void setUp() {
    SimpleModule module = new SimpleModule();
    module.addSerializer(AnInterface.class, new AnInterfaceSerializer());
    module.addDeserializer(AnInterface.class, new AnInterfaceDeserializer());
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(module);
    mapper.configure(MapperFeature.AUTO_DETECT_GETTERS, false);
    mapper.configure(MapperFeature.AUTO_DETECT_SETTERS, false);
    mapper.configure(MapperFeature.AUTO_DETECT_IS_GETTERS, false);
    mapper.setVisibilityChecker(mapper.getSerializationConfig().getDefaultVisibilityChecker().withFieldVisibility(JsonAutoDetect.Visibility.ANY));
    Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).addConverterFactory(JacksonConverterFactory.create(mapper)).build();
    service = retrofit.create(Service.class);
}
Also used : Retrofit(retrofit2.Retrofit) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Before(org.junit.Before)

Example 95 with SimpleModule

use of com.fasterxml.jackson.databind.module.SimpleModule in project spring-boot by spring-projects.

the class JsonObjectDeserializerTests method deserializeObjectShouldReadJson.

@Test
public void deserializeObjectShouldReadJson() throws Exception {
    Deserializer deserializer = new NameAndAgeJsonComponent.Deserializer();
    SimpleModule module = new SimpleModule();
    module.addDeserializer(NameAndAge.class, deserializer);
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(module);
    NameAndAge nameAndAge = mapper.readValue("{\"name\":\"spring\",\"age\":100}", NameAndAge.class);
    assertThat(nameAndAge.getName()).isEqualTo("spring");
    assertThat(nameAndAge.getAge()).isEqualTo(100);
}
Also used : Deserializer(org.springframework.boot.jackson.NameAndAgeJsonComponent.Deserializer) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Aggregations

SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)112 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)41 Test (org.junit.Test)13 Version (com.fasterxml.jackson.core.Version)8 ObjectMapperProvider (io.airlift.json.ObjectMapperProvider)8 Test (org.testng.annotations.Test)8 TestingTypeDeserializer (com.facebook.presto.spi.type.TestingTypeDeserializer)7 TestingTypeManager (com.facebook.presto.spi.type.TestingTypeManager)7 Type (com.facebook.presto.spi.type.Type)7 Block (com.facebook.presto.spi.block.Block)6 TestingBlockEncodingSerde (com.facebook.presto.spi.block.TestingBlockEncodingSerde)6 TestingBlockJsonSerde (com.facebook.presto.spi.block.TestingBlockJsonSerde)6 TypeReference (com.fasterxml.jackson.core.type.TypeReference)5 IOException (java.io.IOException)5 JsonParser (com.fasterxml.jackson.core.JsonParser)4 JsonFactory (com.fasterxml.jackson.core.JsonFactory)3 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)3 JsonDeserializer (com.fasterxml.jackson.databind.JsonDeserializer)3 Module (com.fasterxml.jackson.databind.Module)3 SimpleSerializers (com.fasterxml.jackson.databind.module.SimpleSerializers)3