-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added an easier way to serialize simple objects using gson since this…
… is wideley used in the platform and core library.
- Loading branch information
1 parent
49700f4
commit 0843a1d
Showing
2 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package org.hobbit.core.rabbit; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.google.gson.Gson; | ||
|
||
/** | ||
* A class that offers simple utility methods that hide some steps that are | ||
* necessary to transform java objects into JSON containing byte arrays and vice | ||
* versa. | ||
* | ||
* @author Michael Röder ([email protected]) | ||
* | ||
*/ | ||
public class GsonUtils { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(GsonUtils.class); | ||
|
||
/** | ||
* Serialize a Java object as JSON using the given {@link Gson} instance and the | ||
* {@link RabbitMQUtils} class. | ||
* | ||
* @param <T> The class of the given object | ||
* @param gson The Gson instance used for the serialization as JSON | ||
* @param object The object that should be serialized | ||
* @return The serialized object as JSON in a byte representation or null if the | ||
* given object was null | ||
*/ | ||
public static <T> byte[] serializeObjectWithGson(Gson gson, T object) { | ||
if (object != null) { | ||
return RabbitMQUtils.writeString(gson.toJson(object)); | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Deserialize a Java data object that was received as JSON with a command. | ||
* First, the given byte array will be transformed into a String using the | ||
* {@link RabbitMQUtils} class, before it will be deserialized using the | ||
* controller's {@link #gson} object. | ||
* | ||
* @param <T> The class that the data object should have. | ||
* @param data The byte array that has been received. | ||
* @param clazz The class that the data object should have. | ||
* @return The deserialized object or null if an error occurred | ||
*/ | ||
public static <T> T deserializeObjectWithGson(Gson gson, byte[] data, Class<? extends T> clazz) { | ||
if (data != null) { | ||
String dataString = RabbitMQUtils.readString(data); | ||
try { | ||
return gson.fromJson(dataString, clazz); | ||
} catch (Exception e) { | ||
LOGGER.error("Error while parsing JSON data. Returning null."); | ||
} | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package org.hobbit.core.rabbit; | ||
|
||
import org.hobbit.core.data.ErrorData; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import com.google.gson.Gson; | ||
|
||
public class GsonUtilsTest { | ||
|
||
@Test | ||
public void testErrorData() { | ||
ErrorData original = new ErrorData(); | ||
original.setErrorType("http://example.org/generalError"); | ||
original.setContainerId("123"); | ||
|
||
Gson gson = new Gson(); | ||
ErrorData received; | ||
|
||
received = GsonUtils.deserializeObjectWithGson(gson, GsonUtils.serializeObjectWithGson(gson, original), | ||
ErrorData.class); | ||
Assert.assertEquals(original, received); | ||
|
||
original.setLabel("Example label"); | ||
received = GsonUtils.deserializeObjectWithGson(gson, GsonUtils.serializeObjectWithGson(gson, original), | ||
ErrorData.class); | ||
Assert.assertEquals(original, received); | ||
|
||
original.setDescription("Some description"); | ||
received = GsonUtils.deserializeObjectWithGson(gson, GsonUtils.serializeObjectWithGson(gson, original), | ||
ErrorData.class); | ||
Assert.assertEquals(original, received); | ||
|
||
original.setDetails("A lot of details...."); | ||
received = GsonUtils.deserializeObjectWithGson(gson, GsonUtils.serializeObjectWithGson(gson, original), | ||
ErrorData.class); | ||
Assert.assertEquals(original, received); | ||
} | ||
} |