BAEL-6312 Add example code for @JsonMerge Jackson annotation

This commit is contained in:
Sam Gardner 2023-06-05 11:13:48 +01:00
parent c341505b45
commit 3779c5c50c
5 changed files with 220 additions and 0 deletions

View File

@ -0,0 +1,37 @@
package com.baeldung.jackson.jsonmerge;
public class Keyboard {
private String style;
private String layout;
public Keyboard(){
super();
}
public Keyboard(String style, String layout) {
this.style = style;
this.layout = layout;
}
public String getStyle() {
return style;
}
public void setStyle(String style) {
this.style = style;
}
public String getLayout() {
return layout;
}
public void setLayout(String layout) {
this.layout = layout;
}
@Override
public String toString() {
return "Keyboard{" + "style='" + style + '\'' + ", layout='" + layout + '\'' + '}';
}
}

View File

@ -0,0 +1,38 @@
package com.baeldung.jackson.jsonmerge;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonMerge;
public class ObjectWithMap {
private String name;
@JsonMerge
private Map<String, String> stringPairs;
public ObjectWithMap(String name, Map<String, String> stringPairs) {
this.name = name;
this.stringPairs = stringPairs;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Map<String, String> getStringPairs() {
return stringPairs;
}
public void setStringPairs(Map<String, String> stringPairs) {
this.stringPairs = stringPairs;
}
@Override
public String toString() {
return "ObjectWithMap{" + "name='" + name + '\'' + ", something=" + stringPairs + '}';
}
}

View File

@ -0,0 +1,38 @@
package com.baeldung.jackson.jsonmerge;
import com.fasterxml.jackson.annotation.JsonMerge;
public class ProgrammerAnnotated {
private String name;
private String favouriteLanguage;
@JsonMerge
private Keyboard keyboard;
public ProgrammerAnnotated(String name, String favouriteLanguage, Keyboard keyboard) {
this.name = name;
this.favouriteLanguage = favouriteLanguage;
this.keyboard = keyboard;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFavouriteLanguage() {
return favouriteLanguage;
}
public Keyboard getKeyboard() {
return keyboard;
}
@Override
public String toString() {
return "Programmer{" + "name='" + name + '\'' + ", favouriteLanguage='" + favouriteLanguage + '\'' + ", keyboard=" + keyboard + '}';
}
}

View File

@ -0,0 +1,43 @@
package com.baeldung.jackson.jsonmerge;
public class ProgrammerNotAnnotated {
private String name;
private String favouriteLanguage;
private Keyboard keyboard;
public ProgrammerNotAnnotated(String name, String favouriteLanguage, Keyboard keyboard) {
this.name = name;
this.favouriteLanguage = favouriteLanguage;
this.keyboard = keyboard;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFavouriteLanguage() {
return favouriteLanguage;
}
public void setFavouriteLanguage(String favouriteLanguage) {
this.favouriteLanguage = favouriteLanguage;
}
public Keyboard getKeyboard() {
return keyboard;
}
public void setKeyboard(Keyboard keyboard) {
this.keyboard = keyboard;
}
@Override
public String toString() {
return "Programmer{" + "name='" + name + '\'' + ", favouriteLanguage='" + favouriteLanguage + '\'' + ", keyboard=" + keyboard + '}';
}
}

View File

@ -0,0 +1,64 @@
package com.baeldung.jackson.jsonmerge;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
public class JsonMergeUnitTest {
@Test
public void givenAnObjectAndJson_whenUsingJsonMerge_thenExpectUpdateInPOJO() throws JsonProcessingException {
String newData = "{\"name\":\"Steve\",\"keyboard\":{\"style\":\"Mechanical\"}}";
ProgrammerAnnotated programmerToUpdate = new ProgrammerAnnotated("John", "Java", new Keyboard("Membrane", "US"));
ObjectMapper objectMapper = new ObjectMapper();
ProgrammerAnnotated update = objectMapper.readerForUpdating(programmerToUpdate)
.readValue(newData);
assert (update.getFavouriteLanguage()).equals("Java");
// Only works with annotation
assert (update.getKeyboard()
.getLayout()).equals("US");
}
@Test
public void givenAnObjectAndJson_whenNotUsingJsonMerge_thenExpectNoUpdateInPOJO() throws JsonProcessingException {
String newData = "{\"name\":\"Steve\",\"keyboard\":{\"style\":\"Mechanical\"}}";
ProgrammerNotAnnotated programmerToUpdate = new ProgrammerNotAnnotated("John", "Java", new Keyboard("Membrane", "US"));
ObjectMapper objectMapper = new ObjectMapper();
ObjectReader objectReader = objectMapper.readerForUpdating(programmerToUpdate);
ProgrammerNotAnnotated update = objectReader.readValue(newData);
assert (update.getFavouriteLanguage()).equals("Java");
assertNull(update.getKeyboard()
.getLayout());
}
@Test
public void givenAnObjectWithAMap_whenUsingJsonMerge_thenExpectAllFieldsInMap() throws JsonProcessingException {
String newData = "{\"stringPairs\":{\"field1\":\"value1\",\"field2\":\"value2\"}}";
Map<String, String> map = new HashMap<>();
map.put("field3", "value3");
ObjectWithMap objectToUpdateWith = new ObjectWithMap("James", map);
ObjectMapper objectMapper = new ObjectMapper();
ObjectWithMap update = objectMapper.readerForUpdating(objectToUpdateWith)
.readValue(newData);
assertTrue(update.getStringPairs()
.containsKey("field1"));
assertTrue(update.getStringPairs()
.containsKey("field2"));
assertTrue(update.getStringPairs()
.containsKey("field3"));
}
}