2012-02-15 3 views
0

나는 MyClass 클래스를 가지고 있습니다. 사용자 지정 변환기를 구현하지 않고 serialize하면 사람이 읽을 수 없습니다.중간 객체로 변환하여 쓰고 읽는 XStream 변환기를 작성하는 방법은 무엇입니까?

MyClassMyClassDTO 사이에서 변환을 구현했습니다.

MyClassDTO은 XStream 표준 직렬화를 사용할 때 사람이 읽을 수 있습니다.

XStream Converter 직렬화 및 비 직렬화 MyClass을 작성하고 싶습니다.
Converter.marshal에 대한 구현은 다음과 같아야합니다. MyClass 개체를 MyClassDTO으로 변환하고 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 

    } 
} 
+0

당신의 절차와 전술을 게시 할 수 있습니다 ... –

+0

어떤 절차와 전술? – sergtk

+0

나는 당신이 노력하고있는 코드 샘플을 게시해야한다는 것을 의미했다 ... –

답변

1

MatrixDTO m = new MatrixDTO(matrix); 

는 DTO에 내부 매트릭스 타입으로 변환 가정이보십시오.

public void ToXml(object value, Type expectedType, 
    XStreamWriter writer, MarshallingContext context) 
{ 
    context.convertAnother(new MatrixDTO(matrix)); 
} 

public Object FromXml(Type expectedType, 
    XStreamReader reader, UnmarshallingContext context) 
{ 
    return context.convertAnother(context.currentObject(), MatrixDTO.class); 
} 

언 마샬링의 경우 context.currentObject()에 수동으로 삽입해야 할 수 있습니다. 나 자신을 시도하지 않았다.

희망이 있습니다.

+0

이 맞는 것 같습니다. 그러나 이미 다른 방식으로 구현 되었기 때문에 시도하지 않았습니다. 감사. – sergtk

관련 문제