Skip to content

Commit

Permalink
Update serialization methods to handle null objects
Browse files Browse the repository at this point in the history
Modified the `serialize` method in multiple classes to accept `@Nullable` objects. Added a check for null objects returning `Tag.EMPTY` when null to prevent potential null pointer exceptions. Updated the version in `build.gradle.kts` to 2.2.7 to reflect the changes.
  • Loading branch information
NonSwag committed Oct 31, 2024
1 parent db4eb10 commit e46f6b6
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 4 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.6"
version = "2.2.7"

java {
toolchain.languageVersion = JavaLanguageVersion.of(21)
Expand Down
2 changes: 1 addition & 1 deletion nbt/src/main/java/core/nbt/serialization/NBT.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public <T> T fromTag(@NonNull Tag tag, @NonNull Type type) {
* @param object the object to be serialized
* @return the Tag representation of the provided object
*/
public @NonNull Tag toTag(@NonNull Object object) {
public @NonNull Tag toTag(@Nullable Object object) {
return serializer.serialize(object);
}

Expand Down
3 changes: 2 additions & 1 deletion nbt/src/main/java/core/nbt/serialization/Serializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class Serializer implements TagDeserializationContext, TagSerializationContext {
}

@Override
public Tag serialize(Object object) {
public Tag serialize(@Nullable Object object) {
if (object == null) return Tag.EMPTY;
return serialize(object, object.getClass());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public interface TagSerializationContext {
* @return the serialized Tag representation of the object
*/
@NonNull
Tag serialize(@NonNull Object object);
Tag serialize(@Nullable Object object);

/**
* Serializes the given object into a Tag representation based on the specified type.
Expand Down

0 comments on commit e46f6b6

Please sign in to comment.