diff --git a/nbt/build.gradle.kts b/nbt/build.gradle.kts index 5b3f52b..6e4da64 100644 --- a/nbt/build.gradle.kts +++ b/nbt/build.gradle.kts @@ -5,7 +5,7 @@ plugins { } group = "net.thenextlvl.core" -version = "2.2.7" +version = "2.2.8" java { toolchain.languageVersion = JavaLanguageVersion.of(21) diff --git a/nbt/src/main/java/core/nbt/serialization/ParserException.java b/nbt/src/main/java/core/nbt/serialization/ParserException.java new file mode 100644 index 0000000..7144b7d --- /dev/null +++ b/nbt/src/main/java/core/nbt/serialization/ParserException.java @@ -0,0 +1,18 @@ +package core.nbt.serialization; + +public class ParserException extends IllegalArgumentException { + public ParserException() { + } + + public ParserException(String s) { + super(s); + } + + public ParserException(String message, Throwable cause) { + super(message, cause); + } + + public ParserException(Throwable cause) { + super(cause); + } +} diff --git a/nbt/src/main/java/core/nbt/serialization/Serializer.java b/nbt/src/main/java/core/nbt/serialization/Serializer.java index 4be24c9..ca016e5 100644 --- a/nbt/src/main/java/core/nbt/serialization/Serializer.java +++ b/nbt/src/main/java/core/nbt/serialization/Serializer.java @@ -15,31 +15,32 @@ class Serializer implements TagDeserializationContext, TagSerializationContext { private final Map> serializers = new HashMap<>(); @Override - public @Nullable T deserialize(Tag tag, Class type) { + public @Nullable T deserialize(Tag tag, Class type) throws ParserException { return deserialize(tag, (Type) type); } @Override @SuppressWarnings("unchecked") - public @Nullable T deserialize(@NonNull Tag tag, @NonNull Type type) { + public @Nullable T deserialize(@NonNull Tag tag, @NonNull Type type) throws ParserException { if (tag == Tag.EMPTY) return null; var deserializer = deserializers.get(type); - if (deserializer == null) return null; - return (T) deserializer.deserialize(tag, this); + if (deserializer != null) return (T) deserializer.deserialize(tag, this); + throw new ParserException("No tag deserializer registered for type: " + type); } @Override - public Tag serialize(@Nullable Object object) { + public Tag serialize(@Nullable Object object) throws ParserException { if (object == null) return Tag.EMPTY; return serialize(object, object.getClass()); } @Override @SuppressWarnings("unchecked") - public Tag serialize(@Nullable Object object, Type type) { + public Tag serialize(@Nullable Object object, Type type) throws ParserException { if (object == null) return Tag.EMPTY; var serializer = (TagSerializer) serializers.get(type); - return serializer.serialize(object, this); + if (serializer != null) return serializer.serialize(object, this); + throw new ParserException("No tag serializer registered for type: " + type); } public void registerDeserializer(Type type, TagDeserializer deserializer) { diff --git a/nbt/src/main/java/core/nbt/serialization/TagDeserializationContext.java b/nbt/src/main/java/core/nbt/serialization/TagDeserializationContext.java index 6213275..b04dcc6 100644 --- a/nbt/src/main/java/core/nbt/serialization/TagDeserializationContext.java +++ b/nbt/src/main/java/core/nbt/serialization/TagDeserializationContext.java @@ -16,8 +16,9 @@ public interface TagDeserializationContext { * @param type the class of the object to be returned * @param the type of the object to be returned * @return an object of the specified type deserialized from the tag + * @throws ParserException if an error occurs during serialization */ - T deserialize(@NonNull Tag tag, @NonNull Class type); + T deserialize(@NonNull Tag tag, @NonNull Class type) throws ParserException; /** * Deserializes a given tag to an object of the specified type. @@ -26,6 +27,7 @@ public interface TagDeserializationContext { * @param type the type of the object to be returned * @param the type of the object to be returned * @return an object of the specified type deserialized from the tag + * @throws ParserException if an error occurs during deserialization */ - T deserialize(@NonNull Tag tag, @NonNull Type type); + T deserialize(@NonNull Tag tag, @NonNull Type type) throws ParserException; } diff --git a/nbt/src/main/java/core/nbt/serialization/TagDeserializer.java b/nbt/src/main/java/core/nbt/serialization/TagDeserializer.java index 1cac79f..afc1ef9 100644 --- a/nbt/src/main/java/core/nbt/serialization/TagDeserializer.java +++ b/nbt/src/main/java/core/nbt/serialization/TagDeserializer.java @@ -16,7 +16,8 @@ public interface TagDeserializer { * @param tag the tag to be deserialized * @param context the context used for deserialization * @return the deserialized object of type T + * @throws ParserException if an error occurs during deserialization */ @Nullable - T deserialize(@NonNull Tag tag, @NonNull TagDeserializationContext context); + T deserialize(@NonNull Tag tag, @NonNull TagDeserializationContext context) throws ParserException; } diff --git a/nbt/src/main/java/core/nbt/serialization/TagSerializable.java b/nbt/src/main/java/core/nbt/serialization/TagSerializable.java index 690c702..c429f0e 100644 --- a/nbt/src/main/java/core/nbt/serialization/TagSerializable.java +++ b/nbt/src/main/java/core/nbt/serialization/TagSerializable.java @@ -3,6 +3,8 @@ import core.nbt.tag.Tag; import org.jspecify.annotations.NonNull; +import java.text.ParseException; + /** * An interface for objects that can be serialized into and deserialized from a Tag representation. * Implementing classes should provide specific mechanisms for converting their data to and from Tag instances. @@ -12,14 +14,16 @@ public interface TagSerializable { * Serializes the current object into a Tag representation. * * @return the serialized Tag representation of the current object + * @throws ParserException if an error occurs during serialization */ @NonNull - Tag serialize(); + Tag serialize() throws ParseException; /** * Deserializes the given Tag into the appropriate object. * * @param tag the Tag object to be deserialized + * @throws ParserException if an error occurs during deserialization */ - void deserialize(@NonNull Tag tag); + void deserialize(@NonNull Tag tag) throws ParseException; } diff --git a/nbt/src/main/java/core/nbt/serialization/TagSerializationContext.java b/nbt/src/main/java/core/nbt/serialization/TagSerializationContext.java index e104e52..5e9d028 100644 --- a/nbt/src/main/java/core/nbt/serialization/TagSerializationContext.java +++ b/nbt/src/main/java/core/nbt/serialization/TagSerializationContext.java @@ -5,6 +5,7 @@ import org.jspecify.annotations.Nullable; import java.lang.reflect.Type; +import java.text.ParseException; /** * TagSerializationContext defines the contract for serializing objects into tags. @@ -15,9 +16,10 @@ public interface TagSerializationContext { * * @param object the object to be serialized * @return the serialized Tag representation of the object + * @throws ParserException if an error occurs during serialization */ @NonNull - Tag serialize(@Nullable Object object); + Tag serialize(@Nullable Object object) throws ParseException; /** * Serializes the given object into a Tag representation based on the specified type. @@ -25,7 +27,8 @@ public interface TagSerializationContext { * @param object the object to be serialized * @param type the type of the object to be serialized * @return the serialized Tag representation of the object + * @throws ParserException if an error occurs during serialization */ @NonNull - Tag serialize(@Nullable Object object, @NonNull Type type); + Tag serialize(@Nullable Object object, @NonNull Type type) throws ParseException; } diff --git a/nbt/src/main/java/core/nbt/serialization/TagSerializer.java b/nbt/src/main/java/core/nbt/serialization/TagSerializer.java index 27ac037..df74179 100644 --- a/nbt/src/main/java/core/nbt/serialization/TagSerializer.java +++ b/nbt/src/main/java/core/nbt/serialization/TagSerializer.java @@ -15,7 +15,8 @@ public interface TagSerializer { * @param object the object to be serialized * @param context the context used for serialization * @return the Tag representation of the provided vector + * @throws ParserException if an error occurs during serialization */ @NonNull - Tag serialize(@NonNull T object, @NonNull TagSerializationContext context); + Tag serialize(@NonNull T object, @NonNull TagSerializationContext context) throws ParserException; }