原文链接:
http://jackson.codehaus.org/Tutorial
Jackson有三种处理方式:
Streaming API
数据绑定 它允许我们把Json转换成Java对象,也可以把Java对象转换成Json对象
树模型 通过进驻内存的树形结构来方便的访问Json数据
从使用的角度看,上述三种方式可以归纳为 :
Java类:
public class User {
public enum Gender { MALE, FEMALE };
public static class Name {
private String _first, _last;
public String getFirst() { return _first; }
public String getLast() { return _last; }
public void setFirst(String s) { _first = s; }
public void setLast(String s) { _last = s; }
}
private Gender _gender;
private Name _name;
private boolean _isVerified;
private byte[] _userImage;
public Name getName() { return _name; }
public boolean isVerified() { return _isVerified; }
public Gender getGender() { return _gender; }
public byte[] getUserImage() { return _userImage; }
public void setName(Name n) { _name = n; }
public void setVerified(boolean b) { _isVerified = b; }
public void setGender(Gender g) { _gender = g; }
public void setUserImage(byte[] b) { _userImage = b; }
}
public class User { public enum Gender { MALE, FEMALE }; public static class Name { private String _first, _last; public String getFirst() { return _first; } public String getLast() { return _last; } public void setFirst(String s) { _first = s; } public void setLast(String s) { _last = s; } } private Gender _gender; private Name _name; private boolean _isVerified; private byte[] _userImage; public Name getName() { return _name; } public boolean isVerified() { return _isVerified; } public Gender getGender() { return _gender; } public byte[] getUserImage() { return _userImage; } public void setName(Name n) { _name = n; } public void setVerified(boolean b) { _isVerified = b; } public void setGender(Gender g) { _gender = g; } public void setUserImage(byte[] b) { _userImage = b; } }
JSon数据:
{
"name" : { "first" : "Joe", "last" : "Sixpack" },
"gender" : "MALE",
"verified" : false,
"userImage" : "Rm9vYmFyIQ=="
}
{ "name" : { "first" : "Joe", "last" : "Sixpack" }, "gender" : "MALE", "verified" : false, "userImage" : "Rm9vYmFyIQ==" }
Streaming API (这部分我比较关注,就先翻译下)比数据绑定的方式会快20%到30%
写操作:
JsonFactory f = new JsonFactory();
JsonGenerator g = f.createJsonGenerator(new File("user.json"));
g.writeStartObject();
g.writeObjectFieldStart("name");
g.writeStringField("first", "Joe");
g.writeStringField("last", "Sixpack");
g.writeEndObject(); // for field 'name'
g.writeStringField("gender", Gender.MALE);
g.writeBooleanField("verified", false);
g.writeFieldName("userImage"); // no 'writeBinaryField' (yet?)
byte[] binaryData = ...;
g.writeBinary(binaryData);
g.writeEndObject();
JsonFactory f = new JsonFactory(); JsonGenerator g = f.createJsonGenerator(new File("user.json")); g.writeStartObject(); g.writeObjectFieldStart("name"); g.writeStringField("first", "Joe"); g.writeStringField("last", "Sixpack"); g.writeEndObject(); // for field 'name' g.writeStringField("gender", Gender.MALE); g.writeBooleanField("verified", false); g.writeFieldName("userImage"); // no 'writeBinaryField' (yet?) byte[] binaryData = ...; g.writeBinary(binaryData); g.writeEndObject();
读操作:
JsonFactory f = new JsonFactory();
JsonParser jp = f.createJsonParser(new File("user.json"));
User user = new User();
jp.nextToken(); // will return JsonToken.START_OBJECT (verify?)
while (jp.nextToken() != JsonToken.END_OBJECT) {
String fieldname = jp.getCurrentName();
jp.nextToken(); // move to value, or START_OBJECT/START_ARRAY
if ("name".equals(fieldname)) { // contains an object
Name name = new Name();
while (jp.nextToken() != JsonToken.END_OBJECT) {
String namefield = jp.getCurrentName();
jp.nextToken(); // move to value
if ("first".equals(namefield)) {
name.setFirst(jp.getText());
} else if ("last".equals(namefield)) {
name.setLast(jp.getText());
} else {
throw new IllegalStateException("Unrecognized field '"+fieldname+"'!");
}
}
user.setName(name);
} else if ("gender".equals(fieldname)) {
user.setGender(Gender.valueOf(jp.getText()));
} else if ("verified".equals(fieldname)) {
user.setVerified(jp.getCurrentToken() == JsonToken.VALUE_TRUE);
} else if ("userImage".equals(fieldname)) {
user.setUserImage(jp.getBinaryValue());
} else {
throw new IllegalStateException("Unrecognized field '"+fieldname+"'!");
}
ReadJSON.java JsonFactory f = new JsonFactory(); JsonParser jp = f.createJsonParser(new File("user.json")); User user = new User(); jp.nextToken(); // will return JsonToken.START_OBJECT (verify?) while (jp.nextToken() != JsonToken.END_OBJECT) { String fieldname = jp.getCurrentName(); jp.nextToken(); // move to value, or START_OBJECT/START_ARRAY if ("name".equals(fieldname)) { // contains an object Name name = new Name(); while (jp.nextToken() != JsonToken.END_OBJECT) { String namefield = jp.getCurrentName(); jp.nextToken(); // move to value if ("first".equals(namefield)) { name.setFirst(jp.getText()); } else if ("last".equals(namefield)) { name.setLast(jp.getText()); } else { throw new IllegalStateException("Unrecognized field '"+fieldname+"'!"); } } user.setName(name); } else if ("gender".equals(fieldname)) { user.setGender(Gender.valueOf(jp.getText())); } else if ("verified".equals(fieldname)) { user.setVerified(jp.getCurrentToken() == JsonToken.VALUE_TRUE); } else if ("userImage".equals(fieldname)) { user.setUserImage(jp.getBinaryValue()); } else { throw new IllegalStateException("Unrecognized field '"+fieldname+"'!"); } }
数据绑定:
//读
ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally
User user = mapper.readValue(new File("user.json"), User.class);
//写
mapper.writeValue(<span class="code-keyword">new</span>
File(<span class="code-quote">"user-modified.json"</span>
), user);}
//读 ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally User user = mapper.readValue(new File("user.json"), User.class); //写 mapper.writeValue(new File("user-modified.json" ), user);
无类型的数据绑定:
HashMap<String,Object> untyped = new HashMap<String,Object>();
HashMap<String,String> nameStruct = new HashMap<String,String>();
nameStruct.put("first", "Joe");
nameStruct.put("last", "Sixpack");
untyped.put("name", nameStruct);
untyped.put("gender", "MALE");
untyped.put("verified", Boolean.FALSE);
untyped.put("userImage", "Rm9vYmFyIQ==");
HashMap<String,Object> untyped = new HashMap<String,Object>(); HashMap<String,String> nameStruct = new HashMap<String,String>(); nameStruct.put("first", "Joe"); nameStruct.put("last", "Sixpack"); untyped.put("name", nameStruct); untyped.put("gender", "MALE"); untyped.put("verified", Boolean.FALSE); untyped.put("userImage", "Rm9vYmFyIQ==");
树模型:
可以使用Stream API或者数据绑定的方式写或者ObjectMapper读取树树结构
ObjectMapper的方式:
ObjectMapper m = new ObjectMapper(); // can either use mapper.readTree(JsonParser), or bind to JsonNode JsonNode rootNode = m.readValue(new File("user.json"), JsonNode.class); // ensure that "last name" isn't "Xmler"; if is, change to "Jsoner" JsonNode nameNode = rootNode.path("name"); String lastName = nameNode.path("last").getTextValue(). if ("xmler".equalsIgnoreCase(lastName)) { ((ObjectNode)nameNode).put("last", "Jsoner"); } // and write it out: m.writeValue(new File("user-modified.json"), rootNode);
ObjectMapper m = new ObjectMapper(); // can either use mapper.readTree(JsonParser), or bind to JsonNode JsonNode rootNode = m.readValue(new File("user.json"), JsonNode.class); // ensure that "last name" isn't "Xmler"; if is, change to "Jsoner" JsonNode nameNode = rootNode.path("name"); String lastName = nameNode.path("last").getTextValue(). if ("xmler".equalsIgnoreCase(lastName)) { ((ObjectNode)nameNode).put("last", "Jsoner"); } // and write it out: m.writeValue(new File("user-modified.json"), rootNode);
绝招:可以在JsonParser和JsonGenerator中直接使用数据绑定或者树模型的方式的,看一下以下方法就知道怎么用了