Re: [Simple-support] Beginner Question
Brought to you by:
niallg
|
From: Graham S. <gr...@mo...> - 2008-06-13 23:13:11
|
Thanks Niall,
That worked well.
Unfortunately the DefaultStrategy references a number of non-public
classes so i started out having to copy things like ArrayType and
ClassType into my own project space.
In the end I went ahead and changed DefaultStrategy to be a public
class so I could make my custom Strategy extend it. This makes my own
project code much neater at the expense of having a custom version of
the simple jar.
Is there a rationale for keeping DefaultStrategy private?
regards,
Graham
On Thu, Jun 12, 2008 at 11:25 AM, Niall Gallagher
<gal...@ya...> wrote:
> Hi,
>
> Well the issue exists only for the root class. The root element does not use the class attribute by default. So if you had:
>
> @Blah
> public class Blah {
> @Element
> private TaskInterface task;
> }
>
> Then serialization would have:
>
> <blah>
> <task class="com.package.MyTask" etc.....>
> etc....
> </task>
> </blah>
>
> However because you want the root element to contain the class here is what you can do. If you have the source code to simple, which is available in the download you will see the DefaultStrategy.java object in the package org/simpleframework/http/load package. You need to take this source code to build your own strategy. In your strategy you would re-implement the setRoot method to look like this.
>
> public boolean setElement(Class field, Object value, NodeMap node, Map map){
> boolean result = setElement(field, value node, map);
>
> node.put(label, value.getClass().getName());
> return result;
> }
>
> This will ensure that each time you write an object, the root element will always use the class= attribute. Or course you can be more intelligent than this, and decide when you want the class= attribute and when you do not.
>
> Hope this helps,
> Niall
>
> --- On Tue, 6/10/08, Graham Stewart <gr...@mo...> wrote:
>
>> From: Graham Stewart <gr...@mo...>
>> Subject: Re: [Simple-support] Beginner Question
>> To: "Timo Rumland" <cr...@ol...>
>> Cc: sim...@li...
>> Date: Tuesday, June 10, 2008, 4:43 PM
>> On Tue, Jun 10, 2008 at 3:59 PM, Timo Rumland
>> <cr...@ol...> wrote:
>> > Hello Graham,
>> >
>> >> But if I omit the class attribute then this:
>> >
>> >> TaskInterface o =
>> serializer.read(TaskInterface.class, "<task1
>> >> a=\"14\"
>> b=\"13\"/>>");
>> >> will fail with
>> >
>> > you want to get a TaskInterface, which is apparently
>> an interface,
>> > returned by the serializer. This is possible only if
>> the serializer
>> > knows what implementation to instantiate. If you have
>> no "class"
>> > parameter in the XML, how can the serializer know
>> which class to
>> > instantiate (you can't instantiate an interface)?
>>
>> Right, i entirely expect to have to provide the class
>> attribute when
>> i'm deserializing to an interface.
>>
>> How do i get the serializer to put it there?
>>
>> If i have
>>
>> TaskInterface myTask = new Task1(14,13);
>> myPersister.write(myTask, "output.xml");
>>
>> then it writes out the xml without the class attribute, so
>> I end up with:
>>
>> <task1 a="14" b="13"/>
>>
>> instead of what I really want, which is:
>>
>> <task1 class="com.company.om.tasks.Task1"
>> a="14" b="13"/>
>>
>>
>> Does that make sense?
>> How do I get Persister.write() to include the class
>> attribute?
>>
>>
>> thanks again,
>> 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
>
> __________________________________________________
> Do You Yahoo!?
> Tired of spam? Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com
>
|