Newer
Older
CosmosClient / app / src / test / java / com / example / cosmosclient / JacksonTest.java
package com.example.cosmosclient;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.junit.Test;

import java.io.IOException;

import static junit.framework.TestCase.assertEquals;

public class JacksonTest {
    @Test
    public void jacksonTest() {
        Container srcContainer = new Container();
        srcContainer.addComponent(new Component("A"));
        srcContainer.addComponent(new Component("B"));
        srcContainer.addComponent(new Component("C"));
        ObjectMapper om = new ObjectMapper();
        try {
            String containerJSON = om.writeValueAsString(srcContainer);
            System.out.println(containerJSON);

            ObjectMapper mo = new ObjectMapper();
            Container dstContainer = mo.readValue(containerJSON, Container.class);

            System.out.println(om.writeValueAsString(dstContainer));
            assertEquals(srcContainer, dstContainer);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        Container2 srcContainer2 = new Container2();
        srcContainer2.putComponent("X", new Component("A"));
        srcContainer2.putComponent("Y", new Component("B"));
        srcContainer2.putComponent("Z", new Component("C"));
        ObjectMapper om2 = new ObjectMapper();
        try {
            String containerJSON2 = om2.writeValueAsString(srcContainer2);
            System.out.println(containerJSON2);

            ObjectMapper mo2 = new ObjectMapper();
            Container2 dstContainer2 = mo2.readValue(containerJSON2, Container2.class);

            System.out.println(om2.writeValueAsString(dstContainer2));
            assertEquals(srcContainer2, dstContainer2);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}