Skip to content

Add Date and UUID deserialization support in nullSafeValue method #42956

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;
import java.util.UUID;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.ObjectCodec;
Expand Down Expand Up @@ -115,6 +117,12 @@ protected final <D> D nullSafeValue(JsonNode jsonNode, Class<D> type) {
if (type == BigInteger.class) {
return (D) jsonNode.bigIntegerValue();
}
if (type == Date.class) {
return (D) new Date(jsonNode.longValue());
}
if (type == UUID.class) {
return (D) UUID.fromString(jsonNode.textValue());
}
throw new IllegalArgumentException("Unsupported value type " + type.getName());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.io.InputStream;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;
import java.util.UUID;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.ObjectCodec;
Expand Down Expand Up @@ -144,6 +146,27 @@ void nullSafeValueWhenClassIsBigIntegerShouldReturnBigInteger() {
assertThat(value).isEqualTo(BigInteger.TEN);
}


@Test
void nullSafeValueWhenClassIsDateShouldReturnDate() {
JsonNode node = mock(JsonNode.class);
long timestamp = 1629976800000L;
given(node.longValue()).willReturn(timestamp);
Date expectedDate = new Date(timestamp);
Date value = this.testDeserializer.testNullSafeValue(node, Date.class);
assertThat(value).isEqualTo(expectedDate);
}

@Test
void nullSafeValueWhenClassIsUUIDShouldReturnUUID() {
JsonNode node = mock(JsonNode.class);
String uuidString = "123e4567-e89b-12d3-a456-426614174000";
UUID expectedUUID = UUID.fromString(uuidString);
given(node.textValue()).willReturn(uuidString);
UUID value = this.testDeserializer.testNullSafeValue(node, UUID.class);
assertThat(value).isEqualTo(expectedUUID);
}

@Test
void nullSafeValueWhenClassIsUnknownShouldThrowException() {
assertThatIllegalArgumentException()
Expand Down