天天看點

fasterxml Jackson序列化與反序列化實作,基本使用以及進階特性測試

基本使用以及進階特性測試

  • ​​寫在前面​​
  • ​​一、簡單操作​​
  • ​​1.1、String to Object​​
  • ​​1.2、file to Object​​
  • ​​1.3、string to map​​
  • ​​1.4、寫測試1​​
  • ​​1.5、寫測試2​​
  • ​​1.6、時間轉換1​​
  • ​​1.7、時間轉換2​​
  • ​​1.8、jsonNode​​
  • ​​二、進階特性​​
  • ​​2.1、SerializationFeature​​
  • ​​2.1.1、​​
  • ​​2.2、DeserializationFeature​​
  • ​​2.2.1、​​
  • ​​三、注解使用​​

寫在前面

一、簡單操作

1.1、String to Object

/**
     * json string to Object
     *
     * @throws JsonProcessingException
     */
    @Test
    public void t1() throws JsonProcessingException {
        String carJson = "{ " +
                "\"brand\" : \"Mercedes\"," +
                "\"doors\" : 5 " +
                "}";
        Car car = objectMapper.readValue(carJson, Car.class);

        System.out.println("car brand = " + car.getBrand());
        System.out.println("car doors = " + car.getDoors());
        System.out.println(objectMapper.writeValueAsString(car));
    }      
/**
     * Car{brand='ford', doors=0}
     * Car{brand='Fiat', doors=0}
     *
     * @throws IOException
     */
    @Test
    public void t2_2() throws IOException {
        String jsonArray = "[{\"brand\":\"ford\"}, {\"brand\":\"Fiat\"}]";
        ObjectMapper objectMapper = new ObjectMapper();
        List<Car> cars1 = objectMapper.readValue(jsonArray, new TypeReference<List<Car>>() {
        });
        cars1.forEach(System.out::println);
    }      

1.2、file to Object

/**
     * json File to Object
     * <p>
     * {"brand":"Mercedes","doors":5}
     *
     * @throws JsonProcessingException
     */
    @Test
    public void t2() throws IOException {

        File file = new File("D:\\GitRepository\\multiProject\\Jackson\\src\\test\\resources\\json\\car.json");

        Car car = objectMapper.readValue(file, Car.class);
        System.out.println(objectMapper.writeValueAsString(car));
    }      
/**
     * {
     * "brand": "Mercedes",
     * "doors": 5
     * }
     * tonels.domian.Car@78e67e0a
     *
     * @throws IOException
     */
    @Test
    public void t2_1() throws IOException {
        String resource = "json/car.json";
        InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
        String text = IOUtils.toString(is);
        System.out.println(text);

        final Car car = objectMapper.readValue(text, Car.class);
        System.out.println(car.toString());
    }      

1.3、string to map

/**
     * {brand=ford, doors=5}
     *
     * @throws IOException
     */
    @Test
    public void t2_3() throws IOException {
        String jsonObject = "{\"brand\":\"ford\", \"doors\":5}";
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, Object> jsonMap = objectMapper.readValue(jsonObject,
                new TypeReference<Map<String, Object>>() {
                });
        System.out.println(jsonMap);
    }      

1.4、寫測試1

/**
     * 寫測試
     * @throws IOException
     */
    @Test
    public void t3() throws IOException {
        Car car = new Car();
        car.setBrand("BMW" );
        car.setDoors(4) ;
        objectMapper.writeValue(
                new FileOutputStream("json/output-2.json"), car);
    }      

1.5、寫測試2

/**
     * {"brand":"BMW","doors":4}
     * @throws IOException
     */
    @Test
    public void t3_1() throws IOException {
        Car car = new Car();
        car.setBrand("BMW" );
        car.setDoors(4) ;
        String json = objectMapper.writeValueAsString(car);
        System.out.println(json);
    }      

1.6、時間轉換1

/**
     * {"type":"transfer","date":1591519578900}
     * @throws IOException
     */
    @Test
    public void t4() throws IOException {
        Transaction transaction = new Transaction("transfer", new Date());
        String output = objectMapper.writeValueAsString(transaction);
        System.out.println(output);
    }      

1.7、時間轉換2

/**
     * {"type":"transfer","date":"2020-06-07"}
     * @throws IOException
     */
    @Test
    public void t4_1() throws IOException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        objectMapper.setDateFormat(dateFormat);

        Transaction transaction = new Transaction("transfer", new Date());
        String output2 = objectMapper.writeValueAsString(transaction);
        System.out.println(output2);
    }      

1.8、jsonNode

/**
     *
     * brand = Mercedes
     * doors = 5
     * john  =
     * field = value
     * @throws IOException
     */
    @Test
    public void t5_1() throws IOException {
        String carJson =
                "{ \"brand\" : \"Mercedes\", \"doors\" : 5," +
                        "  \"owners\" : [\"John\", \"Jack\", \"Jill\"]," +
                        "  \"nestedObject\" : { \"field\" : \"value\" } }";
        try {
            JsonNode jsonNode = objectMapper.readValue(carJson, JsonNode.class);

            JsonNode brandNode = jsonNode.get("brand");
            String brand = brandNode.asText();
            System.out.println("brand = " + brand);

            JsonNode doorsNode = jsonNode.get("doors");
            int doors = doorsNode.asInt();
            System.out.println("doors = " + doors);

            JsonNode array = jsonNode.get("owners");
            JsonNode jsonNode1 = array.get(0);
            String john = jsonNode.asText();
            System.out.println("john  = " + john);

            JsonNode child = jsonNode.get("nestedObject");
            JsonNode childField = child.get("field");
            String field = childField.asText();
            System.out.println("field = " + field);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }      

二、進階特性

這裡主要包括 SerializationFeature 和 DeserializationFeature 兩個枚舉對象

fasterxml Jackson序列化與反序列化實作,基本使用以及進階特性測試

2.1、SerializationFeature

2.1.1、

2.2、DeserializationFeature

2.2.1、

三、注解使用