Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a specific codec for byte arrays #28036

Merged
merged 1 commit into from Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -22,6 +22,9 @@ public static <T> Codec<T> getDefaultCodecFor(Class<T> clazz) {
if (clazz.equals(String.class)) {
return (Codec<T>) StringCodec.INSTANCE;
}
if (clazz.equals(byte[].class)) {
return (Codec<T>) ByteArrayCodec.INSTANCE;
}
// JSON by default
return new JsonCodec<>(clazz);
}
Expand Down Expand Up @@ -114,4 +117,23 @@ public Integer decode(byte[] item) {
}
}

public static class ByteArrayCodec implements Codec<byte[]> {

public static ByteArrayCodec INSTANCE = new ByteArrayCodec();

private ByteArrayCodec() {
// Avoid direct instantiation;
}

@Override
public byte[] encode(byte[] item) {
return item;
}

@Override
public byte[] decode(byte[] item) {
return item;
}
}

}
Expand Up @@ -20,6 +20,8 @@
import io.quarkus.redis.datasource.value.SetArgs;
import io.quarkus.redis.datasource.value.ValueCommands;
import io.quarkus.redis.runtime.datasource.BlockingRedisDataSourceImpl;
import io.vertx.core.json.DecodeException;
import io.vertx.core.json.Json;

public class ValueCommandsTest extends DatasourceTestBase {

Expand Down Expand Up @@ -274,5 +276,10 @@ void binary() {
commands.set(key, content);
byte[] bytes = commands.get(key);
assertThat(bytes).isEqualTo(content);

// Verify that we do not get through the JSON codec (which would base64 encode the byte[])
ValueCommands<String, String> cmd = ds.value(String.class);
String str = cmd.get(key);
assertThatThrownBy(() -> Json.decodeValue(str, byte[].class)).isInstanceOf(DecodeException.class);
}
}