Skip to content

Commit

Permalink
feat: add gson adapters for skin overlay data classes
Browse files Browse the repository at this point in the history
- Add PartTypeAdapter, SkinTypeAdapter, UserTypeAdapter, SPropertyTypeAdapter, SerializableBufferedImageTypeAdapter, and SkinPartsTypeAdapter classes
- Implement serialization and deserialization methods for each adapter
- Add toJson and fromJson static methods for convenience
  • Loading branch information
GeorgeV220 committed Jan 3, 2024
1 parent 0d6c609 commit 49cceff
Show file tree
Hide file tree
Showing 6 changed files with 506 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.georgev22.skinoverlay.storage.gsonAdapters;

import com.georgev22.skinoverlay.handler.skin.Part;
import com.georgev22.skinoverlay.utilities.SerializableBufferedImage;
import com.google.gson.*;
import org.jetbrains.annotations.NotNull;

import java.lang.reflect.Type;

/**
* Gson TypeAdapter for serializing and deserializing {@link Part} objects.
* <p>
* This adapter is responsible for converting {@link Part} objects to and from JSON format.
* It handles the serialization and deserialization of the part's name, image, position, dimensions, and emptiness status.
*/
public class PartTypeAdapter implements JsonSerializer<Part>, JsonDeserializer<Part> {

/**
* Deserializes the JSON element to a Part object.
*
* @param jsonElement The JSON element containing part information.
* @param typeOfT The type of the Object to deserialize to.
* @param jsonDeserializationContext The context for deserialization.
* @return The deserialized Part object.
* @throws JsonParseException If there is an issue during the deserialization process.
*/
@Override
public Part deserialize(@NotNull JsonElement jsonElement, Type typeOfT, @NotNull JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
JsonObject jsonObject = jsonElement.getAsJsonObject();

String name = jsonObject.get("name").getAsString();
SerializableBufferedImage image = jsonDeserializationContext.deserialize(jsonObject.get("bufferedImage"), SerializableBufferedImage.class);
int x = jsonObject.get("x").getAsInt();
int y = jsonObject.get("y").getAsInt();
int width = jsonObject.get("width").getAsInt();
int height = jsonObject.get("height").getAsInt();
boolean isEmpty = jsonObject.get("isEmpty").getAsBoolean();

return new Part(name, image, x, y, width, height, isEmpty);
}

/**
* Serializes the Part object to a JSON element.
*
* @param src The Part object to serialize.
* @param typeOfSrc The actual type (fully genericized version) of the source object.
* @param jsonSerializationContext The context for serialization.
* @return The serialized JSON element.
*/
@Override
public JsonElement serialize(@NotNull Part src, Type typeOfSrc, @NotNull JsonSerializationContext jsonSerializationContext) {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("name", src.name());
jsonObject.add("bufferedImage", jsonSerializationContext.serialize(src.image()));
jsonObject.addProperty("x", src.x());
jsonObject.addProperty("y", src.y());
jsonObject.addProperty("width", src.width());
jsonObject.addProperty("height", src.height());
jsonObject.addProperty("isEmpty", src.isEmpty());
return jsonObject;
}

/**
* Converts a {@link Part} object to its JSON representation.
*
* @param part The {@link Part} object to be converted.
* @return A JSON string representing the serialized {@link Part} object.
* @throws IllegalArgumentException If the provided {@link Part} object is null.
*/
public static String toJson(Part part) {
if (part == null) throw new IllegalArgumentException("Part cannot be null");
return part.toJson();
}

/**
* Converts a JSON string to a {@link Part} object.
*
* @param json The JSON string representing the serialized {@link Part} object.
* @return A {@link Part} object deserialized from the provided JSON string.
* @throws IllegalArgumentException If the provided JSON string is null or empty.
*/
public static Part fromJson(String json) {
if (json == null) throw new IllegalArgumentException("Json cannot be null");
if (json.isEmpty()) throw new IllegalArgumentException("Json cannot be empty");
return Part.fromJson(json);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.georgev22.skinoverlay.storage.gsonAdapters;

import com.georgev22.skinoverlay.handler.SProperty;
import com.google.gson.*;
import org.jetbrains.annotations.NotNull;

import java.lang.reflect.Type;

/**
* Gson TypeAdapter for serializing and deserializing {@link SProperty} objects.
* <p>
* This adapter is responsible for converting {@link SProperty} objects to and from JSON format.
* It handles the serialization and deserialization of the property's name, value, and signature.
*/
public class SPropertyTypeAdapter implements
JsonSerializer<SProperty>,
JsonDeserializer<SProperty> {

/**
* Deserializes the JSON element to an {@code SProperty}.
*
* @param jsonElement The JSON element containing name, value, and signature information.
* @param typeOfT The type of the Object to deserialize to.
* @param jsonDeserializationContext The context for deserialization.
* @return The deserialized {@code SProperty}.
*/
@Override
public @NotNull SProperty deserialize(@NotNull JsonElement jsonElement, Type typeOfT, JsonDeserializationContext jsonDeserializationContext) {
JsonObject jsonObject = jsonElement.getAsJsonObject();

String name = jsonObject.get("name").getAsString();
String value = jsonObject.get("value").getAsString();
String signature = jsonObject.get("signature").getAsString();

return new SProperty(name, value, signature);
}

/**
* Serializes the {@code SProperty} to a JSON element.
*
* @param src The {@code SProperty} to serialize.
* @param typeOfSrc The actual type (fully genericized version) of the source object.
* @param jsonSerializationContext The context for serialization.
* @return The serialized JSON element.
*/
@Override
public @NotNull JsonElement serialize(@NotNull SProperty src, Type typeOfSrc, JsonSerializationContext jsonSerializationContext) {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("name", src.name());
jsonObject.addProperty("value", src.value());
jsonObject.addProperty("signature", src.signature());
return jsonObject;
}

/**
* Converts a JSON string to an {@code SProperty} object.
*
* @param json The JSON string representing the serialized {@link SProperty} object.
* @return An {@link SProperty} object deserialized from the provided JSON string.
* @throws IllegalArgumentException If the provided JSON string is null or empty.
*/
public static SProperty fromJson(String json) {
if (json == null) throw new IllegalArgumentException("Json cannot be null");
if (json.isEmpty()) throw new IllegalArgumentException("Json cannot be empty");
return SProperty.fromJson(json);
}

/**
* Converts an {@code SProperty} object to its JSON representation.
*
* @param sProperty The {@code SProperty} object to be converted.
* @return A JSON string representing the serialized {@code SProperty} object.
* @throws IllegalArgumentException If the provided {@code SProperty} object is null.
*/
public static String toJson(SProperty sProperty) {
if (sProperty == null) throw new IllegalArgumentException("SProperty cannot be null");
return sProperty.toJson();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.georgev22.skinoverlay.storage.gsonAdapters;

import com.georgev22.skinoverlay.utilities.SerializableBufferedImage;
import com.google.gson.*;
import org.jetbrains.annotations.NotNull;

import java.awt.image.BufferedImage;
import java.lang.reflect.Type;
import java.util.Base64;

/**
* Gson TypeAdapter for serializing and deserializing {@link SerializableBufferedImage} objects.
* <p>
* This adapter is responsible for converting {@link SerializableBufferedImage} objects to and from JSON format.
* It handles the serialization and deserialization of the image's width, height, and pixel information.
*/
public class SerializableBufferedImageTypeAdapter implements
JsonSerializer<SerializableBufferedImage>,
JsonDeserializer<SerializableBufferedImage> {

/**
* Deserializes the JSON element to a {@link SerializableBufferedImage}.
*
* @param jsonElement The JSON element containing width, height, and pixel information.
* @param type The type to deserialize.
* @param jsonDeserializationContext The context for deserialization.
* @return The deserialized {@link SerializableBufferedImage}.
*/
@Override
public @NotNull SerializableBufferedImage deserialize(@NotNull JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) {
JsonObject jsonObject = jsonElement.getAsJsonObject();

int width = jsonObject.get("width").getAsInt();
int height = jsonObject.get("height").getAsInt();
String encodedPixels = jsonObject.get("pixels").getAsString();
int[] pixels = SerializableBufferedImage.fromByteArray(Base64.getDecoder().decode(encodedPixels));

BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
bufferedImage.setRGB(0, 0, width, height, pixels, 0, width);

return new SerializableBufferedImage(bufferedImage);
}

/**
* Serializes the {@link SerializableBufferedImage} to a JSON element.
*
* @param serializableBufferedImage The {@link SerializableBufferedImage} to serialize.
* @param type The type to serialize.
* @param jsonSerializationContext The context for serialization.
* @return The serialized JSON element.
*/
@Override
public JsonElement serialize(@NotNull SerializableBufferedImage serializableBufferedImage, Type type, @NotNull JsonSerializationContext jsonSerializationContext) {
return jsonSerializationContext.serialize(serializableBufferedImage.serialize());
}

/**
* Converts a {@link SerializableBufferedImage} object to its JSON representation.
*
* @param serializableBufferedImage The {@link SerializableBufferedImage} object to be converted.
* @return A JSON string representing the serialized {@link SerializableBufferedImage} object.
* @throws IllegalArgumentException If the provided {@link SerializableBufferedImage} object is null.
*/
public static String toJson(SerializableBufferedImage serializableBufferedImage) {
if (serializableBufferedImage == null)
throw new IllegalArgumentException("SerializableBufferedImage cannot be null");
return serializableBufferedImage.toJson();
}

/**
* Converts a JSON string to a {@link SerializableBufferedImage} object.
*
* @param json The JSON string representing the serialized {@link SerializableBufferedImage} object.
* @return A {@link SerializableBufferedImage} object deserialized from the provided JSON string.
* @throws IllegalArgumentException If the provided JSON string is null or empty.
*/
public static SerializableBufferedImage fromJson(String json) {
if (json == null) throw new IllegalArgumentException("Json cannot be null");
if (json.isEmpty()) throw new IllegalArgumentException("Json cannot be empty");
return SerializableBufferedImage.fromJson(json);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.georgev22.skinoverlay.storage.gsonAdapters;

import com.georgev22.skinoverlay.handler.skin.SkinParts;
import com.georgev22.skinoverlay.utilities.SerializableBufferedImage;
import com.google.gson.*;
import org.jetbrains.annotations.NotNull;

import java.lang.reflect.Type;

/**
* Gson TypeAdapter for serializing and deserializing {@link SkinParts} objects.
* <p>
* This adapter is responsible for converting {@link SkinParts} objects to and from JSON format.
* It handles the serialization of the buffered image and the skin name properties.
*/
public class SkinPartsTypeAdapter implements JsonSerializer<SkinParts>, JsonDeserializer<SkinParts> {

/**
* Serializes a {@link SkinParts} object to a JSON representation.
*
* @param src The {@link SkinParts} object to be serialized.
* @param typeOfSrc The type of the source object.
* @param context The serialization context.
* @return A {@link JsonElement} representing the serialized {@link SkinParts} object.
*/
@Override
public JsonElement serialize(@NotNull SkinParts src, Type typeOfSrc, @NotNull JsonSerializationContext context) {
JsonObject jsonObject = new JsonObject();
jsonObject.add("bufferedImage", context.serialize(src.getFullSkin()));
jsonObject.addProperty("skinName", src.getSkinName());
return jsonObject;
}

/**
* Deserializes a JSON representation into a {@link SkinParts} object.
*
* @param json The {@link JsonElement} containing the JSON representation.
* @param typeOfT The target type to deserialize into.
* @param context The deserialization context.
* @return A deserialized {@link SkinParts} object.
*/
@Override
public SkinParts deserialize(@NotNull JsonElement json, Type typeOfT, @NotNull JsonDeserializationContext context) {
JsonObject jsonObject = json.getAsJsonObject();

SerializableBufferedImage bufferedImage = context.deserialize(jsonObject.get("bufferedImage"), SerializableBufferedImage.class);
String skinName = jsonObject.get("skinName").getAsString();

return new SkinParts(bufferedImage, skinName);
}

/**
* Converts a {@link SkinParts} object to its JSON representation.
*
* @param skinParts The {@link SkinParts} object to be converted.
* @return A JSON string representing the serialized {@link SkinParts} object.
* @throws IllegalArgumentException If the provided {@link SkinParts} object is null.
*/
public static String toJson(@NotNull SkinParts skinParts) throws IllegalArgumentException {
//noinspection ConstantValue
if (skinParts == null) throw new IllegalArgumentException("SkinParts cannot be null");
return skinParts.toJson();
}

/**
* Converts a JSON string to a {@link SkinParts} object.
*
* @param json The JSON string representing the serialized {@link SkinParts} object.
* @return A {@link SkinParts} object deserialized from the provided JSON string.
* @throws IllegalArgumentException If the provided JSON string is null or empty.
*/
public static SkinParts fromJson(@NotNull String json) {
//noinspection ConstantValue
if (json == null) throw new IllegalArgumentException("Json cannot be null");
if (json.isEmpty()) throw new IllegalArgumentException("Json cannot be empty");
return SkinParts.fromJson(json);
}
}
Loading

0 comments on commit 49cceff

Please sign in to comment.