【发布时间】:2012-02-15 01:57:16
【问题描述】:
我有一堂课MyClass。如果我在不实现自定义转换器的情况下对其进行序列化,则它是不可读的。
我实现了MyClassDTO 以及MyClass 和MyClassDTO 之间的转换。
MyClassDTO 在使用 XStream 标准序列化时是人类可读的。
我想写 XStream Converter 序列化和反序列化MyClass。Converter.marshal 的实现应该如下:将 MyClass 对象转换为 MyClassDTO 1 并为 MyClassDTO 调用默认序列化。
对于Converter.unmarshal :调用MyClassDTO 对象的默认反序列化并将其转换为MyClass。
如何以简单的方式实现这样的行为?
我浏览了XStream Converter Tutorial,但没有找到我需要的东西。
我需要填写下面的存根:
class MatrixConverter<T> : Converter
where T : new()
{
public bool CanConvert(Type type)
{
return type == typeof(Matrix<T>);
}
public void ToXml(object value, Type expectedType, XStreamWriter writer, MarshallingContext context)
{
Matrix<T> matrix = value as Matrix<T>;
if (matrix == null)
{
throw new ArgumentException();
}
// the code which I am asked about should follow here
}
public object FromXml(Type expectedType, XStreamReader reader, UnmarshallingContext context)
{
Matrix<T> matrix = null;
// the code which I am asked about should follow here
}
}
【问题讨论】:
-
你能把你的程序和战术贴出来吗...
-
哪个程序和哪个策略?
-
我的意思是你应该发布你正在尝试的代码示例......
-
我添加了有问题的存根,希望它能让我的问题更清楚。谢谢。
标签: java xml xml-serialization xstream