Last active
August 29, 2015 14:11
-
-
Save sp3c73r2038/1996180682e595f1ea98 to your computer and use it in GitHub Desktop.
jackson-databind ObjectMapper demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.momoka; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import com.fasterxml.jackson.annotation.JsonAutoDetect; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.fasterxml.jackson.databind.SerializationFeature; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
/** | |
* Hello world! | |
* | |
*/ | |
public class App { | |
private static final Logger LOGGER = LoggerFactory.getLogger(App.class); | |
public static void main(String[] args) throws IOException { | |
ObjectMapper om = new ObjectMapper(); | |
// om.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); | |
om.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false); | |
// om.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES); | |
Hello value = new Hello(); | |
value.setName("example"); | |
List<Hello> items = new ArrayList<Hello>(); | |
items.add(value); | |
items.add(value); | |
Result result = new Result(); | |
result.setItems(items); | |
HashMap<String, Object> map = om.convertValue(result, HashMap.class); | |
String obj = om.writeValueAsString(map); | |
LOGGER.info("{}", obj); | |
} | |
@JsonAutoDetect | |
static class Result { | |
private List<Hello> items; | |
private String status; | |
public List<Hello> getItems() { | |
return this.items; | |
} | |
public void setItems(List<Hello> items) { | |
this.items = items; | |
} | |
public String getStatus() { | |
return this.status; | |
} | |
} | |
static class Hello { | |
private String name; | |
private Integer age; | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public Integer getAge() { | |
return this.age; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>net.momoka</groupId> | |
<artifactId>hello</artifactId> | |
<packaging>jar</packaging> | |
<version>1.0</version> | |
<name>hello</name> | |
<url>http://maven.apache.org</url> | |
<dependencies> | |
<dependency> | |
<groupId>com.fasterxml.jackson.core</groupId> | |
<artifactId>jackson-databind</artifactId> | |
<version>2.4.4</version> | |
</dependency> | |
<dependency> | |
<groupId>org.codehaus.jackson</groupId> | |
<artifactId>jackson-mapper-asl</artifactId> | |
<version>1.9.13</version> | |
</dependency> | |
<dependency> | |
<groupId>ch.qos.logback</groupId> | |
<artifactId>logback-classic</artifactId> | |
<version>1.1.2</version> | |
</dependency> | |
<dependency> | |
<groupId>com.googlecode.json-simple</groupId> | |
<artifactId>json-simple</artifactId> | |
<version>1.1.1</version> | |
</dependency> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>3.8.1</version> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment