List of usage examples for com.fasterxml.jackson.databind ObjectMapper registerModule
public ObjectMapper registerModule(Module module)
From source file:com.mastfrog.acteur.mongo.impl.JacksonMongoDB.java
@Override public ObjectMapper configure(ObjectMapper om) { om.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY); SimpleModule sm = new SimpleModule("mongo", new Version(1, 0, 0, null, "com.timboudreau", "trackerapi")); sm.addSerializer(new ObjectIdSerializer()); sm.addDeserializer(ObjectId.class, new ObjectIdDeserializer()); om.registerModule(sm); return om;/*from w w w. ja v a 2 s .c o m*/ }
From source file:com.bazaarvoice.jolt.jsonUtil.testdomain.five.MappingTest5.java
@Test public void testPolymorphicJacksonSerializationAndDeserialization() { ObjectMapper mapper = new ObjectMapper(); SimpleModule testModule = new SimpleModule("testModule", new Version(1, 0, 0, null, null, null)) .addDeserializer(QueryFilter5.class, new QueryFilter5Deserializer()); mapper.registerModule(testModule); // Verifying that we can pass in a custom Mapper and create a new JsonUtil JsonUtil jsonUtil = JsonUtils.customJsonUtil(mapper); String testFixture = "/jsonUtils/testdomain/five/queryFilter-realAndLogical5.json"; // TEST JsonUtil and our deserialization logic QueryFilter5 queryFilter = jsonUtil.classpathToType(testFixture, new TypeReference<QueryFilter5>() { });//from w w w .jav a 2 s .co m // Make sure the hydrated QFilter looks right AssertJUnit.assertTrue(queryFilter instanceof LogicalFilter5); LogicalFilter5 andFilter = (LogicalFilter5) queryFilter; AssertJUnit.assertEquals(Operator.AND, andFilter.getOperator()); AssertJUnit.assertNotNull(andFilter.getValues()); AssertJUnit.assertEquals(3, andFilter.getValues().size()); // Make sure one of the top level RealFilters looks right QueryFilter5 productIdFilter = andFilter.getValues().get(1); AssertJUnit.assertTrue(productIdFilter instanceof StringRealFilter5); StringRealFilter5 stringRealProductIdFilter = (StringRealFilter5) productIdFilter; AssertJUnit.assertEquals(Field.PRODUCTID, stringRealProductIdFilter.getField()); AssertJUnit.assertEquals(Operator.EQ, stringRealProductIdFilter.getOperator()); AssertJUnit.assertEquals("Acme-1234", stringRealProductIdFilter.getValues().get(0)); // Make sure the nested OR looks right QueryFilter5 orFilter = andFilter.getValues().get(2); AssertJUnit.assertTrue(orFilter instanceof LogicalFilter5); LogicalFilter5 realOrFilter = (LogicalFilter5) orFilter; AssertJUnit.assertEquals(Operator.OR, realOrFilter.getOperator()); AssertJUnit.assertEquals(2, realOrFilter.getValues().size()); // Make sure nested AND looks right QueryFilter5 nestedAndFilter = realOrFilter.getValues().get(1); AssertJUnit.assertTrue(nestedAndFilter instanceof LogicalFilter5); AssertJUnit.assertEquals(Operator.AND, nestedAndFilter.getOperator()); AssertJUnit.assertEquals(3, nestedAndFilter.getValues().size()); // SERIALIZE TO STRING to test serialization logic String unitTestString = jsonUtil.toJsonString(queryFilter); // LOAD and Diffy the plain vanilla JSON versions of the documents Map<String, Object> actual = JsonUtils.jsonToMap(unitTestString); Map<String, Object> expected = JsonUtils.classpathToMap(testFixture); // Diffy the vanilla versions Diffy.Result result = diffy.diff(expected, actual); if (!result.isEmpty()) { AssertJUnit.fail("Failed.\nhere is a diff:\nexpected: " + JsonUtils.toJsonString(result.expected) + "\n actual: " + JsonUtils.toJsonString(result.actual)); } }
From source file:com.aerospike.cache.ConfigReader.java
/** * Read configuration from given file// ww w. j a v a2 s . c o m * * @param configresourcename * @return * @throws IOException */ public AerospikeCacheConfig getConfiguration(String configresourcename) throws IOException { final ObjectMapper mapper = new ObjectMapper(); log.debug("Reading cache config frm {}.", configresourcename); @Cleanup final InputStream istream = getClass().getClassLoader().getResourceAsStream(configresourcename); SimpleModule module = new SimpleModule(); module.addDeserializer(Host.class, new HostDeserializer()); mapper.registerModule(module); return mapper.readValue(istream, AerospikeCacheConfig.class); }
From source file:com.aerospike.session.impl.ConfigReader.java
/** * Read configuration from the filename provided by the user. *///from ww w. j av a 2 s . co m public AerospikeSessionStoreConfig getConfiguration(String configresourcename) throws IOException { final ObjectMapper mapper = new ObjectMapper(); log.debug("Reading store config from {}.", configresourcename); @Cleanup final InputStream stream = getClass().getClassLoader().getResourceAsStream(configresourcename); SimpleModule module = new SimpleModule(); module.addDeserializer(Host.class, new HostDeserializer()); mapper.registerModule(module); return mapper.readValue(stream, AerospikeSessionStoreConfig.class); }
From source file:org.jberet.support.io.MappingJsonFactoryObjectFactory.java
static void configureCustomSerializersAndDeserializers(final ObjectMapper objectMapper, final String customSerializers, final String customDeserializers, final String customDataTypeModules, final ClassLoader classLoader) throws Exception { if (customDeserializers != null || customSerializers != null) { final SimpleModule simpleModule = new SimpleModule("custom-serializer-deserializer-module"); if (customSerializers != null) { final StringTokenizer st = new StringTokenizer(customSerializers, ", "); while (st.hasMoreTokens()) { final Class<?> aClass = classLoader.loadClass(st.nextToken()); simpleModule.addSerializer(aClass, (JsonSerializer) aClass.newInstance()); }//www .j a v a 2 s . com } if (customDeserializers != null) { final StringTokenizer st = new StringTokenizer(customDeserializers, ", "); while (st.hasMoreTokens()) { final Class<?> aClass = classLoader.loadClass(st.nextToken()); simpleModule.addDeserializer(aClass, (JsonDeserializer) aClass.newInstance()); } } objectMapper.registerModule(simpleModule); } if (customDataTypeModules != null) { final StringTokenizer st = new StringTokenizer(customDataTypeModules, ", "); while (st.hasMoreTokens()) { final Class<?> aClass = classLoader.loadClass(st.nextToken()); objectMapper.registerModule((Module) aClass.newInstance()); } } }
From source file:org.dice_research.topicmodeling.lang.DictionaryDictCC.java
@Override public void saveAsObjectFile() { if (localDictionaryChanged) { FileOutputStream fout = null; try {// www . j a v a 2s .c om String filename = DICTIONARY_FILE_NAME; if (System.getProperty(SYSTEM_PROPERTY_NAME_FILE_APPENDIX) != null) { filename = filename.replace("$APPENDIX$", System.getProperty(SYSTEM_PROPERTY_NAME_FILE_APPENDIX)); } else { filename = filename.replace("$APPENDIX$", ""); } fout = new FileOutputStream(filename); ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new HppcModule()); mapper.writeValue(fout, dictionary); localDictionaryChanged = false; } catch (IOException e) { LOGGER.error("Error while writing the serialized dictionary to file.", e); } finally { try { fout.close(); } catch (Exception e) { } } } }
From source file:it.eng.spagobi.behaviouralmodel.lov.service.GridMetadataContainer.java
/** * JSON serializer for this object/*from w w w .j a va2 s . c o m*/ * @return the network serialized * @throws SerializationException */ @JsonIgnore public String toJSONString() throws it.eng.spagobi.commons.serializer.SerializationException { ObjectMapper mapper = new ObjectMapper(); String s = ""; try { SimpleModule simpleModule = new SimpleModule("SimpleModule", new Version(1, 0, 0, null)); simpleModule.addSerializer(GridMetadataContainer.class, new GridMetadataContainerJSONSerializer()); mapper.registerModule(simpleModule); s = mapper.writeValueAsString((GridMetadataContainer) this); } catch (Exception e) { throw new org.apache.commons.lang.SerializationException("Error serializing the network", e); } s = StringEscapeUtils.unescapeJavaScript(s); return s; }
From source file:com.hurence.logisland.serializer.JsonSerializer.java
@Override public Record deserialize(InputStream in) throws RecordSerializationException { ObjectMapper mapper = new ObjectMapper(); SimpleModule module = new SimpleModule(); module.addDeserializer(Record.class, new EventDeserializer()); mapper.registerModule(module); Record record = null;/*from ww w .ja va 2s .c om*/ try { record = mapper.readValue(in, Record.class); } catch (IOException e) { e.printStackTrace(); } return record; }
From source file:com.hurence.logisland.serializer.JsonSerializer.java
@Override public void serialize(OutputStream out, Record record) throws RecordSerializationException { ObjectMapper mapper = new ObjectMapper(); SimpleModule module = new SimpleModule(); module.addSerializer(StandardRecord.class, new EventSerializer()); mapper.registerModule(module); //map json to student try {/*from ww w . j a v a2 s . c o m*/ mapper.enable(SerializationFeature.INDENT_OUTPUT); mapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES); String jsonString = mapper.writeValueAsString(record); out.write(jsonString.getBytes()); out.flush(); } catch (IOException e) { e.printStackTrace(); } }