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
     * 

* {"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序列化与反序列化实现,基本使用以及高级特性测试_第1张图片

2.1、SerializationFeature

2.1.1、

2.2、DeserializationFeature

2.2.1、

待补充

三、注解使用

待补充

你可能感兴趣的:(测试)