这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
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
25 changes: 25 additions & 0 deletions core-java-modules/core-java-collections-maps-8/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,24 @@
<version>0.0.1-SNAPSHOT</version>
</parent>

<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>${beanutils.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
Expand All @@ -26,4 +44,11 @@
</plugin>
</plugins>
</build>

<properties>
<gson.version>2.10.1</gson.version>
<jackson.version>2.17.0</jackson.version>
<beanutils.version>1.9.4</beanutils.version>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.baeldung.map.castingmaptoobject;

public class Address {
private String city;
private Country country;

public Address() {
// default constructor needed for Jackson deserialization
}

public Address(String city, Country country) {
this.city = city;
this.country = country;
}

public Country getCountry() {
return country;
}

public void setCountry(Country country) {
this.country = country;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Address address = (Address) o;

if (!city.equals(address.city)) return false;
return country.getName().equals(address.country.getName());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.baeldung.map.castingmaptoobject;

public class Country {
private String name;

public Country() {
// default constructor needed for Jackson deserialization
}

public Country(String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.baeldung.map.castingmaptoobject;

import java.util.List;

public class User {
private Long id;
private String name;
private List<Address> addresses;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public List<Address> getAddresses() {
return addresses;
}

public void setAddresses(List<Address> addresses) {
this.addresses = addresses;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.baeldung.map.castingmaptoobject;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import org.apache.commons.beanutils.BeanUtils;
import org.junit.jupiter.api.Test;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class CastingMapToObjectUnitTest {

private static final Map<String, Object> map = Map.of(
"id", 1L,
"name", "Baeldung",
"addresses", List.of(
new Address("La Havana", new Country("Cuba")),
new Address("Paris", new Country("France"))
)
);

@Test
void givenMap_whenCasting_thenThrow() {
assertThrows(ClassCastException.class, () -> { User user = (User) map; });
}

@Test
void givenMap_whenUsingBeanUtils_thenConvertToObject() throws InvocationTargetException, IllegalAccessException {
User user = new User();
BeanUtils.populate(user, map);

assertEqualsMapAndUser(map, user);
}

@Test
void givenMap_whenUsingJackson_thenConvertToObject() {
ObjectMapper objectMapper = new ObjectMapper();
User user = objectMapper.convertValue(map, User.class);

assertEqualsMapAndUser(map, user);
}

@Test
void givenMap_whenUsingJacksonWithWrongAttrs_thenThrow() {
Map<String, Object> modifiedMap = new HashMap<>(map);
modifiedMap.put("enabled", true);

ObjectMapper objectMapper = new ObjectMapper();

assertThrows(IllegalArgumentException.class, () -> objectMapper.convertValue(modifiedMap, User.class));
}

@Test
void givenMap_whenUsingJacksonIgnoreUnknownProps_thenConvertToObject() {
Map<String, Object> modifiedMap = new HashMap<>(map);
modifiedMap.put("enabled", true);

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
User user = objectMapper.convertValue(modifiedMap, User.class);

assertEqualsMapAndUser(modifiedMap, user);
}

@Test
void givenMap_whenUsingGson_thenConvertToObject() {
Gson gson = new Gson();
String jsonMap = gson.toJson(map);
User user = gson.fromJson(jsonMap, User.class);

assertEqualsMapAndUser(map, user);
}

private static void assertEqualsMapAndUser(Map<String, Object> map, User user) {
assertEquals(map.get("id"), user.getId());
assertEquals(map.get("name"), user.getName());
assertEquals(map.get("addresses"), user.getAddresses());
}

}