JSON之Jackson(一)
JSON之Jackson(二)
Jackson JsonParser比Jackson ObjectMapper更底层,这就使得Jackson JsonParser比ObjectMapper更快,但是也更加笨重。Jackson JsonParser的工作方式是:将JSON分成一个记号序列,让你迭代记号(Token)序列进行解析。Jackson Parser的Token有:
{ "name": { "first": "Joe", "last": "Sixpack" }, "gender": "MALE", "verified": false, "userImage": "Rm9vYm" }
public static void readFromFile(){ String path = "F:\\Codes\\IDEA\\Tools\\data\\json\\user.json"; // String carJson = "{ \"brand\" : \"Mercedes\", \"doors\" : 5 }"; JsonFactory jsonFactory = new JsonFactory(); User user = new User(); try { JsonParser parser = jsonFactory.createParser(new File(path)); parser.nextToken(); //进入JSON的“{” while (parser.nextToken() != JsonToken.END_OBJECT){ String fieldName = parser.getCurrentName(); parser.nextToken(); if(fieldName.equals("name")) { System.out.println(fieldName + ":"); while (parser.nextToken() != JsonToken.END_OBJECT) { parser.nextToken(); String field = parser.getCurrentName(); System.out.println("\t" + field + ": " + parser.getValueAsString()); } } else { System.out.println(fieldName + ": " + parser.getText()); } } parser.close(); } catch (Exception e){ e.printStackTrace(); } }
String carJson = "{ \"brand\" : \"Mercedes\", \"doors\" : 5 }"; JsonFactory factory = new JsonFactory(); JsonParser parser = factory.createParser(carJson);
// Write simple variables to file public static void writeSimpleToFile(){ JsonFactory jsonFactory = new JsonFactory(); OutputStream outputStream = null; // Car car = new Car("BMW", 4, new Car.Owner("Gatsby", "Newton"), new String[]{"engine", "brake"}); try { outputStream = new FileOutputStream("F:\\Codes\\IDEA\\Tools\\data\\json\\out\\generate_simple.json"); JsonGenerator generator = jsonFactory.createGenerator(outputStream, JsonEncoding.UTF8); generator.writeStartObject(); generator.writeStringField("brand", "Mercedes"); generator.writeNumberField("doors", 5); generator.writeObjectFieldStart("owner"); generator.writeStringField("first", "Gatsby"); generator.writeStringField("last", "Newton"); generator.writeEndObject(); generator.writeArrayFieldStart("component"); generator.writeString("engine"); generator.writeString("brake"); generator.writeEndArray(); // Write a object. // generator.writeObjectField("owner", new Car.Owner("Gatsby", "Newton")); // generator.writeObjectField("component", new String[]{"engine", "brake"}); generator.writeEndObject(); generator.flush(); generator.close(); } catch (Exception e){ e.printStackTrace(); } }
java.lang.IllegalStateException: No ObjectCodec defined for the generator,can only serialize simple wrapper types (type passed edu.wzm.jackson.Car$Owner)
// Write objects to file. public static void writeComplexToFile(){ // If you use JsonFactory jsonFactory = new JsonFactory(), it shows the error: // java.lang.IllegalStateException: No ObjectCodec defined for the generator, // can only serialize simple wrapper types (type passed edu.wzm.jackson.Car$Owner) JsonFactory jsonFactory = new ObjectMapper().getJsonFactory(); OutputStream outputStream = null; // Car car = new Car("BMW", 4, new Car.Owner("Gatsby", "Newton"), new String[]{"engine", "brake"}); try { outputStream = new FileOutputStream("F:\\Codes\\IDEA\\Tools\\data\\json\\out\\generate_complex.json"); JsonGenerator generator = jsonFactory.createGenerator(outputStream, JsonEncoding.UTF8); generator.writeStartObject(); generator.writeStringField("brand", "Mercedes"); generator.writeNumberField("doors", 5); // Write a object. generator.writeObjectField("owner", new Car.Owner("Gatsby", "Newton")); generator.writeObjectField("component", new String[]{"engine", "brake"}); generator.writeEndObject(); generator.flush(); generator.close(); } catch (Exception e){ e.printStackTrace(); } }
例子的源代码托管在:https://github.com/GatsbyNewton/jackson