jackson 流式API
2019-04-29 18:29 dogdogwang 阅读(1216) 评论(0) 编辑 收藏 举报http://www.cnblogs.com/lee0oo0/articles/2652528.html
Jackson提供了三种可选的JSON处理方法
1、流式API
com.fasterxml.jackson.core.JsonParser读
com.fasterxml.jackson.core.JsonGenerator写
2、树模型:提供一个 JSON 文档可变内存树的表示形式
com.fasterxml.jackson.databind.ObjectMapper生成树 ;树组成 JsonNode 节点集
树模型类似于 XML DOM
3、数据绑定:JSON和POJO相互转换,基于属性访问器规约或注解
有两种变体:简单和完整的数据绑定
简单数据绑定:是指从Java Map、List、String、Numbers、Boolean和空值进行转换
完整数据绑定:是指从任何Java bean 类型(及上文所述的"简单"类型)进行转换
com.fasterxml.jackson.databind.ObjectMapper对两个变种进行编排(marshalling)处理(写入JSON)和反编排(unmarshalling读JSON)
从使用的角度来看,总结这些3 种方法的用法如下
流 API: 性能最佳的方式 (最低开销、 速度最快的读/写; 其它二者基于它实现)。
数据绑定 :使用最方便的方式。
树模型: 最灵活的方式。
流式API示例
user.json
{
"name" : { "first" : "Joe", "last" : "Sixpack" },
"gender" : "MALE",
"verified" : false,
"userImage" : "Rm9vYmFyIQ=="
}
pojo
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; }
}
junit test
public class JsonStreamTest {
@Test
public void read() throws JsonParseException, IOException{
JsonFactory factory = new JsonFactory();
InputStream is = this.getClass().getClassLoader().getResourceAsStream("user.json");
JsonParser jsonParse = factory.createJsonParser(is);
User user = new User();
JsonToken token = jsonParse.nextToken();
if(token==JsonToken.START_OBJECT){
while((jsonParse.nextToken())!=JsonToken.END_OBJECT){
String fieldName = jsonParse.getCurrentName();
jsonParse.nextToken();
if("name".equalsIgnoreCase(fieldName)){
Name name = new Name();
while(jsonParse.nextToken()!=JsonToken.END_OBJECT){
if(jsonParse.getCurrentName().equals("first")){
name.setFirst(jsonParse.getText());
}
if(jsonParse.getCurrentName().equals("last")){
name.setLast(jsonParse.getText());
}
}
user.setName(name);
}else if("gender".equals(fieldName)){
String text = jsonParse.getText();
user.setGender(Gender.valueOf(text));
}else if("verified".equals(fieldName)){
user.setVerified(jsonParse.getBooleanValue());
}else if("userImage".equals(fieldName)){
user.setUserImage(jsonParse.getBinaryValue());
}
}
}
jsonParse.close();
}
@Test
public void write() throws IOException{
JsonFactory jsonFactory = new JsonFactory();
JsonGenerator jg = jsonFactory.createGenerator(new File("user2.json"), JsonEncoding.UTF8);
jg.useDefaultPrettyPrinter();
jg.writeStartObject();
jg.writeObjectFieldStart("name");
jg.writeStringField("first", "Joe");
jg.writeStringField("last", "Sixpack");
jg.writeEndObject();
jg.writeStringField("gender", Gender.MALE.name());
jg.writeBooleanField("verified", false);
jg.writeFieldName("userImage");
byte[] binaryData = new byte[]{70, 111, 111, 98, 97, 114, 33};
jg.writeBinary(binaryData);
jg.writeEndObject();
jg.close();
}
}