SoftLeader Tech. Corp.
2014.07
Jackson Training
Main Projects
• jackson-core
• jackson-annotations
• jackson-databind
• Third-party
 Guava
 Hibernate
 HPPC
 Joda
 or...
Maven
• groupId : com.fasterxml.jackson.core
• artifactId:
 jackson-core
 jackson-databind
 jackson-annotations
• versi...
What is JSON format
{
"id": 1,
"account": "admin",
"name": "Administrator"
}
if in XML , too complex and bigger size.
<roo...
JSON is simpler
• Property
{"attribute1 ": "string ", "attribute2 ": number}
• List
{"attribute3 ": ["Apple", "Banana", "C...
JSON Parser
• Streaming
• Tree Traversing
• Data Binding
Data Binding
ObjectMapper objectMapper = new ObjectMapper();
mapper.readValue( src, MyBean.class);
MyBean myBean = new MyB...
Java Mapping
JSON Type Java Type
object LinkedHashMap<String, Object>
array ArrayList<Object>
string String
number ( no fr...
Basic Annotations
• @JsonProperty 對應到 json 的屬性值
• @JsonIgnore 忽略輸入與輸出 // property level
• @JsonIgnoreProperties({.., ..}) ...
parent-child
• @JsonTypeInfo(use=Id.CLASS, include=As.PROPERTY,
property="softleader@class")
{
"pets":[
{
"name":"Mars",
"...
Joda-time
• jackson-datatype-joda
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JodaModule());
Q & A
Upcoming SlideShare
Loading in …5
×

SoftLeader Jackson Training

360 views
279 views

Published on

Java Object and JSON data mapping by FasterXML Jackson

Published in: Software
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
360
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

SoftLeader Jackson Training

  1. 1. SoftLeader Tech. Corp. 2014.07 Jackson Training
  2. 2. Main Projects • jackson-core • jackson-annotations • jackson-databind • Third-party  Guava  Hibernate  HPPC  Joda  org.json  JSR-310 ( Java 8 Date )  JSR-353 ( Java JSON API )  JDK7
  3. 3. Maven • groupId : com.fasterxml.jackson.core • artifactId:  jackson-core  jackson-databind  jackson-annotations • version  now is 2.4.0
  4. 4. What is JSON format { "id": 1, "account": "admin", "name": "Administrator" } if in XML , too complex and bigger size. <root> <user id= "1"> <account>admin</account> <name>Administrator</name> </user> </root>
  5. 5. JSON is simpler • Property {"attribute1 ": "string ", "attribute2 ": number} • List {"attribute3 ": ["Apple", "Banana", "Cherry"]} • Map/Object {"attribute4":{"field1":"value1", "field2":"value2"}}
  6. 6. JSON Parser • Streaming • Tree Traversing • Data Binding
  7. 7. Data Binding ObjectMapper objectMapper = new ObjectMapper(); mapper.readValue( src, MyBean.class); MyBean myBean = new MyBean(); mapper.writeValue(dst, myBean); • File • InputStream / OutputStream • Reader / Writer • String
  8. 8. Java Mapping JSON Type Java Type object LinkedHashMap<String, Object> array ArrayList<Object> string String number ( no fraction) Integer, Long, BigInteger number ( fraction ) Double (  BigDecimal ) true/false Boolean null null
  9. 9. Basic Annotations • @JsonProperty 對應到 json 的屬性值 • @JsonIgnore 忽略輸入與輸出 // property level • @JsonIgnoreProperties({.., ..}) // class level • @JsonIgnoreProperties(ignoreUnknown=true)
  10. 10. parent-child • @JsonTypeInfo(use=Id.CLASS, include=As.PROPERTY, property="softleader@class") { "pets":[ { "name":"Mars", "softleader@class":"tw.com.softleader.json.jackson.entity.Question07Dog", "age":7 }, { "name":"Tiger", "softleader@class":"tw.com.softleader.json.jackson.entity.Question07Cat", "color":"yellow“ } ] }
  11. 11. Joda-time • jackson-datatype-joda ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new JodaModule());
  12. 12. Q & A

×