[Simple-support] Asymmetry in Persister behaviour
Brought to you by:
niallg
|
From: Pete C. <pr...@ca...> - 2011-01-12 00:25:52
|
Hi, I've noticed that the behaviour of write(Object, OutputNode) and read(Class, InputNode) is not entirely consistent. read loads directly from the node whereas write first creates a new child (using the name of the object's class) and then writes to this. Is there a way of working around this? i.e. write writing directly to the node given? The reason I need this is that I'm trying to import/export Objectify entities. References to other entities are stored as "Key<T>", so I've got a Converter for "Key" that 'skips' over the reference to export the entity in its place. I've included some code at the end of the email that exhibits the behaviour I'm talking about. Thanks Pete --- import org.simpleframework.xml.*; import org.simpleframework.xml.strategy.*; import org.simpleframework.xml.convert.*; import org.simpleframework.xml.core.*; import org.simpleframework.xml.stream.*; import java.io.*; public class Test { @Root public static class Example { @Element Foo x; } public static class Foo { String val; public Foo(String x) { val = x; } } public static class FooConverter implements Converter<Foo> { public Foo read(InputNode node) throws Exception { return new Foo(new Persister().read(String.class, node)); } public void write(OutputNode node, Foo f) throws Exception { new Persister().write(f.val, node); } } public static void main(String[] args) throws Exception { Registry registry = new Registry(); Strategy strategy = new RegistryStrategy(registry); Serializer serializer = new Persister(strategy); registry.bind(Foo.class, FooConverter.class); Example bla = new Example(); bla.x = new Foo("test"); serializer.write(bla, new File("example.xml")); serializer.read(Example.class, new File("example.xml")); } } |