Re: [Simple-support] Asymmetry in Persister behaviour
Brought to you by:
niallg
|
From: Pete C. <pr...@ca...> - 2011-01-12 10:23:14
|
Hi, Yep ok. I should have thought of that, thank you. And there's no way to make them consistent without the child node? As is, "@Element Key<Foo> foo" is going to end up looking like: <foo><foo>...</foo></foo> If the only way is by making some changes, then I'm happy to make them if you think this is worthwhile. I don't really know my way around the code, but I think the following would do it: - Stop Traverser.write(OutputNode, Object, Class) from adding the child node itself (i.e. rather than working out a name for a child, don't add a child). - Add an argument "boolean createChild" to Persister.write(Object, OutputNode, ...) methods. Then in Persister.write(Object, OutputNode, Context, boolean) add the child as before if it is set. - Add a new method Persister.write(Object, OutputNode) that defaults createChild to true to make this backwards compatible (although I would argue that the default should actually be false and that write(Object, Writer) should set it to true). The potential problem is that I don't know whether Traverser.write(OutputNode, Object, Class) is used elsewhere, or just Traverser.write(OutputNode, Object, Class, String). Thanks Pete Niall Gallagher wrote: > Hi, > > Take a look at the following, it works fine. > > https://simple.svn.sourceforge.net/svnroot/simple/trunk/download/stream/src/test/java/org/simpleframework/xml/convert/WrapperTest.java > > Niall > > --- On Tue, 11/1/11, Pete Calvert <pr...@ca...> wrote: > >> From: Pete Calvert <pr...@ca...> >> Subject: [Simple-support] Asymmetry in Persister behaviour >> To: sim...@li... >> Received: Tuesday, 11 January, 2011, 4:25 PM >> 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")); >> } >> } >> >> ------------------------------------------------------------------------------ >> Protect Your Site and Customers from Malware Attacks >> Learn about various malware tactics and how to avoid them. >> Understand >> malware threats, the impact they can have on your business, >> and how you >> can protect your company and customers by using code >> signing. >> http://p.sf.net/sfu/oracle-sfdevnl >> _______________________________________________ >> Simple-support mailing list >> Sim...@li... >> https://lists.sourceforge.net/lists/listinfo/simple-support >> > > > |