BAEL-87 - @JsonComponent in Spring Boot (#1519)

This commit is contained in:
Wim Deblauwe 2017-04-04 00:35:35 +02:00 committed by Zeger Hendrikse
parent 2a76b9c656
commit 8da820b35a
6 changed files with 166 additions and 0 deletions

View File

@ -0,0 +1,15 @@
package org.baeldung.jsoncomponent;
import javafx.scene.paint.Color;
public class User {
private final Color favoriteColor;
public User(Color favoriteColor) {
this.favoriteColor = favoriteColor;
}
public Color getFavoriteColor() {
return favoriteColor;
}
}

View File

@ -0,0 +1,46 @@
package org.baeldung.jsoncomponent;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.TreeNode;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.node.TextNode;
import javafx.scene.paint.Color;
import org.springframework.boot.jackson.JsonComponent;
import java.io.IOException;
@JsonComponent
public class UserCombinedSerializer {
public static class UserJsonSerializer extends JsonSerializer<User> {
@Override
public void serialize(User user, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
jsonGenerator.writeStartObject();
jsonGenerator.writeStringField("favoriteColor",
getColorAsWebColor(user.getFavoriteColor()));
jsonGenerator.writeEndObject();
}
private static String getColorAsWebColor(Color color) {
int r = (int) Math.round(color.getRed() * 255.0);
int g = (int) Math.round(color.getGreen() * 255.0);
int b = (int) Math.round(color.getBlue() * 255.0);
return String.format("#%02x%02x%02x", r, g, b);
}
}
@JsonComponent
public static class UserJsonDeserializer extends JsonDeserializer<User> {
@Override
public User deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
TreeNode treeNode = jsonParser.getCodec().readTree(jsonParser);
TextNode favoriteColor = (TextNode) treeNode.get("favoriteColor");
return new User(Color.web(favoriteColor.asText()));
}
}
}

View File

@ -0,0 +1,22 @@
package org.baeldung.jsoncomponent;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.TreeNode;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.node.TextNode;
import javafx.scene.paint.Color;
import org.springframework.boot.jackson.JsonComponent;
import java.io.IOException;
@JsonComponent
public class UserJsonDeserializer extends JsonDeserializer<User> {
@Override
public User deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
TreeNode treeNode = jsonParser.getCodec().readTree(jsonParser);
TextNode favoriteColor = (TextNode) treeNode.get("favoriteColor");
return new User(Color.web(favoriteColor.asText()));
}
}

View File

@ -0,0 +1,29 @@
package org.baeldung.jsoncomponent;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import javafx.scene.paint.Color;
import org.springframework.boot.jackson.JsonComponent;
import java.io.IOException;
@JsonComponent
public class UserJsonSerializer extends JsonSerializer<User> {
@Override
public void serialize(User user, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
jsonGenerator.writeStartObject();
jsonGenerator.writeStringField("favoriteColor",
getColorAsWebColor(user.getFavoriteColor()));
jsonGenerator.writeEndObject();
}
private static String getColorAsWebColor(Color color) {
int r = (int) Math.round(color.getRed() * 255.0);
int g = (int) Math.round(color.getGreen() * 255.0);
int b = (int) Math.round(color.getBlue() * 255.0);
return String.format("#%02x%02x%02x", r, g, b);
}
}

View File

@ -0,0 +1,27 @@
package org.baeldung.jsoncomponent;
import com.fasterxml.jackson.databind.ObjectMapper;
import javafx.scene.paint.Color;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.json.JsonTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;
import static org.junit.Assert.assertEquals;
@JsonTest
@RunWith(SpringRunner.class)
public class UserJsonDeserializerTest {
@Autowired
private ObjectMapper objectMapper;
@Test
public void testDeserialize() throws IOException {
User user = objectMapper.readValue("{\"favoriteColor\":\"#f0f8ff\"}", User.class);
assertEquals(Color.ALICEBLUE, user.getFavoriteColor());
}
}

View File

@ -0,0 +1,27 @@
package org.baeldung.jsoncomponent;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import javafx.scene.paint.Color;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.json.JsonTest;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.assertEquals;
@JsonTest
@RunWith(SpringRunner.class)
public class UserJsonSerializerTest {
@Autowired
private ObjectMapper objectMapper;
@Test
public void testSerialization() throws JsonProcessingException {
String json = objectMapper.writeValueAsString(new User(Color.ALICEBLUE));
assertEquals("{\"favoriteColor\":\"#f0f8ff\"}", json);
}
}