HHeLiBeXの日記 正道編

日々の記憶の記録とメモ‥

Simpleライブラリの検証(6)

ざっくりと試してみたSimpleライブラリ。

いろいろと気になる点もあったので、ざくっと検証してみる。の続き。

全プリミティブ型とその配列をシリアライズしてみる

いまさらだが、まぁ、基本ということで。

まず、各プリミティブ型の値を保持するためのPOJO

import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;

@Root
public class Primitives {

    @Element
    public boolean booleanValue;

    @Element
    public boolean[] booleanArray;

    @Element
    public byte byteValue;

    @Element
    public byte[] byteArray;

    @Element
    public short shortValue;

    @Element
    public short[] shortArray;

    @Element
    public int intValue;

    @Element
    public int[] intArray;

    @Element
    public long longValue;

    @Element
    public long[] longArray;

    @Element
    public char charValue;

    @Element
    public char[] charArray;

    @Element
    public float floatValue;

    @Element
    public float[] floatArray;

    @Element
    public double doubleValue;

    @Element
    public double[] doubleArray;

}

これを使用してシリアライズ/デシリアライズするプログラム。

import java.io.File;
import java.util.Arrays;

import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;

public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        System.out.println("=== serialize ===");
        try {
            Serializer serializer = new Persister();
            Primitives primitives = generatePrimitives();

            File result = new File("data/primitives.xml");
            System.out.println("      " + result);
            serializer.write(primitives, result);
        } catch (Exception e) {
            e.printStackTrace(System.out);
        }
        System.out.println("=== deserialize ===");
        try {
            Serializer serializer = new Persister();

            File source = new File("data/primitives.xml");
            System.out.println("      " + source);
            Primitives primitives = serializer.read(Primitives.class, source);
            dumpPrimitives(primitives);
        } catch (Exception e) {
            e.printStackTrace(System.out);
        }
    }

    private static Primitives generatePrimitives() {
        Primitives primitives = new Primitives();
        primitives.booleanValue = true;
        primitives.booleanArray = new boolean[] { false, true, };
        primitives.byteValue = 64;
        primitives.byteArray = new byte[] { 96, 64, 32, };
        primitives.shortValue = 16384;
        primitives.shortArray = new short[] { 24576, 16384, 8192, };
        primitives.intValue = 1073741824;
        primitives.intArray = new int[] { 1610612736, 1073741824, 536870912, };
        primitives.longValue = 4611686018427387904L;
        primitives.longArray = new long[] { 6917529027641081856L, 4611686018427387904L, 2305843009213693952L, };
        primitives.charValue = '@';
        primitives.charArray = new char[] { '`', '@', ' ', };
        primitives.floatValue = 1.7014117E38F;
        primitives.floatArray = new float[] { 2.5521175E38F, 1.7014117E38F, 8.5070587E37F, };
        primitives.doubleValue = 8.988465674311579E307;
        primitives.doubleArray = new double[] { 1.3482698511467367E308, 8.988465674311579E307, 4.4942328371557893E307, };

        return primitives;
    }

    private static void dumpPrimitives(Primitives primitives) {
        System.out.println("    booleanValue:       " + primitives.booleanValue);
        System.out.println("    booleanArray:       " + Arrays.toString(primitives.booleanArray));
        System.out.println("    byteValue:          " + primitives.byteValue);
        System.out.println("    byteArray:          " + Arrays.toString(primitives.byteArray));
        System.out.println("    shortValue:         " + primitives.shortValue);
        System.out.println("    shortArray:         " + Arrays.toString(primitives.shortArray));
        System.out.println("    intValue:           " + primitives.intValue);
        System.out.println("    intArray:           " + Arrays.toString(primitives.intArray));
        System.out.println("    longValue:          " + primitives.longValue);
        System.out.println("    longArray:          " + Arrays.toString(primitives.longArray));
        System.out.println("    charValue:          " + primitives.charValue);
        System.out.println("    charArray:          " + Arrays.toString(primitives.charArray));
        System.out.println("    floatValue:         " + primitives.floatValue);
        System.out.println("    floatArray:         " + Arrays.toString(primitives.floatArray));
        System.out.println("    doubleValue:        " + primitives.doubleValue);
        System.out.println("    doubleArray:        " + Arrays.toString(primitives.doubleArray));
    }

}

セットしている値には特に意味はない。(単に、各プリミティブ型の値のMAX_VALUE(charはASCIIの最大値である127)の { 3 / 4, 1 / 2, 1 / 4, } というだけのこと。
で、これを実行すると、次のような出力が得られる。

=== serialize ===
      data\primitives.xml
=== deserialize ===
      data\primitives.xml
    booleanValue:       true
    booleanArray:       [false, true]
    byteValue:          64
    byteArray:          [96, 64, 32]
    shortValue:         16384
    shortArray:         [24576, 16384, 8192]
    intValue:           1073741824
    intArray:           [1610612736, 1073741824, 536870912]
    longValue:          4611686018427387904
    longArray:          [6917529027641081856, 4611686018427387904, 2305843009213693952]
    charValue:          @
    charArray:          [`, @,  ]
    floatValue:         1.7014117E38
    floatArray:         [2.5521175E38, 1.7014117E38, 8.5070587E37]
    doubleValue:        8.988465674311579E307
    doubleArray:        [1.3482698511467367E308, 8.988465674311579E307, 4.4942328371557893E307]

配列のデータがどのようにシリアライズされているかを確認するために、生成されたファイル primitives.xml を見てみる。

<primitives>
   <booleanValue>true</booleanValue>
   <booleanArray length="2">false, true</booleanArray>
   <byteValue>64</byteValue>
   <byteArray length="3">96, 64, 32</byteArray>
   <shortValue>16384</shortValue>
   <shortArray length="3">24576, 16384, 8192</shortArray>
   <intValue>1073741824</intValue>
   <intArray length="3">1610612736, 1073741824, 536870912</intArray>
   <longValue>4611686018427387904</longValue>
   <longArray length="3">6917529027641081856, 4611686018427387904, 2305843009213693952</longArray>
   <charValue>@</charValue>
   <charArray length="3">`@ </charArray>
   <floatValue>1.7014117E38</floatValue>
   <floatArray length="3">2.5521175E38, 1.7014117E38, 8.5070587E37</floatArray>
   <doubleValue>8.988465674311579E307</doubleValue>
   <doubleArray length="3">1.3482698511467367E308, 8.988465674311579E307, 4.4942328371557893E307</doubleArray>
</primitives>

ほとんどの型は各値がカンマで区切られた形となっている。一方、char型はカンマ自体を値として含む可能性があるのでカンマ区切りとはせず、単に配列の各要素の文字が並んでいるだけになっている。(たぶん、デシリアライズする際には、String.toCharArray()の結果をフィールドにセットしているのだろう。)