diff --git a/src/main/java/io/lettuce/core/json/DefaultJsonParser.java b/src/main/java/io/lettuce/core/json/DefaultJsonParser.java index afab7afa70..dd20fa41ca 100644 --- a/src/main/java/io/lettuce/core/json/DefaultJsonParser.java +++ b/src/main/java/io/lettuce/core/json/DefaultJsonParser.java @@ -10,6 +10,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.NullNode; import java.io.IOException; import java.nio.ByteBuffer; @@ -61,6 +62,10 @@ public JsonValue fromObject(Object object) { } private JsonValue parse(String value) { + if (value == null) { + return DelegateJsonValue.wrap(NullNode.getInstance()); + } + ObjectMapper mapper = new ObjectMapper(); try { JsonNode root = mapper.readTree(value); @@ -72,6 +77,10 @@ private JsonValue parse(String value) { } private JsonValue parse(ByteBuffer byteBuffer) { + if (byteBuffer == null) { + return DelegateJsonValue.wrap(NullNode.getInstance()); + } + ObjectMapper mapper = new ObjectMapper(); try { byte[] bytes = new byte[byteBuffer.remaining()]; diff --git a/src/main/java/io/lettuce/core/json/UnproccessedJsonValue.java b/src/main/java/io/lettuce/core/json/UnproccessedJsonValue.java index 0a00167503..92e98418a6 100644 --- a/src/main/java/io/lettuce/core/json/UnproccessedJsonValue.java +++ b/src/main/java/io/lettuce/core/json/UnproccessedJsonValue.java @@ -45,6 +45,11 @@ public UnproccessedJsonValue(ByteBuffer bytes, JsonParser theParser) { @Override public String toString() { + + if (unprocessedData == null) { + return null; + } + if (isDeserialized()) { return jsonValue.toString(); } @@ -161,7 +166,9 @@ private void lazilyDeserialize() { try { if (!isDeserialized()) { jsonValue = parser.createJsonValue(unprocessedData); - unprocessedData.clear(); + if (unprocessedData != null) { + unprocessedData.clear(); + } } } finally { lock.unlock(); diff --git a/src/main/java/io/lettuce/core/output/JsonValueListOutput.java b/src/main/java/io/lettuce/core/output/JsonValueListOutput.java index 389b696624..e415f46a72 100644 --- a/src/main/java/io/lettuce/core/output/JsonValueListOutput.java +++ b/src/main/java/io/lettuce/core/output/JsonValueListOutput.java @@ -39,9 +39,13 @@ public void set(ByteBuffer bytes) { multi(1); } - ByteBuffer fetched = ByteBuffer.allocate(bytes.remaining()); - fetched.put(bytes); - fetched.flip(); + ByteBuffer fetched = null; + if (bytes != null) { + fetched = ByteBuffer.allocate(bytes.remaining()); + fetched.put(bytes); + fetched.flip(); + } + output.add(parser.loadJsonValue(fetched)); } diff --git a/src/test/java/io/lettuce/core/json/RedisJsonIntegrationTests.java b/src/test/java/io/lettuce/core/json/RedisJsonIntegrationTests.java index e471050c47..116b127712 100644 --- a/src/test/java/io/lettuce/core/json/RedisJsonIntegrationTests.java +++ b/src/test/java/io/lettuce/core/json/RedisJsonIntegrationTests.java @@ -157,6 +157,16 @@ void jsonArrpop(String path) { "{\"id\":\"bike:3\",\"model\":\"Weywot\",\"description\":\"This bike gives kids aged six years and old"); } + @Test + public void jsonArrpopEmptyArray() { + JsonValue value = redis.getJsonParser().createJsonValue("[\"one\"]"); + redis.jsonSet("myKey", JsonPath.ROOT_PATH, value); + List result = redis.jsonArrpop("myKey"); + assertThat(result.toString()).isEqualTo("[\"one\"]"); + result = redis.jsonArrpop("myKey", JsonPath.ROOT_PATH, 0); + assertThat(result.get(0).isNull()).isTrue(); + } + @ParameterizedTest(name = "With {0} as path") @ValueSource(strings = { BIKE_COLORS_V1, BIKE_COLORS_V2 }) void jsonArrtrim(String path) {