Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package beans;
- import java.io.File;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.LinkedHashMap;
- import java.util.Map;
- import com.fasterxml.jackson.core.JsonEncoding;
- import com.fasterxml.jackson.core.JsonFactory;
- import com.fasterxml.jackson.core.JsonGenerator;
- import com.fasterxml.jackson.core.JsonParser;
- import com.fasterxml.jackson.core.JsonToken;
- import com.fasterxml.jackson.databind.JsonNode;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import com.fasterxml.jackson.databind.node.ArrayNode;
- import com.fasterxml.jackson.databind.node.ObjectNode;
- public class DemoJackson {
- @SuppressWarnings({ "unchecked", "rawtypes" })
- public static void testDataBinding() {
- ObjectMapper om = new ObjectMapper();
- Map<String, Object> studentDataMap = new LinkedHashMap<String, Object>();
- Student stud = new Student();
- stud.setName("Dusko");
- stud.setAge(21);
- studentDataMap.put("student", stud);
- studentDataMap.put("faculty", "PMF");
- int[] marks = {9, 10, 10};
- studentDataMap.put("marks", marks);
- studentDataMap.put("verified", Boolean.FALSE);
- try {
- om.writeValue(new File("student1.json"), studentDataMap);
- } catch (IOException e) {
- e.printStackTrace();
- }
- try {
- LinkedHashMap<Student, Object> studentDataMapRead = om.readValue(new File("student1.json"), LinkedHashMap.class);
- LinkedHashMap<String, Object> studRead = (LinkedHashMap) studentDataMapRead.get("student");
- System.out.println(studRead.get("name") + " " + studRead.get("age"));
- System.out.println(studentDataMapRead.get("faculty") + " " + studentDataMapRead.get("verified"));
- ArrayList<Integer> marksRead = (ArrayList<Integer>) studentDataMapRead.get("marks");
- for (int i = 0; i < marksRead.size(); i++)
- System.out.println(marksRead.get(i) + " ");
- System.out.println();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- @SuppressWarnings("rawtypes")
- private static void testTreeMode() {
- ObjectMapper om = new ObjectMapper();
- String json = ("{\"student\": {\"name\": \"Dusko\", \"age\": 25}, \"faculty\":\"PMF\", \"marks\": [9, 10, 10], \"verified\": false}");
- JsonNode root;
- try {
- root = om.readTree(json);
- JsonNode student = root.path("student");
- String name = student.path("name").asText();
- int age = student.path("age").asInt();
- String faculty = root.path("faculty").asText();
- boolean verified = root.path("verified").asBoolean();
- JsonNode marks = root.path("marks");
- Iterator i = (Iterator) marks.elements();
- System.out.printf("%s %d %s %b\n", name, age, faculty, verified);
- while (i.hasNext())
- System.out.println(i.next());
- } catch (IOException e) {
- e.printStackTrace();
- }
- JsonNode rootWrite = om.createObjectNode();
- JsonNode marksNodeWrite = om.createArrayNode();
- ((ArrayNode) marksNodeWrite).add(10);
- ((ArrayNode) marksNodeWrite).add(10);
- ((ArrayNode) marksNodeWrite).add(10);
- JsonNode studNode = om.createObjectNode();
- ((ObjectNode) studNode).put("name", "Dusko");
- ((ObjectNode) studNode).put("age", 21);
- ((ObjectNode)rootWrite).set("student", studNode);
- ((ObjectNode)rootWrite).put("faculty", "PMF");
- ((ObjectNode)rootWrite).put("verified", Boolean.FALSE);
- ((ObjectNode)rootWrite).set("marks", marksNodeWrite);
- try {
- om.writeValue(new File("student2.json"), rootWrite);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static void testStreamingMode() {
- JsonFactory factory = new JsonFactory();
- JsonGenerator generator;
- try {
- generator = factory.createGenerator(new File("student3.json"), JsonEncoding.UTF8);
- generator.writeStartObject();
- generator.writeStringField("name", "Dusko");
- generator.writeNumberField("age", 21);
- generator.writeStringField("faculty", "PMF");
- generator.writeBooleanField("verified", Boolean.FALSE);
- generator.writeFieldName("marks");
- generator.writeStartArray();
- generator.writeNumber(10);
- generator.writeNumber(10);
- generator.writeNumber(10);
- generator.writeEndArray();
- generator.writeEndObject();
- generator.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- JsonParser parser;
- try {
- parser = factory.createParser(new File("student3.json"));
- while (parser.nextToken() != JsonToken.END_OBJECT) {
- String fieldName = parser.getCurrentName();
- if ("name".equals(fieldName)) {
- parser.nextToken();
- System.out.println(parser.getText());
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- // testDataBinding();
- // testTreeMode();
- testStreamingMode();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement