Skip to content

Commit

Permalink
Added an easier way to serialize simple objects using gson since this…
Browse files Browse the repository at this point in the history
… is wideley used in the platform and core library.
  • Loading branch information
MichaelRoeder committed Dec 8, 2023
1 parent 49700f4 commit 0843a1d
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/main/java/org/hobbit/core/rabbit/GsonUtils.java
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;
}
}
39 changes: 39 additions & 0 deletions src/test/java/org/hobbit/core/rabbit/GsonUtilsTest.java
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);
}
}

0 comments on commit 0843a1d

Please sign in to comment.