com.thoughtworks.xstream.converters.basic.AbstractBasicConverter Maven / Gradle / Ivy
/*
* Copyright (C) 2003, 2004 Joe Walnes.
* Copyright (C) 2006, 2007 XStream Committers.
* All rights reserved.
*
* The software in this package is published under the terms of the BSD
* style license a copy of which has been included with this distribution in
* the LICENSE.txt file.
*
* Created on 26. September 2003 by Joe Walnes
*/
package com.thoughtworks.xstream.converters.basic;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
/**
* Base helper class for converters that can store the
* state of an object as a single String.
*
* Subclasses should implement the toString(Object) and
* fromString(String) methods for the conversion.
*
* @author Joe Walnes
* @deprecated Since 1.2 use {@link com.thoughtworks.xstream.converters.basic.AbstractSingleValueConverter}
*/
public abstract class AbstractBasicConverter implements Converter {
protected abstract Object fromString(String str);
public abstract boolean canConvert(Class type);
protected String toString(Object obj) {
return obj.toString();
}
public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
writer.setValue(toString(source));
}
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
return fromString(reader.getValue());
}
}