Skip to content

Commit

Permalink
Add error handling with ParserException in serialization
Browse files Browse the repository at this point in the history
Refactored the serialization and deserialization methods to throw a new ParserException when appropriate serializers or deserializers are not found. This change ensures that error conditions are properly reported, improving robustness and debuggability of the serialization process.
  • Loading branch information
NonSwag committed Oct 31, 2024
1 parent e46f6b6 commit 9d0887d
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 16 deletions.
2 changes: 1 addition & 1 deletion nbt/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group = "net.thenextlvl.core"
version = "2.2.7"
version = "2.2.8"

java {
toolchain.languageVersion = JavaLanguageVersion.of(21)
Expand Down
18 changes: 18 additions & 0 deletions nbt/src/main/java/core/nbt/serialization/ParserException.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
15 changes: 8 additions & 7 deletions nbt/src/main/java/core/nbt/serialization/Serializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,32 @@ class Serializer implements TagDeserializationContext, TagSerializationContext {
private final Map<Type, TagSerializer<?>> serializers = new HashMap<>();

@Override
public @Nullable <T> T deserialize(Tag tag, Class<T> type) {
public @Nullable <T> T deserialize(Tag tag, Class<T> type) throws ParserException {
return deserialize(tag, (Type) type);
}

@Override
@SuppressWarnings("unchecked")
public @Nullable <T> T deserialize(@NonNull Tag tag, @NonNull Type type) {
public @Nullable <T> 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<Object>) 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ public interface TagDeserializationContext {
* @param type the class of the object to be returned
* @param <T> 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> T deserialize(@NonNull Tag tag, @NonNull Class<T> type);
<T> T deserialize(@NonNull Tag tag, @NonNull Class<T> type) throws ParserException;

/**
* Deserializes a given tag to an object of the specified type.
Expand All @@ -26,6 +27,7 @@ public interface TagDeserializationContext {
* @param type the type of the object to be returned
* @param <T> 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> T deserialize(@NonNull Tag tag, @NonNull Type type);
<T> T deserialize(@NonNull Tag tag, @NonNull Type type) throws ParserException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public interface TagDeserializer<T> {
* @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;
}
8 changes: 6 additions & 2 deletions nbt/src/main/java/core/nbt/serialization/TagSerializable.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -15,17 +16,19 @@ 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.
*
* @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;
}
3 changes: 2 additions & 1 deletion nbt/src/main/java/core/nbt/serialization/TagSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public interface TagSerializer<T> {
* @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;
}

0 comments on commit 9d0887d

Please sign in to comment.