Jackson快速指南

JSON的三种方式

    Jackson提供了三种可替代的方式来处理JSON。
    streaming API:以分离事件来读、写Json内容。
        org.codehaus.jackson.JsonParser读,org.codehaus.jackson.JsonGenerator写
    Tree Model:提供易变的JSON文档在内存中以树的方式展现。
        org.codehaus.jackson.map.ObjectMapper可以构建树,多个树构建了JsonNode节点。
    Data Binding:基于属性或annotation进行JSON和POJO之间的互转。有两种变种:simple和full 数据绑定。
        simple data binding:从Java Maps,List,String和Numbers,Booleans和nulls的互转。
        full data binding:任意java类型的互转。
        org.codehaus.jackson.map.ObjectMapper完成组装(写Json)和解组(读JSON)。
    streaming api性能最好,最小开销,最快读写,其他两种都是基于它。
    Data Binding:最便利

    Tree Model:弹性最好(扩展性好)

例子

full data binding

    jackson的ObjectMapper仅仅映射Json数据到Java对象。

{
  "name" : { "first" : "Joe", "last" : "Sixpack" },
  "gender" : "MALE",
  "verified" : false,
  "userImage" : "Rm9vYmFyIQ=="
}
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);

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; }
}

raw数据绑定(无类型或简单数据)

    有时不想创建特定的java类来绑定Json,Untyped data binding会很好点。
Map<String,Object> userData = mapper.readValue(new File("user.json"), Map.class);
Map<String,Object> userData = new HashMap<String,Object>();
Map<String,String> nameStruct = new HashMap<String,String>();
nameStruct.put("first", "Joe");
nameStruct.put("last", "Sixpack");
userData.put("name", nameStruct);
userData.put("gender", "MALE");
userData.put("verified", Boolean.FALSE);
userData.put("userImage", "Rm9vYmFyIQ==");
mapper.writeValue(new File("user-modified.json"), userData);
Json Type Java Type
object LinkedHashMap<String,Object>
array ArrayList<Object>
string string
number (no fraction) Integer, Long or BigInteger (smallest applicable)
number (fraction) Double (configurable to use BigDecimal)
true|false Boolean
null null

使用泛型绑定数据

    除了绑定POJO外和simple类型外,还有另外一个变种:绑定泛型容器。这种情况下需要特殊处理:
    Map<String,User> result = mapper.readValue(src, new TypeReference<Map<String,User>>() { });
    此处TypeReference仅fancier需要传递泛型定义:<Map<String,User>>。

Tree Model Example

    获取Json对象的另一种方式就是构建tree。类似与XML的DOM tree。Jackson使用基本JsonNode来构建树。

    可以使用streaming api或ObjectMapper读取或写tree。

ObjectMapper m = new ObjectMapper();
// can either use mapper.readTree(source), or mapper.readValue(source, JsonNode.class);
JsonNode rootNode = m.readTree(new File("user.json"));
// 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");
}
m.writeValue(new File("user-modified.json"), rootNode);
TreeMapper treeMapper = new TreeMapper();
ObjectNode userOb = treeMapper.objectNode();
Object nameOb = userRoot.putObject("name");
nameOb.put("first", "Joe");
nameOb.put("last", "Sixpack");
userOb.put("gender", User.Gender.MALE.toString());
userOb.put("verified", false);
byte[] imageData = getImageData(); // or wherever it comes from
userOb.put("userImage", imageData);

Streaming API Example

生成Json:
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();
g.close();


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(User.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+"'!");
       }
}
jp.close();

你可能感兴趣的:(Jackson快速指南)