Skip to content

Commit

Permalink
improve text component serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
burdoto committed Sep 8, 2024
1 parent 6911cd8 commit 3e9cd5d
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/main/java/org/comroid/api/ByteConverter.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package org.comroid.api;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import org.comroid.api.func.util.Debug;
import org.comroid.api.info.Log;

import java.io.IOException;

public interface ByteConverter<T> {
byte[] toBytes(T it);
Expand All @@ -12,15 +16,23 @@ static <T> ByteConverter<T> jackson(Class<T> type) {
private final ObjectMapper mapper = new ObjectMapper();

@Override
@SneakyThrows
public byte[] toBytes(T it) {
return mapper.writeValueAsBytes(it);
try {
return mapper.writeValueAsBytes(it);
} catch (JsonProcessingException e) {
Debug.log(Log.get(), "Could not serialize " + type.getCanonicalName(), e);
return new byte[0];
}
}

@Override
@SneakyThrows
public T fromBytes(byte[] bytes) {
return mapper.readValue(bytes, type);
try {
return mapper.readValue(bytes, type);
} catch (IOException e) {
Debug.log(Log.get(), "Could not deserialize " + type.getCanonicalName(), e);
return null;
}
}
};
}
Expand Down

0 comments on commit 3e9cd5d

Please sign in to comment.