Skip to content

Commit

Permalink
Refactor(serialization): Update serializers to throw RuntimeException…
Browse files Browse the repository at this point in the history
… on errors
  • Loading branch information
Uni0305 committed Jan 4, 2025
1 parent 6d87dbc commit 825d4bb
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
* A serializer for arrays of objects using Bukkit's object streams.
Expand All @@ -25,14 +24,14 @@ public class BukkitObjectArraySerializer<T> implements ByteArraySerializer<T[]>
* @throws RuntimeException if an I/O error occurs during serialization
*/
@Override
public byte @NotNull [] serialize(T @NotNull [] array) {
public byte @NotNull [] serialize(T @NotNull [] array) throws RuntimeException {
try (ByteArrayOutputStream output = new ByteArrayOutputStream(); BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(output)) {
dataOutput.writeInt(array.length);
for (T element : array) {
dataOutput.writeObject(element);
}
return output.toByteArray();
} catch (IOException e) {
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Expand All @@ -46,15 +45,15 @@ public class BukkitObjectArraySerializer<T> implements ByteArraySerializer<T[]>
*/
@SuppressWarnings("unchecked")
@Override
public T @Nullable [] deserialize(byte @NotNull [] bytes) {
public T @Nullable [] deserialize(byte @NotNull [] bytes) throws RuntimeException {
try (ByteArrayInputStream input = new ByteArrayInputStream(bytes); BukkitObjectInputStream dataInput = new BukkitObjectInputStream(input)) {
int length = dataInput.readInt();
T[] array = (T[]) new Object[length];
for (int i = 0; i < length; i++) {
array[i] = (T) dataInput.readObject();
}
return array;
} catch (IOException | ClassNotFoundException e) {
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
* A serializer for objects using Bukkit's object streams.
Expand All @@ -25,11 +24,11 @@ public class BukkitObjectSerializer<T> implements ByteArraySerializer<T> {
* @throws RuntimeException if an I/O error occurs during serialization
*/
@Override
public byte @NotNull [] serialize(@NotNull T obj) {
public byte @NotNull [] serialize(@NotNull T obj) throws RuntimeException {
try (ByteArrayOutputStream output = new ByteArrayOutputStream(); BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(output)) {
dataOutput.writeObject(obj);
return output.toByteArray();
} catch (IOException e) {
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Expand All @@ -43,10 +42,10 @@ public class BukkitObjectSerializer<T> implements ByteArraySerializer<T> {
*/
@SuppressWarnings("unchecked")
@Override
public @Nullable T deserialize(byte @NotNull [] src) {
public @Nullable T deserialize(byte @NotNull [] src) throws RuntimeException {
try (ByteArrayInputStream input = new ByteArrayInputStream(src); BukkitObjectInputStream dataInput = new BukkitObjectInputStream(input)) {
return (T) dataInput.readObject();
} catch (IOException | ClassNotFoundException e) {
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,38 @@
import me.uni0305.mokoko.library.common.JsonSerializer;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class JsonItemStackArraySerializer implements JsonSerializer<ItemStack[]> {
private final JsonItemStackSerializer serializer = new JsonItemStackSerializer();

@Override
public @NotNull JsonElement serialize(ItemStack @NotNull [] items) {
JsonArray array = new JsonArray();
for (ItemStack item : items) {
JsonElement element = serializer.serialize(item);
array.add(element);
public @NotNull JsonElement serialize(ItemStack @NotNull [] items) throws RuntimeException {
try {
JsonArray array = new JsonArray();
for (ItemStack item : items) {
JsonElement element = serializer.serialize(item);
array.add(element);
}
return array;
} catch (Exception e) {
throw new RuntimeException(e);
}
return array;
}

@Override
public ItemStack @NotNull [] deserialize(@NotNull JsonElement json) {
JsonArray array = json.getAsJsonArray();
int length = array.size();
ItemStack[] items = new ItemStack[length];
for (int i = 0; i < length; i++) {
JsonElement element = array.get(i);
items[i] = serializer.deserialize(element);
public @Nullable ItemStack @NotNull [] deserialize(@NotNull JsonElement json) throws RuntimeException {
try {
JsonArray array = json.getAsJsonArray();
int length = array.size();
ItemStack[] items = new ItemStack[length];
for (int i = 0; i < length; i++) {
JsonElement element = array.get(i);
items[i] = serializer.deserialize(element);
}
return items;
} catch (Exception e) {
throw new RuntimeException(e);
}
return items;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public class JsonItemStackSerializer implements JsonSerializer<ItemStack> {
private final JsonSerializer<ItemStack> serializer;

public JsonItemStackSerializer() {
public JsonItemStackSerializer() throws UnsupportedOperationException {
String minecraftVersion = Bukkit.getMinecraftVersion();
switch (minecraftVersion) {
case "1.19.4" -> serializer = new me.uni0305.mokoko.library.nms.v1_19_r3.JsonItemStackSerializer();
Expand All @@ -26,12 +26,20 @@ public JsonItemStackSerializer() {
}

@Override
public @NotNull JsonElement serialize(@NotNull ItemStack obj) {
return serializer.serialize(obj);
public @NotNull JsonElement serialize(@NotNull ItemStack obj) throws RuntimeException {
try {
return serializer.serialize(obj);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@Override
public @Nullable ItemStack deserialize(@NotNull JsonElement src) {
return serializer.deserialize(src);
public @Nullable ItemStack deserialize(@NotNull JsonElement src) throws RuntimeException {
try {
return serializer.deserialize(src);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,24 @@ public class JsonLocationSerializer implements JsonSerializer<Location> {
private final Gson gson = new Gson();

@Override
public @NotNull JsonElement serialize(@NotNull Location obj) {
Map<String, Object> map = obj.serialize();
return gson.toJsonTree(map);
public @NotNull JsonElement serialize(@NotNull Location obj) throws RuntimeException {
try {
Map<String, Object> map = obj.serialize();
return gson.toJsonTree(map);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@Override
public @NotNull Location deserialize(@NotNull JsonElement src) throws IllegalArgumentException {
TypeToken<Map<String, Object>> type = new TypeToken<>() {};
Map<String, Object> map = gson.fromJson(src, type);
return Location.deserialize(map);
public @NotNull Location deserialize(@NotNull JsonElement src) throws RuntimeException {
try {
TypeToken<Map<String, Object>> type = new TypeToken<>() {
};
Map<String, Object> map = gson.fromJson(src, type);
return Location.deserialize(map);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

0 comments on commit 825d4bb

Please sign in to comment.