Re: [Simple-support] Beginner Question
Brought to you by:
niallg
|
From: Graham S. <gr...@mo...> - 2008-06-10 21:23:31
|
Thanks for the quick reply Niall. Unfortunately it seems that the class attribute is not ignored by the deserializer. This works fine: TaskInterface o = serializer.read(TaskInterface.class, "<task1 class=\"com.company.om.tasks.Task1\" a=\"14\" b=\"13\"/>"); But if I omit the class attribute then this: TaskInterface o = serializer.read(TaskInterface.class, "<task1 a=\"14\" b=\"13\"/>"); will fail with Exception in thread "main" org.simpleframework.xml.load.InstantiationException: Cannot instantiate interface com.company.om.tasks.TaskInterface at org.simpleframework.xml.load.ObjectFactory.getInstance(ObjectFactory.java:67) at org.simpleframework.xml.load.Composite.read(Composite.java:112) at org.simpleframework.xml.load.Traverser.read(Traverser.java:71) at org.simpleframework.xml.load.Persister.read(Persister.java:392) at org.simpleframework.xml.load.Persister.read(Persister.java:374) at org.simpleframework.xml.load.Persister.read(Persister.java:355) at org.simpleframework.xml.load.Persister.read(Persister.java:337) at org.simpleframework.xml.load.Persister.read(Persister.java:258) at com.somispo.processor.main.Main.main(Main.java:69) I think that makes sense. If i've got a number of concrete classes that implement the same interface, how would the deserializer know which one to use without a fully qualified class name in the xml? In any event the real problem I have is getting the serializer to include the classname when i persist the object regards, Graham On Tue, Jun 10, 2008 at 3:01 PM, Niall Gallagher <gal...@ya...> wrote: > Hi, > > The class attribute is a special attribute that is ignored by the deserialization. Can you use object.getClass().getName() ? > > Also you dont need to cast, the following. > > TaskInterface o = (TaskInterface) serializer.read(TaskInterface.class, > serializedText); > > works as: > > TaskInterface o = serializer.read(TaskInterface.class, > serializedText); > > Its casted by the class type TaskInterface.class, the return type is autocasted at compile time. > > Hope this helps, > Niall > > > --- On Tue, 6/10/08, Graham Stewart <gr...@mo...> wrote: > >> From: Graham Stewart <gr...@mo...> >> Subject: [Simple-support] Beginner Question >> To: sim...@li... >> Date: Tuesday, June 10, 2008, 12:36 PM >> First let me say that simple looks great - it's >> refreshing to find a >> java serialization tool that works without 5 megs of >> dependencies! >> >> >> I'm trying to use Simple in a situation very similar to >> this tutorial example >> >> http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php#override >> >> I have two classes that implement TaskInterface, Task1 and >> Task2 and >> i'd like to be able to serialize the TaskInterface and >> get usable XML. >> >> Putting the class name in the xml seems to work and both of >> these >> deserialize fine: >> >> <task1 class="com.somispo.om.tasks.Task1" >> a="14" b="13"/> >> <task2 class="com.somispo.om.tasks.Task2" >> c="15" d="16"/> >> >> TaskInterface o = (TaskInterface) >> serializer.read(TaskInterface.class, >> serializedText); >> >> >> What I can't figure out is how to get that class >> attribute in the xml >> output from the Persister? It seems like it should be >> straightforward, >> but I can't get it to show when I try to persist a >> TaskInterface. >> >> thanks, >> Graham >> >> ------------------------------------------------------------------------- >> Check out the new SourceForge.net Marketplace. >> It's the best place to buy or sell services for >> just about anything Open Source. >> http://sourceforge.net/services/buy/index.php >> _______________________________________________ >> Simple-support mailing list >> Sim...@li... >> https://lists.sourceforge.net/lists/listinfo/simple-support > > > > |