Skip to content

Instantly share code, notes, and snippets.

@pavolloffay
Created December 8, 2015 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pavolloffay/41eb9245064532fb4e90 to your computer and use it in GitHub Desktop.
Save pavolloffay/41eb9245064532fb4e90 to your computer and use it in GitHub Desktop.
Hawkular Bus serialization test
@Test
public void testA() throws Exception {
ObjectMapper objectMapper = new ObjectMapper();
InventoryJacksonConfig.configure(objectMapper);
Query relationships = Query.filter().with(RelationWith.name("__inPrediction")).with(RelationWith.sourceOfType
(Tenant.class)).with(RelationWith.targetOfType(Metric.class)).get();
String json = serialize(relationships, objectMapper);
Query queryFromJson = deserialize(json, Query.class, objectMapper);
}
private String serialize(Object object, ObjectMapper mapper) throws IOException {
StringWriter out = new StringWriter();
JsonGenerator gen = mapper.getFactory().createGenerator(out);
gen.writeObject(object);
gen.close();
out.close();
return out.toString();
}
private <T> T deserialize(String json, Class<T> type, ObjectMapper mapper) throws Exception {
JsonParser parser = mapper.getFactory().createParser(json);
return parser.readValueAs(type);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment