@Inject public PostConfigurer(final ObjectMapper mapper, final Set<Module> jacksonModules) { mapper.registerModules(jacksonModules); }
/** * Convenience method that is functionally equivalent to: *<code> * mapper.registerModules(mapper.findModules()); *</code> *<p> * As with {@link #findModules()}, no caching is done for modules, so care * needs to be taken to either create and share a single mapper instance; * or to cache introspected set of modules. * * @since 2.2 */ public ObjectMapper findAndRegisterModules() { return registerModules(findModules()); }
public JacksonJsonSupport(Module... modules) { if (modules != null && modules.length > 0) { objectMapper.registerModules(modules); } init(objectMapper); }
public static void update() { final String jsonSerialiserClass = System.getProperty(JSON_SERIALISER_CLASS_KEY, DEFAULT_SERIALISER_CLASS_NAME); final JSONSerialiser newInstance; try { newInstance = Class.forName(jsonSerialiserClass).asSubclass(JSONSerialiser.class).newInstance(); } catch (final InstantiationException | IllegalAccessException | ClassNotFoundException e) { throw new IllegalArgumentException("Property " + JSON_SERIALISER_CLASS_KEY + " must be set to a class that is a sub class of " + JSONSerialiser.class.getName() + ". This class is not valid: " + jsonSerialiserClass, e); } final String moduleFactories = System.getProperty(JSON_SERIALISER_MODULES, ""); final Set<String> factoryClasses = Sets.newHashSet(moduleFactories.split(",")); factoryClasses.remove(""); for (final String factoryClass : factoryClasses) { final JSONSerialiserModules factory; try { factory = Class.forName(factoryClass).asSubclass(JSONSerialiserModules.class).newInstance(); } catch (final InstantiationException | IllegalAccessException | ClassNotFoundException e) { throw new IllegalArgumentException("Property " + JSON_SERIALISER_MODULES + " must be set to a csv of classes that are a sub class of " + JSONSerialiserModules.class.getName() + ". These classes are not valid: " + factoryClass, e); } final List<Module> modules = factory.getModules(); if (null != modules) { newInstance.mapper.registerModules(modules); } } newInstance.mapper.configure( DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, Boolean.parseBoolean(System.getProperty(STRICT_JSON, STRICT_JSON_DEFAULT_STR)) ); instance = newInstance; LOGGER.debug("Updated json serialiser to use: {}, and modules: {}", jsonSerialiserClass, moduleFactories); }
private List<AggregatorFactory> getAggregatorList(String aggregatorJSON) { ComponentLog log = getLogger(); ObjectMapper mapper = new ObjectMapper(null); mapper.registerModule(new AggregatorsModule()); mapper.registerModules(Lists.newArrayList(new SketchModule().getJacksonModules())); mapper.registerModules(Lists.newArrayList(new ApproximateHistogramDruidModule().getJacksonModules())); try { return mapper.readValue( aggregatorJSON, new TypeReference<List<AggregatorFactory>>() { } ); } catch (IOException e) { log.error(e.getMessage(), e); return null; } }
objectMapper.registerModules(ObjectMapper.findModules(this.moduleClassLoader)); objectMapper.registerModules(this.modules);
@Before public void setup() { mapper = new ObjectMapper(); ClassLoader loader = getClass().getClassLoader(); mapper.registerModules(SecurityJackson2Modules.getModules(loader)); }
@Before public void setup() { mapper = new ObjectMapper(); ClassLoader loader = getClass().getClassLoader(); mapper.registerModules(SecurityJackson2Modules.getModules(loader)); }
objectMapper.registerModules(ObjectMapper.findModules(this.moduleClassLoader)); objectMapper.registerModules(this.modules);
@Test public void shouldUpdateInstanceWithCustomProperties() throws Exception { // Given TestCustomJsonSerialiser1.mapper = mock(ObjectMapper.class); System.setProperty(JSONSerialiser.JSON_SERIALISER_CLASS_KEY, TestCustomJsonSerialiser1.class.getName()); TestCustomJsonModules1.modules = Arrays.asList( mock(Module.class), mock(Module.class) ); TestCustomJsonModules2.modules = Arrays.asList( mock(Module.class), mock(Module.class) ); System.setProperty(JSONSerialiser.JSON_SERIALISER_MODULES, TestCustomJsonModules1.class.getName() + "," + TestCustomJsonModules2.class.getName()); System.setProperty(JSONSerialiser.STRICT_JSON, "false"); // When JSONSerialiser.update(); // Then assertEquals(TestCustomJsonSerialiser1.class, JSONSerialiser.getInstance().getClass()); assertSame(TestCustomJsonSerialiser1.mapper, JSONSerialiser.getMapper()); verify(TestCustomJsonSerialiser1.mapper).registerModules(TestCustomJsonModules1.modules); verify(TestCustomJsonSerialiser1.mapper).registerModules(TestCustomJsonModules2.modules); verify(TestCustomJsonSerialiser1.mapper).configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); }
@Test public void shouldUpdateInstanceTwiceWithCustomProperties() throws Exception { // Given TestCustomJsonSerialiser1.mapper = mock(ObjectMapper.class); TestCustomJsonSerialiser2.mapper = mock(ObjectMapper.class); TestCustomJsonModules1.modules = Arrays.asList( mock(Module.class), mock(Module.class) ); TestCustomJsonModules2.modules = Arrays.asList( mock(Module.class), mock(Module.class) ); // When - initial update JSONSerialiser.update(TestCustomJsonSerialiser1.class.getName(), TestCustomJsonModules1.class.getName(), false); // Then assertEquals(TestCustomJsonSerialiser1.class, JSONSerialiser.getInstance().getClass()); assertSame(TestCustomJsonSerialiser1.mapper, JSONSerialiser.getMapper()); verify(TestCustomJsonSerialiser1.mapper).registerModules(TestCustomJsonModules1.modules); verify(TestCustomJsonSerialiser1.mapper, never()).registerModules(TestCustomJsonModules2.modules); verify(TestCustomJsonSerialiser1.mapper).configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); // When - second update JSONSerialiser.update(TestCustomJsonSerialiser2.class.getName(), TestCustomJsonModules2.class.getName(), true); // Then assertEquals(TestCustomJsonSerialiser2.class, JSONSerialiser.getInstance().getClass()); assertSame(TestCustomJsonSerialiser2.mapper, JSONSerialiser.getMapper()); verify(TestCustomJsonSerialiser2.mapper).registerModules(TestCustomJsonModules1.modules); verify(TestCustomJsonSerialiser2.mapper).registerModules(TestCustomJsonModules2.modules); verify(TestCustomJsonSerialiser2.mapper).configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true); }
UberLinkDiscoverer() { this.mapper = new ObjectMapper(); this.mapper.registerModules(new Jackson2UberModule()); }
public static <T> T deserialize(String value, Class<T> cls, List<Module> modules) throws Exception{ ObjectMapper mapper = new ObjectMapper(); if (modules != null) { mapper.registerModules(modules); } return mapper.readValue(value, cls); }
public JacksonEncoder(Iterable<Module> modules) { this(new ObjectMapper() .setSerializationInclusion(JsonInclude.Include.NON_NULL) .configure(SerializationFeature.INDENT_OUTPUT, true) .registerModules(modules)); }
/** * Adds {@link Module Jackson modules} to the object mapper. * * @param modules the modules to add * @return this * @see ObjectMapper#registerModule(Module) */ default ConfigDataBuilder jacksonModules(Module... modules) { getObjectMapper().registerModules(modules); return this; }
public CartographerCoreBuilder withObjectMapperModules( final Module... modules ) { initDefaultObjectMapper(); logger.debug( "{} adding custom modules", objectMapper ); objectMapper.registerModules( modules ); return this; }
@Provides @Singleton protected ObjectMapper objectMapper(Config config) { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.registerModules(config.getModules()); for (Consumer<? super ObjectMapper> configurer : config.configurers) { configurer.accept(objectMapper); } return objectMapper; }
private JsonTransformer(Collection<Module> modules) { objectMapper = new ObjectMapper() .configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false) .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .registerModule(new Jdk8Module()) .registerModule(new JavaTimeModule()) .registerModule(new GuavaModule()) .registerModules(modules); }
@Test public void itUsesCustomDeserializerForWrappedRepeatedObject() throws IOException { ObjectMapper mapper = new ObjectMapper().registerModules(new ProtobufModule(), new CustomDeserializer()); String json = "{\"custom\":[123,456]}"; RepeatedCustomWrapper wrapper = mapper.readValue(json, RepeatedCustomWrapper.class); assertThat(wrapper.getCustomCount()).isEqualTo(2); assertThat(wrapper.getCustom(0)).isEqualTo(Custom.newBuilder().setValue(123).build()); assertThat(wrapper.getCustom(1)).isEqualTo(Custom.newBuilder().setValue(456).build()); }
@Test public void testNestedMessage() { ObjectMapper MAPPER = new ObjectMapper().registerModules(new ProtobufModule(), new SerializerModule(Nested.class)); AllFields allFields = AllFields.newBuilder().setNested(Nested.newBuilder().setString("test").build()).build(); String expected = allFields.getNested().toString(); String actual = MAPPER.valueToTree(allFields).get("nested").get("toString").textValue(); assertThat(actual).isEqualTo(expected); }