Skip to content
This repository has been archived by the owner on Nov 7, 2020. It is now read-only.

Commit

Permalink
defined an annotation to control Mapper.serializeAs
Browse files Browse the repository at this point in the history
  • Loading branch information
kohsuke committed Oct 9, 2010
1 parent 98c11b0 commit 935480a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.thoughtworks.xstream.annotations;

import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.*;

/**
* Designates that the type and its derived types will serialize
* as the specified type. This is useful if you want an entire
* family of types serialize as the base type with its special converter.
*
* @author Kohsuke Kawaguchi
*/
@Retention(RUNTIME)
@Target(TYPE)
@Inherited
public @interface XStreamSerializeAs {
/**
* Use {@code void.class} to cancel out the annotation defined in the super type.
*/
Class<?> value();
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.thoughtworks.xstream.annotations.XStreamImplicitCollection;
import com.thoughtworks.xstream.annotations.XStreamInclude;
import com.thoughtworks.xstream.annotations.XStreamOmitField;
import com.thoughtworks.xstream.annotations.XStreamSerializeAs;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.ConverterMatcher;
import com.thoughtworks.xstream.converters.ConverterRegistry;
Expand Down Expand Up @@ -68,6 +69,8 @@ public class AnnotationMapper extends MapperWrapper implements AnnotationConfigu
private final Map<Class<?>, Converter> converterCache = new HashMap<Class<?>, Converter>();
private final Set<Class<?>> annotatedTypes = new WeakHashSet<Class<?>>();

private final Map<Class,String> serializedClass = new WeakHashMap<Class, String>();

/**
* Construct an AnnotationMapper.
*
Expand Down Expand Up @@ -104,6 +107,8 @@ public String serializedClass(final Class type) {
if (!locked) {
processAnnotations(type);
}
String name = serializedClass.get(type);
if (name!=null) return name;
return super.serializedClass(type);
}

Expand Down Expand Up @@ -167,6 +172,10 @@ private void processTypes(final Set<Class<?>> types) {
continue;
}

XStreamSerializeAs a = type.getAnnotation(XStreamSerializeAs.class);
if (a!=null && a.value()!=void.class)
serializedClass.put(type,a.value().getName());

addParametrizedTypes(type, types);

processConverterAnnotations(type);
Expand Down

0 comments on commit 935480a

Please sign in to comment.