mock.com.camel.json.JsonBeanTest.java Source code

Java tutorial

Introduction

Here is the source code for mock.com.camel.json.JsonBeanTest.java

Source

/*
 * Copyright (c) 2013, FPX and/or its affiliates. All rights reserved.
 * Use, Copy is subject to authorized license.
 */
package mock.com.camel.json;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

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

/**
 * json and bean transformer test
 * @author dengqb
 * @date 20151123
 */
public class JsonBeanTest {

    private ObjectMapper objectMapper;

    /**
     * @throws java.lang.Exception
     */
    @Before
    public void setUp() throws Exception {
        objectMapper = new ObjectMapper();
    }

    /**
     * @throws java.lang.Exception
     */
    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void testBeanToJson() throws JsonProcessingException {
        Car car = new Car();
        car.setBrand("");
        List<Map<String, String>> seatDescs = new ArrayList();
        Map map1 = new HashMap();
        map1.put(1, "?1??");
        seatDescs.add(map1);

        Map map2 = new HashMap();
        map2.put(2, "?2?");
        seatDescs.add(map2);

        Map map3 = new HashMap();
        map3.put(3, "?3??");
        seatDescs.add(map3);

        Map map4 = new HashMap();
        map4.put(4, "?4??");
        seatDescs.add(map4);

        Map map5 = new HashMap();
        map5.put(5, "?5???");
        seatDescs.add(map5);
        car.setSeats(seatDescs);

        Map uniqCodeMap = new HashMap();
        uniqCodeMap.put("enginCode", "12EABB");
        uniqCodeMap.put("boneCode", "ARCH3211");
        uniqCodeMap.put("turboCode", "Boch88801");
        car.setUniqCode(uniqCodeMap);

        String rs = objectMapper.writeValueAsString(car);
        System.out.println(rs);
    }

    private class Car {
        //?
        private String brand;
        //?
        private List seats;
        //???
        private Map uniqCode;

        public String getBrand() {
            return brand;
        }

        public List getSeats() {
            return seats;
        }

        public Map getUniqCode() {
            return uniqCode;
        }

        public void setBrand(String brand) {
            this.brand = brand;
        }

        public void setSeats(List seats) {
            this.seats = seats;
        }

        public void setUniqCode(Map uniqCode) {
            this.uniqCode = uniqCode;
        }
    }

}