Skip to content

Commit 6b6dd8b

Browse files
authored
Merge pull request #26 from tatu-at-datastax/main
Fix #25: add explicit support for "plain" (JSON String) serialization in `UUIDDeserializer`
2 parents 60da92b + f4e2849 commit 6b6dd8b

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

astra-db-java/src/main/java/com/datastax/astra/internal/serdes/collections/UUIDDeserializer.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@
2121
*/
2222

2323
import com.fasterxml.jackson.core.JsonParser;
24+
import com.fasterxml.jackson.core.JsonToken;
25+
import com.fasterxml.jackson.core.JsonTokenId;
2426
import com.fasterxml.jackson.databind.DeserializationContext;
2527
import com.fasterxml.jackson.databind.JsonDeserializer;
2628
import com.fasterxml.jackson.databind.JsonNode;
29+
import com.fasterxml.jackson.databind.node.JsonNodeType;
2730

2831
import java.io.IOException;
2932
import java.util.UUID;
@@ -44,11 +47,29 @@ public UUIDDeserializer() {
4447
@Override
4548
public UUID deserialize(JsonParser jp, DeserializationContext ctxt)
4649
throws IOException {
47-
JsonNode node = jp.getCodec().readTree(jp);
48-
if (null == node.get("$uuid")) {
50+
String uuidStr = null;
51+
JsonNode node = ctxt.readTree(jp);
52+
53+
switch (node.getNodeType()) {
54+
case STRING:
55+
uuidStr = node.asText();
56+
break;
57+
58+
case OBJECT:
59+
JsonNode uuidValue = node.get("$uuid");
60+
if (null != uuidValue && uuidValue.isTextual()) {
61+
uuidStr = uuidValue.textValue();
62+
}
63+
default:
64+
break;
65+
}
66+
if (null == uuidStr) {
4967
throw new IllegalArgumentException("Cannot convert the expression as an UUID " + node);
5068
}
51-
return UUID.fromString(node.get("$uuid").asText());
69+
try {
70+
return UUID.fromString(uuidStr);
71+
} catch (IllegalArgumentException e) {
72+
throw new IllegalArgumentException("Invalid UUID String: \"" + uuidStr + "\"", e);
73+
}
5274
}
53-
5475
}

0 commit comments

Comments
 (0)