Samples JDK
Sample4SerializeXStream.java
1 package com.freemindcafe.serialization.sample4;
2 
3 import com.thoughtworks.xstream.XStream;
4 import com.thoughtworks.xstream.io.HierarchicalStreamDriver;
5 import com.thoughtworks.xstream.mapper.ClassAliasingMapper;
6 import com.thoughtworks.xstream.mapper.Mapper;
7 import com.thoughtworks.xstream.mapper.MapperWrapper;
8 
9 /**
10  * extends xstream and over rides mapper to use simple classname if the alias is not used.
11  * @author KANAG00R
12  *
13  */
14 public class Sample4SerializeXStream extends XStream
15 {
16 
17  Sample4SerializeXStream(HierarchicalStreamDriver hierarchicalStreamDriver)
18  {
19  super(hierarchicalStreamDriver);
20 
21  }
22 
23  @Override
24  protected MapperWrapper wrapMapper(MapperWrapper next) {
25  return new ClassMapper(next);
26  }
27 
28 }
29 
30 class ClassMapper extends ClassAliasingMapper
31 {
32 
33  public ClassMapper(Mapper wrapped) {
34  super(wrapped);
35  }
36 
37  @Override
38  public String serializedClass(Class clazz) {
39  if(clazz == null){
40  return "null";
41  }
42  String name = super.serializedClass(clazz);
43  if (clazz.getName().equals(name))
44  {
45  return clazz.getSimpleName();
46  } else {
47  return name;
48  }
49  }
50 }
51