|
| 1 | +package com.fasterxml.jackson.databind.deser.builder; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.JsonCreator; |
| 4 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 5 | +import com.fasterxml.jackson.annotation.JsonUnwrapped; |
| 6 | +import com.fasterxml.jackson.databind.BaseMapTest; |
| 7 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 8 | +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; |
| 9 | +import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder; |
| 10 | + |
| 11 | +public class BuilderWithUnwrappedTest extends BaseMapTest { |
| 12 | + /* |
| 13 | + ************************************* |
| 14 | + * Mock classes |
| 15 | + ************************************* |
| 16 | + */ |
| 17 | + |
| 18 | + final static class Name { |
| 19 | + private final String first; |
| 20 | + private final String last; |
| 21 | + |
| 22 | + @JsonCreator |
| 23 | + Name( |
| 24 | + @JsonProperty("first_name") String first, |
| 25 | + @JsonProperty("last_name") String last |
| 26 | + ) { |
| 27 | + this.first = first; |
| 28 | + this.last = last; |
| 29 | + } |
| 30 | + |
| 31 | + String getFirst() { |
| 32 | + return first; |
| 33 | + } |
| 34 | + |
| 35 | + String getLast() { |
| 36 | + return last; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + @JsonDeserialize(builder = Person.Builder.class) |
| 41 | + final static class Person { |
| 42 | + private final long id; |
| 43 | + private final Name name; |
| 44 | + private final int age; |
| 45 | + private final boolean alive; |
| 46 | + |
| 47 | + private Person(Builder builder) { |
| 48 | + id = builder.id; |
| 49 | + name = builder.name; |
| 50 | + age = builder.age; |
| 51 | + alive = builder.alive; |
| 52 | + } |
| 53 | + |
| 54 | + long getId() { |
| 55 | + return id; |
| 56 | + } |
| 57 | + |
| 58 | + Name getName() { |
| 59 | + return name; |
| 60 | + } |
| 61 | + |
| 62 | + int getAge() { |
| 63 | + return age; |
| 64 | + } |
| 65 | + |
| 66 | + boolean isAlive() { |
| 67 | + return alive; |
| 68 | + } |
| 69 | + |
| 70 | + @JsonPOJOBuilder(withPrefix = "set") |
| 71 | + final static class Builder { |
| 72 | + private final long id; |
| 73 | + private Name name; |
| 74 | + private int age; |
| 75 | + private boolean alive; |
| 76 | + |
| 77 | + Builder(@JsonProperty("person_id") long id) { |
| 78 | + this.id = id; |
| 79 | + } |
| 80 | + |
| 81 | + @JsonUnwrapped |
| 82 | + void setName(Name name) { |
| 83 | + this.name = name; |
| 84 | + } |
| 85 | + |
| 86 | + @JsonProperty("years_old") |
| 87 | + void setAge(int age) { |
| 88 | + this.age = age; |
| 89 | + } |
| 90 | + |
| 91 | + @JsonProperty("living") |
| 92 | + void setAlive(boolean alive) { |
| 93 | + this.alive = alive; |
| 94 | + } |
| 95 | + |
| 96 | + Person build() { |
| 97 | + return new Person(this); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + @JsonDeserialize(builder = Animal.Builder.class) |
| 103 | + final static class Animal { |
| 104 | + private final long id; |
| 105 | + private final Name name; |
| 106 | + private final int age; |
| 107 | + private final boolean alive; |
| 108 | + |
| 109 | + private Animal(Builder builder) { |
| 110 | + id = builder.id; |
| 111 | + name = builder.name; |
| 112 | + age = builder.age; |
| 113 | + alive = builder.alive; |
| 114 | + } |
| 115 | + |
| 116 | + long getId() { |
| 117 | + return id; |
| 118 | + } |
| 119 | + |
| 120 | + Name getName() { |
| 121 | + return name; |
| 122 | + } |
| 123 | + |
| 124 | + int getAge() { |
| 125 | + return age; |
| 126 | + } |
| 127 | + |
| 128 | + boolean isAlive() { |
| 129 | + return alive; |
| 130 | + } |
| 131 | + |
| 132 | + @JsonPOJOBuilder(withPrefix = "set") |
| 133 | + final static class Builder { |
| 134 | + private final long id; |
| 135 | + private Name name; |
| 136 | + private int age; |
| 137 | + private final boolean alive; |
| 138 | + |
| 139 | + Builder( |
| 140 | + @JsonProperty("animal_id") long id, |
| 141 | + @JsonProperty("living") boolean alive |
| 142 | + ) { |
| 143 | + this.id = id; |
| 144 | + this.alive = alive; |
| 145 | + } |
| 146 | + |
| 147 | + @JsonUnwrapped |
| 148 | + void setName(Name name) { |
| 149 | + this.name = name; |
| 150 | + } |
| 151 | + |
| 152 | + @JsonProperty("years_old") |
| 153 | + void setAge(int age) { |
| 154 | + this.age = age; |
| 155 | + } |
| 156 | + |
| 157 | + Animal build() { |
| 158 | + return new Animal(this); |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + /* |
| 164 | + ************************************* |
| 165 | + * Unit tests |
| 166 | + ************************************* |
| 167 | + */ |
| 168 | + |
| 169 | + public void testWithUnwrappedAndCreatorSingleParameterAtBeginning() throws Exception { |
| 170 | + final String json = aposToQuotes("{'person_id':1234,'first_name':'John','last_name':'Doe','years_old':30,'living':true}"); |
| 171 | + |
| 172 | + final ObjectMapper mapper = new ObjectMapper(); |
| 173 | + Person person = mapper.readValue(json, Person.class); |
| 174 | + assertEquals(1234, person.getId()); |
| 175 | + assertNotNull(person.getName()); |
| 176 | + assertEquals("John", person.getName().getFirst()); |
| 177 | + assertEquals("Doe", person.getName().getLast()); |
| 178 | + assertEquals(30, person.getAge()); |
| 179 | + assertEquals(true, person.isAlive()); |
| 180 | + } |
| 181 | + |
| 182 | + public void testWithUnwrappedAndCreatorSingleParameterInMiddle() throws Exception { |
| 183 | + final String json = aposToQuotes("{'first_name':'John','last_name':'Doe','person_id':1234,'years_old':30,'living':true}"); |
| 184 | + |
| 185 | + final ObjectMapper mapper = new ObjectMapper(); |
| 186 | + Person person = mapper.readValue(json, Person.class); |
| 187 | + assertEquals(1234, person.getId()); |
| 188 | + assertNotNull(person.getName()); |
| 189 | + assertEquals("John", person.getName().getFirst()); |
| 190 | + assertEquals("Doe", person.getName().getLast()); |
| 191 | + assertEquals(30, person.getAge()); |
| 192 | + assertEquals(true, person.isAlive()); |
| 193 | + } |
| 194 | + |
| 195 | + public void testWithUnwrappedAndCreatorSingleParameterAtEnd() throws Exception { |
| 196 | + final String json = aposToQuotes("{'first_name':'John','last_name':'Doe','years_old':30,'living':true,'person_id':1234}"); |
| 197 | + |
| 198 | + final ObjectMapper mapper = new ObjectMapper(); |
| 199 | + Person person = mapper.readValue(json, Person.class); |
| 200 | + assertEquals(1234, person.getId()); |
| 201 | + assertNotNull(person.getName()); |
| 202 | + assertEquals("John", person.getName().getFirst()); |
| 203 | + assertEquals("Doe", person.getName().getLast()); |
| 204 | + assertEquals(30, person.getAge()); |
| 205 | + assertEquals(true, person.isAlive()); |
| 206 | + } |
| 207 | + |
| 208 | + public void testWithUnwrappedAndCreatorMultipleParametersAtBeginning() throws Exception { |
| 209 | + final String json = aposToQuotes("{'animal_id':1234,'living':true,'first_name':'John','last_name':'Doe','years_old':30}"); |
| 210 | + |
| 211 | + final ObjectMapper mapper = new ObjectMapper(); |
| 212 | + Animal animal = mapper.readValue(json, Animal.class); |
| 213 | + assertEquals(1234, animal.getId()); |
| 214 | + assertNotNull(animal.getName()); |
| 215 | + assertEquals("John", animal.getName().getFirst()); |
| 216 | + assertEquals("Doe", animal.getName().getLast()); |
| 217 | + assertEquals(30, animal.getAge()); |
| 218 | + assertEquals(true, animal.isAlive()); |
| 219 | + } |
| 220 | + |
| 221 | + public void testWithUnwrappedAndCreatorMultipleParametersInMiddle() throws Exception { |
| 222 | + final String json = aposToQuotes("{'first_name':'John','animal_id':1234,'last_name':'Doe','living':true,'years_old':30}"); |
| 223 | + |
| 224 | + final ObjectMapper mapper = new ObjectMapper(); |
| 225 | + Animal animal = mapper.readValue(json, Animal.class); |
| 226 | + assertEquals(1234, animal.getId()); |
| 227 | + assertNotNull(animal.getName()); |
| 228 | + assertEquals("John", animal.getName().getFirst()); |
| 229 | + assertEquals("Doe", animal.getName().getLast()); |
| 230 | + assertEquals(30, animal.getAge()); |
| 231 | + assertEquals(true, animal.isAlive()); |
| 232 | + } |
| 233 | + |
| 234 | + public void testWithUnwrappedAndCreatorMultipleParametersAtEnd() throws Exception { |
| 235 | + final String json = aposToQuotes("{'first_name':'John','last_name':'Doe','years_old':30,'living':true,'animal_id':1234}"); |
| 236 | + |
| 237 | + final ObjectMapper mapper = new ObjectMapper(); |
| 238 | + Animal animal = mapper.readValue(json, Animal.class); |
| 239 | + assertEquals(1234, animal.getId()); |
| 240 | + assertNotNull(animal.getName()); |
| 241 | + assertEquals("John", animal.getName().getFirst()); |
| 242 | + assertEquals("Doe", animal.getName().getLast()); |
| 243 | + assertEquals(30, animal.getAge()); |
| 244 | + assertEquals(true, animal.isAlive()); |
| 245 | + } |
| 246 | +} |
0 commit comments