Tree Model:弹性最好(扩展性好)
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; } }
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 |
获取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);
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();