2009年6月16日火曜日

GSONを使ってみる。

GSON JavaオブジェクトとJSONの変換ライブラリ



① Mavenリポジトリの設定

    <!-- リポジトリ -->

    <repositories>

        <!-- GSONのリポジトリ追加 -->

        <repository>

            <id>gson</id>

            <url>http://google-gson.googlecode.com/svn/mavenrepo</url>

            <snapshots>

                <enabled>true</enabled>

            </snapshots>

            <releases>

                <enabled>true</enabled>

            </releases>

        </repository>

    </repositories>

    

    <dependencies>

        <!-- Gson: Java to Json conversion -->

        <dependency>

            <groupId>com.google.code.gson</groupId>

            <artifactId>gson</artifactId>

            <version>1.3</version>

            <scope>compile</scope>

        </dependency>

    </dependencies>

    



② 使い方

== Bean.java ==

public class Bean {



    private String a;

    private Integer b;

    private BBB[] bbbs;



    public BBB[] getBbbs() {

        return bbbs;

    }



    public void setBbbs(BBB[] bbbs) {

        this.bbbs = bbbs;

    }



    public String getA() {

        return a;

    }



    public void setA(String a) {

        this.a = a;

    }



    public Integer getB() {

        return b;

    }



    public void setB(Integer b) {

        this.b = b;

    }

}





== BBB.java ==

public class BBB {



    private String c;

    private String d;



    public String getC() {

        return c;

    }



    public void setC(String c) {

        this.c = c;

    }



    public String getD() {

        return d;

    }



    public void setD(String d) {

        this.d = d;

    }

}





== GsonTest.java ==

import com.google.gson.Gson;



import junit.framework.TestCase;



public class GsonTest extends TestCase {



    protected void setUp() throws Exception {

        super.setUp();

    }



    protected void tearDown() throws Exception {

        super.tearDown();

    }



    public void testGson() throws Exception {

        Bean bean = new Bean();

        bean.setA("aa");

        bean.setB(9);



        BBB b1 = new BBB();

        b1.setC("333");

        b1.setD("3d3d");



        BBB b2 = new BBB();

        b2.setC("444");

        b2.setD("4d4d");



        BBB[] bbbs = { b1, b2 };



        bean.setBbbs(bbbs);



        Gson gson = new Gson();

        String json = gson.toJson(bean);

        assertNotNull(json);

        // System.out.println(json);



        Bean bean2 = gson.fromJson(json, Bean.class);



        assertNotNull(bean2);

        assertEquals(bean.getA(), bean2.getA());

        assertEquals(bean.getB(), bean2.getB());

        assertNotNull(bean2.getBbbs());

        assertEquals(bean.getBbbs().length, bean2.getBbbs().length);



        for (int i = 0; i < bean2.getBbbs().length; i++) {

            assertEquals(bean.getBbbs()[i].getC(), bean2.getBbbs()[i].getC());

            assertEquals(bean.getBbbs()[i].getD(), bean2.getBbbs()[i].getD());

        }

    }

}





楽だね(^(エ)^)