Add nullSafeValue method that accepts a mapper Function

Update `JsonObjectDeserializer` with a `nullSafeValue` method that
accepts a mapper `Function`

Closes gh-42972
This commit is contained in:
Phillip Webb
2024-11-01 14:07:54 -07:00
parent 2fa1180332
commit 23607ee4c4
4 changed files with 43 additions and 32 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,9 @@
package org.springframework.boot.test.autoconfigure.json;
import java.util.Date;
import java.util.UUID;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
@@ -75,7 +78,7 @@ class JsonTestIntegrationTests {
@Test
void jacksonCustom() throws Exception {
ExampleCustomObject object = new ExampleCustomObject("spring");
ExampleCustomObject object = new ExampleCustomObject("spring", new Date(), UUID.randomUUID());
assertThat(this.jacksonCustomJson.write(object)).isEqualToJson("example.json");
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,35 +16,17 @@
package org.springframework.boot.test.autoconfigure.json.app;
import java.util.Date;
import java.util.UUID;
/**
* Example object to read/write as JSON through {@link ExampleJsonComponent}.
*
* @author Phillip Webb
* @param value the value
* @param date a date
* @param uuid a uuid
*/
public class ExampleCustomObject {
private final String value;
public ExampleCustomObject(String value) {
this.value = value;
}
@Override
public boolean equals(Object obj) {
if (obj != null && obj.getClass() == getClass()) {
return this.value.equals(((ExampleCustomObject) obj).value);
}
return false;
}
@Override
public int hashCode() {
return this.value.hashCode();
}
@Override
public String toString() {
return this.value;
}
public record ExampleCustomObject(String value, Date date, UUID uuid) {
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,6 +17,8 @@
package org.springframework.boot.test.autoconfigure.json.app;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
@@ -44,7 +46,9 @@ public class ExampleJsonComponent {
@Override
protected void serializeObject(ExampleCustomObject value, JsonGenerator jgen, SerializerProvider provider)
throws IOException {
jgen.writeStringField("value", value.toString());
jgen.writeStringField("value", value.value());
jgen.writeNumberField("date", value.date().getTime());
jgen.writeStringField("uuid", value.uuid().toString());
}
}
@@ -54,7 +58,10 @@ public class ExampleJsonComponent {
@Override
protected ExampleCustomObject deserializeObject(JsonParser jsonParser, DeserializationContext context,
ObjectCodec codec, JsonNode tree) throws IOException {
return new ExampleCustomObject(nullSafeValue(tree.get("value"), String.class));
String value = nullSafeValue(tree.get("value"), String.class);
Date date = nullSafeValue(tree.get("date"), Integer.class, Date::new);
UUID uuid = nullSafeValue(tree.get("uuid"), String.class, UUID::fromString);
return new ExampleCustomObject(value, date, uuid);
}
}