Skip to content

Commit

Permalink
Merge pull request #25 from lucasponce/HAWKULAR-382
Browse files Browse the repository at this point in the history
HAWKULAR-382 Replace gson library by jackson
  • Loading branch information
jmazzitelli committed Jul 6, 2015
2 parents 55833a1 + c0baf10 commit 56eefc6
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 36 deletions.
9 changes: 5 additions & 4 deletions hawkular-bus-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,14 @@
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson2-provider</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
*/
package org.hawkular.bus.common;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
* Basic information that is sent over the message bus.
Expand All @@ -37,8 +40,11 @@
*/
public abstract class BasicMessage {
// these are passed out-of-band of the message body - these attributes will therefore not be JSON encoded
@JsonIgnore
private MessageId _messageId;
@JsonIgnore
private MessageId _correlationId;
@JsonIgnore
private Map<String, String> _headers;

/**
Expand All @@ -50,8 +56,12 @@ public abstract class BasicMessage {
* @return the message object that was represented by the JSON string
*/
public static <T extends BasicMessage> T fromJSON(String json, Class<T> clazz) {
final Gson gson = createGsonBuilder();
return gson.fromJson(json, clazz);
final ObjectMapper mapper = new ObjectMapper();
try {
return mapper.readValue(json, clazz);
} catch (IOException e) {
throw new IllegalStateException("JSON message cannot be converted to object.", e);
}
}

/**
Expand All @@ -60,8 +70,17 @@ public static <T extends BasicMessage> T fromJSON(String json, Class<T> clazz) {
* @return JSON encoded data that represents this message.
*/
public String toJSON() {
final Gson gson = createGsonBuilder();
return gson.toJson(this);
final ObjectMapper mapper = new ObjectMapper();
mapper.setVisibilityChecker(mapper.getSerializationConfig().getDefaultVisibilityChecker()
.withFieldVisibility(JsonAutoDetect.Visibility.ANY)
.withGetterVisibility(JsonAutoDetect.Visibility.NONE)
.withSetterVisibility(JsonAutoDetect.Visibility.NONE)
.withCreatorVisibility(JsonAutoDetect.Visibility.NONE));
try {
return mapper.writeValueAsString(this);
} catch (JsonProcessingException e) {
throw new IllegalStateException("Object cannot be parsed as JSON.", e);
}
}

protected BasicMessage() {
Expand Down Expand Up @@ -149,8 +168,4 @@ public String toString() {
str.append("]]");
return str.toString();
}

protected static Gson createGsonBuilder() {
return new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
public class MessageId {
private final String id;

public MessageId() {
this.id = null;
}

public MessageId(String id) {
if (id == null || id.length() == 0) {
throw new IllegalArgumentException("ID cannot be null or empty");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
*/
package org.hawkular.bus.common;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.Expose;
import java.io.IOException;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
* A message that contains a complex object, which gets serialized into JSON.
Expand All @@ -29,7 +32,7 @@
* @author John Mazzitelli
*/
public class ObjectMessage extends BasicMessage {
@Expose
@JsonInclude
private String message; // the object in JSON form
private Class<?> objectClass; // the ad-hoc class that this object message represents

Expand All @@ -43,9 +46,19 @@ public ObjectMessage(Object object) {
}
setObjectClass(object.getClass());

final Gson gson = new GsonBuilder().create();
final String msg = gson.toJson(object);
setMessage(msg);
final ObjectMapper mapper = new ObjectMapper();
mapper.setVisibilityChecker(mapper.getSerializationConfig().getDefaultVisibilityChecker()
.withFieldVisibility(JsonAutoDetect.Visibility.ANY)
.withGetterVisibility(JsonAutoDetect.Visibility.NONE)
.withSetterVisibility(JsonAutoDetect.Visibility.NONE)
.withCreatorVisibility(JsonAutoDetect.Visibility.NONE));
final String msg;
try {
msg = mapper.writeValueAsString(object);
setMessage(msg);
} catch (JsonProcessingException e) {
throw new IllegalStateException("Object cannot be parsed as JSON.", e);
}
}

public ObjectMessage(Class<?> clazz) {
Expand Down Expand Up @@ -87,7 +100,11 @@ public Object getObject() {
throw new IllegalStateException("Do not know what the class is that represents the JSON data");
}

final Gson gson = new GsonBuilder().create();
return gson.fromJson(getMessage(), clazz);
final ObjectMapper mapper = new ObjectMapper();
try {
return mapper.readValue(getMessage(), clazz);
} catch (IOException e) {
throw new IllegalStateException("JSON message cannot be converted to object.", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,17 @@
import java.util.HashMap;
import java.util.Map;

import com.google.gson.annotations.Expose;

import com.fasterxml.jackson.annotation.JsonInclude;
/**
* A simple message that is sent over the message bus.
*/
public class SimpleBasicMessage extends BasicMessage {
// the basic message body - it will be exposed to the JSON output
@Expose
@JsonInclude
private String message;

// some optional additional details about the basic message
@Expose
@JsonInclude
private Map<String, String> details;

protected SimpleBasicMessage() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ public void testObjectMessageNoArgConstructor() {
assert false : "should have failed - didn't tell it the class";
} catch (IllegalStateException expected) {
}

msg.setMessage("foo");
/*
Jackson library needs quotes for single json string representation
*/
msg.setMessage("\"foo\"");
msg.setObjectClass(String.class);
Object o = msg.getObject();
assert o.getClass() == String.class;
Expand Down
4 changes: 4 additions & 0 deletions hawkular-bus-samples/hawkular-bus-sample-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
<artifactId>hawkular-bus-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson2-provider</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
6 changes: 5 additions & 1 deletion hawkular-bus-samples/hawkular-bus-sample-jsonschema/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
<artifactId>hawkular-bus-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson2-provider</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand All @@ -51,7 +55,7 @@
<configuration>
<sourceDirectory>${basedir}/src/main/resources/schema</sourceDirectory>
<targetPackage>org.hawkular.bus.sample.msg</targetPackage>
<annotationStyle>gson</annotationStyle>
<annotationStyle>jackson</annotationStyle>
<includeToString>false</includeToString>
<includeHashcodeAndEquals>false</includeHashcodeAndEquals>
<initializeCollections>true</initializeCollections>
Expand Down
6 changes: 6 additions & 0 deletions hawkular-bus-test-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson2-provider</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,21 @@
*/
package org.hawkular.bus.common.test;

import com.fasterxml.jackson.annotation.JsonInclude;
import java.util.Map;

import org.hawkular.bus.common.SimpleBasicMessage;

import com.google.gson.annotations.Expose;

/**
* Test subclass of BasicMessage.
*/
public class SpecificMessage extends SimpleBasicMessage {
@Expose
@JsonInclude
private final String specific;

public SpecificMessage() {
this.specific = null;
}

public SpecificMessage(String message, Map<String, String> details, String specific) {
super(message, details);
if (specific == null) {
Expand Down
2 changes: 1 addition & 1 deletion hawkular-nest/hawkular-nest-distro/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
</includeArtifactIds>
<stripVersion>true</stripVersion>
<stripClassifier>true</stripClassifier>
<overWriteIfNew>true</overWriteIfNew>
<overWriteIfNewer>true</overWriteIfNewer>
<excludeTransitive>true</excludeTransitive>
</configuration>
</execution>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
<resource-root path="${project.build.finalName}.jar" />
<resource-root path="hawkular-bus-common-${project.version}.jar" />
<resource-root path="hawkular-bus-mdb-${project.version}.jar" />
<resource-root path="gson-${version.com.google.code.gson}.jar" />
</resources>

<dependencies>
Expand Down

0 comments on commit 56eefc6

Please sign in to comment.