サンプルプログラム

JSON TYPE表現テスト (TypeTest.java)

package jp.sourceforge.reflex;

import java.util.Date;

import jp.sourceforge.reflex.core.ResourceMapper;
import model.Type;

public class TypeTest {

        public static void main(String[] args) {

                // モデルビーンのパッケージ名を指定してmapperをnewする
                IResourceMapper mapper = new ResourceMapper("model");

                // 値の設定
                Type type = new Type();
                type.type_str__ing = "文字";
                type.type_int = 1111;
                type.type_long = 2222222l;
                type.type_float = 3333.33f;
                type.type_double = 3333.33;
                type.type_date = new Date();
                type.type_Integer = 6666;
                type.type_Long = 7777777l;
                type.type_Float = 8888.88f;
                type.type_Double = 8888.88;

                type.type_str__ing_$attr1 = "属性1";
                type.type_str__ing_$attr2 = "属性2";
                type._$xml$lang = "ja";
                type.type_str__ing_$xml$lang = "jp";
                type.xml$atom = "間にコロン";
                type._$$text = "テキスト要素";
                type.setRevision(3);

                try {
                        // XMLにシリアライズ
                        String toXML = mapper.toXML(type);
                        System.out.println("\n【XML(Type) シリアライズテスト】:");
                        System.out.println(toXML);

                        // デシリアライズ
                        Type type_fromXML = (Type)mapper.fromXML(toXML);

                        toXML = mapper.toXML(type_fromXML);
                        System.out.println("\n上をXMLにデシリアライズ:");
                        System.out.println(toXML);

                        // JSONにシリアライズ
                        String toJSON = mapper.toJSON(type);
                        System.out.println("\n【JSON(Type) シリアライズテスト】:");
                        System.out.println(toJSON);

                        // デシリアライズ
                        Type type_fromJSON = (Type)mapper.fromJSON(toJSON);

                        toJSON = mapper.toJSON(type_fromJSON);
                        System.out.println("\n上をJSONにデシリアライズ:");
                        System.out.println(toJSON);

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

        }

}

Model(Type.java)

package model;

import java.util.Date;

public class Type {

        public String _$xml$lang;
        public String type_str__ing;
        public String type_str__ing_$attr1;
        public String type_str__ing_$attr2;
        public String type_str__ing_$xml$lang;
        public int type_int;
        public long type_long;
        public float type_float;
        public double type_double;
        public Date type_date;
        public Integer type_Integer;
        public Long type_Long;
        public Float type_Float;
        public Double type_Double;
        public String xml$atom;
        public String _$$text;
        private int revision;

        public int getRevision() {
                return this.revision;
        }
        public void setRevision(int revision) {
                this.revision = revision;
        }

}

実行結果

【XML(Type) シリアライズテスト】:
<type xml:lang="ja">
  <type_str-ing attr1="属性1" attr2="属性2" xml:lang="jp">文字</type_str-ing>
  <type_int>1111</type_int>
  <type_long>2222222</type_long>
  <type_float>3333.33</type_float>
  <type_double>3333.33</type_double>
  <type_date>2009-09-03T11:33:21+0900</type_date>
  <type_Integer>6666</type_Integer>
  <type_Long>7777777</type_Long>
  <type_Float>8888.88</type_Float>
  <type_Double>8888.88</type_Double>
  <xml:atom>間にコロン</xml:atom>テキスト要素</type>

上をXMLにデシリアライズ:
<type xml:lang="ja">
  <type_str-ing attr1="属性1" attr2="属性2" xml:lang="jp">文字</type_str-ing>
  <type_int>1111</type_int>
  <type_long>2222222</type_long>
  <type_float>3333.33</type_float>
  <type_double>3333.33</type_double>
  <type_date>2009-09-03T11:33:21+0900</type_date>
  <type_Integer>6666</type_Integer>
  <type_Long>7777777</type_Long>
  <type_Float>8888.88</type_Float>
  <type_Double>8888.88</type_Double>
  <xml:atom>間にコロン</xml:atom>テキスト要素</type>

【JSON(Type) シリアライズテスト】:
{"type" : {"____xml___lang" : "ja","type_str__ing" : "文字","type_str__ing____attr1" : "属性1","type_str__ing____attr2" : "属性2","type_str__ing____xml___lang" : "jp","type_int" : 1111,"type_long" : 2222222,"type_float" : 3333.33,"type_double" : 3333.33,"type_date" : "2009-09-03T11\:33\:21+0900","type_Integer" : 6666,"type_Long" : 7777777,"type_Float" : 8888.88,"type_Double" : 8888.88,"xml___atom" : "間にコロン","_______text" : "テキスト要素"}}

上をJSONにデシリアライズ:
{"type" : {"____xml___lang" : "ja","type_str__ing" : "文字","type_str__ing____attr1" : "属性1","type_str__ing____attr2" : "属性2","type_str__ing____xml___lang" : "jp","type_int" : 1111,"type_long" : 2222222,"type_float" : 3333.33,"type_double" : 3333.33,"type_date" : "2009-09-03T11\:33\:21+0900","type_Integer" : 6666,"type_Long" : 7777777,"type_Float" : 8888.88,"type_Double" : 8888.88,"xml___atom" : "間にコロン","_______text" : "テキスト要素"}}

JSON配列 (ReflexJSONTest.java)

package jp.sourceforge.reflex;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;

import jp.sourceforge.reflex.core.RXStream;
import model.Array;
import model.Element;


public class ReflexJSONTest {

        public static void main(String[] args) {

                // モデルビーンのパッケージ名を指定してmapperをnewする
                EntityMapper mapper = new RXStream("model");

                // 配列
                List<Element> elementList = new ArrayList<Element>();
                Element element1 = new Element();
                element1._$$text = "101";
                elementList.add(element1);
                Element element2 = new Element();
                element2._$$text = "102";
                elementList.add(element2);
                Element element3 = new Element();
                element3._$$text = "103";
                elementList.add(element3);
                Element element4 = new Element();
                element4._$$text = "104";
                elementList.add(element4);
                Element element5 = new Element();
                element5._$$text = "105";
                elementList.add(element5);
                Element element6 = new Element();
                element6._$$text = "106";
                elementList.add(element6);
                
                Array array = new Array();
                array.element = elementList;
                
                // XMLにシリアライズ
                String toXML = mapper.toXML(array);
                System.out.println("\n【XML(array) シリアライズテスト】:");
                System.out.println(toXML);

                // JSONにシリアライズ
                String toJSON = mapper.toJSON(array);
                System.out.println("\n【JSON(array) シリアライズテスト】:");
                System.out.println(toJSON);

                try {
                        // 試しにデシリアライズしてみる
                        Array arrayFromJSON = (Array) mapper.fromJSON(toJSON);
                        toJSON = mapper.toJSON(arrayFromJSON);
                        System.out.println("\n上をデシリアライズ:");
                        System.out.println(toJSON);
                
                } catch (JSONException e) {
                        e.printStackTrace();
                }
                
        }

}

Model (model.Array)

package model;

import java.util.List;

public class Array {

        public List<Element> element;
        
}


Model (model.Element)

package model;

public class Element {

        public final int _$$col = 3;    // 配列の要素数
        
        public String _$$text;
        
}


実行結果

【XML(array) シリアライズテスト】:
<array>
  <element>101</element>
  <element>102</element>
  <element>103</element>
  <element>104</element>
  <element>105</element>
  <element>106</element>
</array>

【JSON(array) シリアライズテスト】:
{"array" : {"element" : [["101","102","103"],["104","105","106"]]}}

上をデシリアライズ(XML):
<array>
  <element>101</element>
  <element>102</element>
  <element>103</element>
  <element>104</element>
  <element>105</element>
  <element>106</element>
</array>

上をデシリアライズ(JSON):
{"array" : {"element" : [["101","102","103"],["104","105","106"]]}}