mirror of
https://github.com/davidalves04/Trabalho-Pratico-SD.git
synced 2025-12-08 20:43:32 +00:00
115 lines
3.5 KiB
Java
115 lines
3.5 KiB
Java
package sd.serialization;
|
|
|
|
import com.google.gson.Gson;
|
|
import com.google.gson.GsonBuilder;
|
|
import com.google.gson.JsonSyntaxException;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
/**
|
|
* JSON-based implementation of {@link MessageSerializer} using Google's Gson library.
|
|
*
|
|
* This serializer converts objects to JSON format for transmission, providing:
|
|
* - Human-readable message format (easy debugging)
|
|
* - Cross-platform compatibility
|
|
* - Smaller message sizes compared to Java native serialization
|
|
* - Better security (no code execution during deserialization)
|
|
*
|
|
* The serializer is configured with pretty printing disabled by default for
|
|
* production use, but can be enabled for debugging purposes.
|
|
*
|
|
* Thread-safety: This class is thread-safe as Gson instances are thread-safe.
|
|
*
|
|
* @see MessageSerializer
|
|
*/
|
|
public class JsonMessageSerializer implements MessageSerializer {
|
|
|
|
private final Gson gson;
|
|
private final boolean prettyPrint;
|
|
|
|
/**
|
|
* Creates a new JSON serializer with default configuration (no pretty printing).
|
|
*/
|
|
public JsonMessageSerializer() {
|
|
this(false);
|
|
}
|
|
|
|
/**
|
|
* Creates a new JSON serializer with optional pretty printing.
|
|
*
|
|
* @param prettyPrint If true, JSON output will be formatted with indentation
|
|
*/
|
|
public JsonMessageSerializer(boolean prettyPrint) {
|
|
this.prettyPrint = prettyPrint;
|
|
GsonBuilder builder = new GsonBuilder();
|
|
|
|
if (prettyPrint) {
|
|
builder.setPrettyPrinting();
|
|
}
|
|
|
|
// Register custom type adapters here if needed
|
|
// builder.registerTypeAdapter(Vehicle.class, new VehicleAdapter());
|
|
|
|
this.gson = builder.create();
|
|
}
|
|
|
|
@Override
|
|
public byte[] serialize(Object object) throws SerializationException {
|
|
if (object == null) {
|
|
throw new IllegalArgumentException("Cannot serialize null object");
|
|
}
|
|
|
|
try {
|
|
String json = gson.toJson(object);
|
|
return json.getBytes(StandardCharsets.UTF_8);
|
|
} catch (Exception e) {
|
|
throw new SerializationException(
|
|
"Failed to serialize object of type " + object.getClass().getName(), e);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public <T> T deserialize(byte[] data, Class<T> clazz) throws SerializationException {
|
|
if (data == null) {
|
|
throw new IllegalArgumentException("Cannot deserialize null data");
|
|
}
|
|
if (clazz == null) {
|
|
throw new IllegalArgumentException("Class type cannot be null");
|
|
}
|
|
|
|
try {
|
|
String json = new String(data, StandardCharsets.UTF_8);
|
|
return gson.fromJson(json, clazz);
|
|
} catch (JsonSyntaxException e) {
|
|
throw new SerializationException(
|
|
"Failed to parse JSON for type " + clazz.getName(), e);
|
|
} catch (Exception e) {
|
|
throw new SerializationException(
|
|
"Failed to deserialize object of type " + clazz.getName(), e);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public String getName() {
|
|
return "JSON (Gson)";
|
|
}
|
|
|
|
/**
|
|
* Returns the underlying Gson instance for advanced usage.
|
|
*
|
|
* @return The Gson instance
|
|
*/
|
|
public Gson getGson() {
|
|
return gson;
|
|
}
|
|
|
|
/**
|
|
* Checks if pretty printing is enabled.
|
|
*
|
|
* @return true if pretty printing is enabled
|
|
*/
|
|
public boolean isPrettyPrint() {
|
|
return prettyPrint;
|
|
}
|
|
}
|