List of usage examples for com.fasterxml.jackson.databind.module SimpleModule SimpleModule
public SimpleModule()
From source file:com.corundumstudio.socketio.parser.JacksonJsonSupport.java
protected void init(ObjectMapper objectMapper) { SimpleModule module = new SimpleModule(); module.addDeserializer(Event.class, eventDeserializer); module.addDeserializer(JsonObject.class, jsonObjectDeserializer); module.addDeserializer(AckArgs.class, ackArgsDeserializer); objectMapper.registerModule(module); objectMapper.setSerializationInclusion(Include.NON_NULL); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); objectMapper.configure(SerializationFeature.WRITE_BIGDECIMAL_AS_PLAIN, true); // TODO If jsonObjectDeserializer will be not enough // TypeResolverBuilder<?> typer = new DefaultTypeResolverBuilder(DefaultTyping.NON_FINAL); // typer.init(JsonTypeInfo.Id.CLASS, null); // typer.inclusion(JsonTypeInfo.As.PROPERTY); // typer.typeProperty(configuration.getJsonTypeFieldName()); // objectMapper.setDefaultTyping(typer); }
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);/*w w w . j a va 2 s. c o m*/ Record record = null; try { record = mapper.readValue(in, Record.class); } catch (IOException e) { e.printStackTrace(); } return record; }
From source file:org.apache.nifi.registry.client.impl.JerseyNiFiRegistryClient.java
private static JacksonJaxbJsonProvider jacksonJaxbJsonProvider() { JacksonJaxbJsonProvider jacksonJaxbJsonProvider = new JacksonJaxbJsonProvider(); ObjectMapper mapper = new ObjectMapper(); mapper.setPropertyInclusion(// ww w . jav a 2 s. c o m JsonInclude.Value.construct(JsonInclude.Include.NON_NULL, JsonInclude.Include.NON_NULL)); mapper.setAnnotationIntrospector(new JaxbAnnotationIntrospector(mapper.getTypeFactory())); // Ignore unknown properties so that deployed client remain compatible with future versions of NiFi Registry that add new fields mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); SimpleModule module = new SimpleModule(); module.addDeserializer(BucketItem[].class, new BucketItemDeserializer()); mapper.registerModule(module); jacksonJaxbJsonProvider.setMapper(mapper); return jacksonJaxbJsonProvider; }
From source file:com.intelligentsia.dowsers.entity.reference.ReferenceTest.java
@Test public void testSerialization() throws JsonParseException, JsonMappingException, IOException { final ObjectMapper mapper = JacksonSerializer.getMapper(); final SimpleModule module = new SimpleModule(); module.addSerializer(new ReferenceSerializer()); module.addDeserializer(Reference.class, new ReferenceDeSerializer()); mapper.registerModule(module);//from ww w.java 2s. com final StringWriter writer = new StringWriter(); final Reference reference = Reference.parseString( "urn:dowsers:com.intelligentsia.dowsers.entity.model.Person:identity#4c8b03dd-908a-4cad-8d48-3c7277d44ac9"); mapper.writeValue(writer, reference); final String result = writer.toString(); final Reference reference2 = mapper.readValue(new StringReader(result), Reference.class); assertNotNull(reference2); assertEquals(reference, reference2); }
From source file:eu.trentorise.opendata.semtext.jackson.test.SemTextModuleTest.java
@Test public void testMetadataSerializationSimple() throws JsonProcessingException, IOException { ObjectMapper om = new ObjectMapper(); // register all required modules into the Jackson Object Mapper SemTextModule.registerModulesInto(om); // declare that metadata under namespace 'testns' in SemText objects should be deserialized into a Date object SemTextModule.registerMetadata(SemText.class, "testns", Date.class); // Let's say MyMetadata is tricky to deserialize, so we tell Jackson how to deserialize it with a mixin annotation om.registerModule(new SimpleModule() { {//from w ww.j ava 2 s .c o m setMixInAnnotation(MyMetadata.class, MyMetadataJackson.class); } }); String json = om.writeValueAsString(SemText.of("ciao").withMetadata("testns", new Date(123))); LOG.fine("json = " + json); SemText reconstructedSemText = om.readValue(json, SemText.class); Date reconstructedMetadata = (Date) reconstructedSemText.getMetadata("testns"); assert new Date(123).equals(reconstructedMetadata); }
From source file:com.corundumstudio.socketio.protocol.JacksonJsonSupport.java
protected void init(ObjectMapper objectMapper) { SimpleModule module = new SimpleModule(); module.setSerializerModifier(modifier); module.addDeserializer(Event.class, eventDeserializer); module.addDeserializer(AckArgs.class, ackArgsDeserializer); objectMapper.registerModule(module); objectMapper.setSerializationInclusion(Include.NON_NULL); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); objectMapper.configure(SerializationFeature.WRITE_BIGDECIMAL_AS_PLAIN, true); objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); }
From source file:eu.trentorise.opendata.semtext.jackson.test.SemTextModuleTest.java
@Test public void testMetadataSerializationComplex() throws JsonProcessingException, IOException { ObjectMapper om = new ObjectMapper(); // register all required modules into the Jackson Object Mapper SemTextModule.registerModulesInto(om); // declare that metadata under namespace 'testns' in SemText objects should be deserialized into a MyMetadata object SemTextModule.registerMetadata(SemText.class, "testns", MyMetadata.class); // Let's say MyMetadata is tricky to deserialize, so we tell Jackson how to deserialize it with a mixin annotation om.registerModule(new SimpleModule() { {// w w w . ja v a 2 s .c o m setMixInAnnotation(MyMetadata.class, MyMetadataJackson.class); } }); String json = om.writeValueAsString( SemText.of(Locale.ITALIAN, "ciao").withMetadata("testns", MyMetadata.of("hello"))); SemText reconstructedSemText = om.readValue(json, SemText.class); MyMetadata reconstructedMetadata = (MyMetadata) reconstructedSemText.getMetadata("testns"); assert MyMetadata.of("hello").equals(reconstructedMetadata); }
From source file:org.wisdom.monitor.extensions.jcr.script.JcrScriptExecutorExtension.java
public String listToJsonString(List list) throws IOException { final ByteArrayOutputStream out = new ByteArrayOutputStream(); final ObjectMapper mapper = new ObjectMapper(); SimpleModule module = new SimpleModule(); module.addSerializer(Event.class, new JcrEventSerializer()); // assuming serializer declares correct class to bind to mapper.registerModule(module);/* ww w. j a v a 2 s . c om*/ mapper.writeValue(out, list); final byte[] data = out.toByteArray(); return new String(data); }
From source file:io.klerch.alexa.state.model.AlexaStateModel.java
/** * Returns a json with key-value-pairs - one for each AlexaStateSave-annotated field in this model configured to be valid * in the given scope//from w ww . j av a 2 s . c o m * @param scope The scope a AlexaStateSave-annotated field must have or be part of to be considered in the returned json * @throws AlexaStateException Wraps all inner exceptions and gives you context related to handler and model * @return A json-string with key-value-pairs - one for each AlexaStateSave-annotated field in this model configured to be valid */ public String toJSON(final AlexaScope scope) throws AlexaStateException { // for each scope there is a custom json serializer so initialize the one which corresponds to the given scope final AlexaStateSerializer serializer = AlexaScope.APPLICATION.equals(scope) ? new AlexaAppStateSerializer() : AlexaScope.USER.equals(scope) ? new AlexaUserStateSerializer() : new AlexaSessionStateSerializer(); // associate a mapper with the serializer final ObjectMapper mapper = new ObjectMapper(); final SimpleModule module = new SimpleModule(); module.addSerializer(this.getClass(), serializer); mapper.registerModule(module); try { // serialize model which only contains those fields tagged with the given scope return mapper.writeValueAsString(this); } catch (JsonProcessingException e) { final String error = String.format("Error while serializing model of '%1$s' as Json.", this); log.error(error, e); throw AlexaStateException.create(error).withCause(e).withModel(this).build(); } }
From source file:org.apache.tinkerpop.gremlin.structure.IoTest.java
/** * This is just a serialization check.// ww w . ja v a 2 s . c om */ @Test @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = FEATURE_USER_SUPPLIED_IDS) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = FEATURE_ANY_IDS) public void shouldProperlySerializeDeserializeCustomIdWithGraphSON() throws Exception { final UUID id = UUID.fromString("AF4B5965-B176-4552-B3C1-FBBE2F52C305"); g.addVertex(T.id, new CustomId("vertex", id)); final SimpleModule module = new SimpleModule(); module.addSerializer(CustomId.class, new CustomId.CustomIdJacksonSerializer()); module.addDeserializer(CustomId.class, new CustomId.CustomIdJacksonDeserializer()); final GraphWriter writer = g.io().graphSONWriter() .mapper(g.io().graphSONMapper().addCustomModule(module).embedTypes(true).create()).create(); try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) { writer.writeGraph(baos, g); final JsonNode jsonGraph = new ObjectMapper().readTree(baos.toByteArray()); final JsonNode onlyVertex = jsonGraph.findValues(GraphSONTokens.VERTICES).get(0).get(0); final JsonNode idValue = onlyVertex.get(GraphSONTokens.ID); assertTrue(idValue.has("cluster")); assertEquals("vertex", idValue.get("cluster").asText()); assertTrue(idValue.has("elementId")); assertEquals("AF4B5965-B176-4552-B3C1-FBBE2F52C305".toLowerCase(), idValue.get("elementId").asText()); // reusing the same config used for creation of "g". final Configuration configuration = graphProvider.newGraphConfiguration("g2", this.getClass(), name.getMethodName()); graphProvider.clear(configuration); final Graph g2 = graphProvider.openTestGraph(configuration); try (final InputStream is = new ByteArrayInputStream(baos.toByteArray())) { final GraphReader reader = g.io().graphSONReader() .mapper(g.io().graphSONMapper().embedTypes(true).addCustomModule(module).create()).create(); reader.readGraph(is, g2); } final Vertex v2 = g2.V().next(); final CustomId customId = (CustomId) v2.id(); assertEquals(id, customId.getElementId()); assertEquals("vertex", customId.getCluster()); // need to manually close the "g2" instance graphProvider.clear(g2, configuration); } }