私は単純な XML シリアライゼーションを使用することに決めましたが、基本的な問題で立ち往生していました。java.util.UUID
この小さなクラスの final フィールドとしてクラス インスタンスをシリアル化しようとしています。
@Root
public class Identity {
@Attribute
private final UUID id;
public Identity(@Attribute UUID id) {
this.id = id;
}
}
チュートリアルでは、次のようにコンバーターを登録してサードパーティ オブジェクトをシリアル化する方法を示します。
Registry registry = new Registry();
registry.bind(UUID.class, UUIDConverter.class);
Strategy strategy = new RegistryStrategy(registry);
Serializer serializer = new Persister(strategy);
serializer.write( object, stream );
UUID の適切なコンバーターは非常に単純です。
public class UUIDConverter implements Converter<UUID> {
@Override
public UUID read(InputNode node) throws Exception {
return new UUID.fromString(node.getValue());
}
@Override
public void write(OutputNode node, UUID value) throws Exception {
node.setValue(value.toString());
}
}
しかし、この単純なコードは私にとってはうまくいきませんでした.UUIDフィールドを持つオブジェクトのシリアル化中に例外Transform of class java.util.UUID not supportedがスローされました。
私は自分のために働くカスタムMatcher
(チュートリアルにはありませんでした)と似たようなことを試しました:
Serializer serializer = new Persister(new MyMatcher());
serializer.write( object, stream );
クラスは次のようになりMatcher
ます。
public static class MyMatcher implements Matcher {
@Override
@SuppressWarnings("unchecked")
public Transform match(Class type) throws Exception {
if (type.equals(UUID.class))
return new UUIDTransform();
return null;
}
}
public class UUIDTransform implements Transform<UUID> {
@Override
public UUID read(String value) throws Exception {
return UUID.fromString(value);
}
@Override
public String write(UUID value) throws Exception {
return value.toString();
}
}
質問:
- カスタムMatcherは、サードパーティクラスをストリーミングするために常に推奨される方法ですか?
- どのような場合に Converter を使用できますか?
- Simple XML のより良いチュートリアル/例はありますか?
ありがとうございました。