Disable id reference for Class<?> fields

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Disable id reference for Class<?> fields

Geoffrey De Smet
I am using Xstream instance with ID_REFERENCES (because need those
further down the xml file).

I read an XML format like this:

<benchmark>
   <solver>
     <entityClass>org.foo.Bar</entityclass>
   </solver>
   <solver>
     <entityClass>org.foo.Bar</entityclass>
   </solver>
<benchmark>

When I try to write it, I get different output:

<benchmark>
   <solver>
     <entityClass id="7">org.foo.Bar</entityclass>
   </solver>
   <solver>
     <entityClass ref="7"/>
   </solver>
<benchmark>

My model looks like this:

@XstreamAlias("solver")
class Solver {

     @XstreamAlias("entityClass")
     Class<?> entityClass;

}

How do I make my write format the same as my read format?


==========
Second question (more advanced form)
==========

Actually, my entity model looks like this:

@XstreamAlias("solver")
class Solver {

     @XStreamImplicit(itemFieldName = "entityClass")
     protected List<Class<?>> entityClassList;

}

The XML input is the same.
During XML output it throws "ReferencedImplicitElementException: Cannot
reference implicit element"
because it uses refs for the Class fields.
I 'd like it not to use id-references for those fields to avoid that
exception.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Disable id reference for Class<?> fields

Jörg Schaible-2
Hi Geoffrey,

Geoffrey De Smet wrote:

> I am using Xstream instance with ID_REFERENCES (because need those
> further down the xml file).
>
> I read an XML format like this:
>
> <benchmark>
>    <solver>
>      <entityClass>org.foo.Bar</entityclass>
>    </solver>
>    <solver>
>      <entityClass>org.foo.Bar</entityclass>
>    </solver>
> <benchmark>
>
> When I try to write it, I get different output:
>
> <benchmark>
>    <solver>
>      <entityClass id="7">org.foo.Bar</entityclass>
>    </solver>
>    <solver>
>      <entityClass ref="7"/>
>    </solver>
> <benchmark>
>
> My model looks like this:
>
> @XstreamAlias("solver")
> class Solver {
>
>      @XstreamAlias("entityClass")
>      Class<?> entityClass;
>
> }
>
> How do I make my write format the same as my read format?

XStream adds the id attribute to any element in this mode, unless the
instance's type has been declared as immutable. Actually this is possible
for type "Class", maybe XStream should do it for Class by default anyway:

  xstream.addImmutableType(Class.class);


> ==========
> Second question (more advanced form)
> ==========
>
> Actually, my entity model looks like this:
>
> @XstreamAlias("solver")
> class Solver {
>
>      @XStreamImplicit(itemFieldName = "entityClass")
>      protected List<Class<?>> entityClassList;
>
> }
>
> The XML input is the same.
> During XML output it throws "ReferencedImplicitElementException: Cannot
> reference implicit element"
> because it uses refs for the Class fields.

No, it is thrown because something references the entire list, which has no
representation in XML.

> I 'd like it not to use id-references for those fields to avoid that
> exception.

Will not make a difference.

Cheers,
Jörg


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Disable id reference for Class<?> fields

Geoffrey De Smet
> No, it is thrown because something references the entire list, which has no
> representation in XML.

Confirmed. Thanks!

Not sure why I didn't grok the error message,
as it clearly outputs the [] of the List.toString().

Suggestion: It might help to improve the error message from
   "Cannot reference implicit element"
to
   "The same instance used twice and at least once as implicit element
collection: cannot reference implicit element collection"
so the keywords "twice" and "collection" might make it dawn on people
like me :)


On 11-01-14 15:44, Jörg Schaible wrote:

> Hi Geoffrey,
>
> Geoffrey De Smet wrote:
>
>> I am using Xstream instance with ID_REFERENCES (because need those
>> further down the xml file).
>>
>> I read an XML format like this:
>>
>> <benchmark>
>>     <solver>
>>       <entityClass>org.foo.Bar</entityclass>
>>     </solver>
>>     <solver>
>>       <entityClass>org.foo.Bar</entityclass>
>>     </solver>
>> <benchmark>
>>
>> When I try to write it, I get different output:
>>
>> <benchmark>
>>     <solver>
>>       <entityClass id="7">org.foo.Bar</entityclass>
>>     </solver>
>>     <solver>
>>       <entityClass ref="7"/>
>>     </solver>
>> <benchmark>
>>
>> My model looks like this:
>>
>> @XstreamAlias("solver")
>> class Solver {
>>
>>       @XstreamAlias("entityClass")
>>       Class<?> entityClass;
>>
>> }
>>
>> How do I make my write format the same as my read format?
> XStream adds the id attribute to any element in this mode, unless the
> instance's type has been declared as immutable. Actually this is possible
> for type "Class", maybe XStream should do it for Class by default anyway:
>
>    xstream.addImmutableType(Class.class);
>
>
>> ==========
>> Second question (more advanced form)
>> ==========
>>
>> Actually, my entity model looks like this:
>>
>> @XstreamAlias("solver")
>> class Solver {
>>
>>       @XStreamImplicit(itemFieldName = "entityClass")
>>       protected List<Class<?>> entityClassList;
>>
>> }
>>
>> The XML input is the same.
>> During XML output it throws "ReferencedImplicitElementException: Cannot
>> reference implicit element"
>> because it uses refs for the Class fields.
> No, it is thrown because something references the entire list, which has no
> representation in XML.
>
>> I 'd like it not to use id-references for those fields to avoid that
>> exception.
> Will not make a difference.
>
> Cheers,
> Jörg
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>      http://xircles.codehaus.org/manage_email
>
>
>



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Disable id reference for Class<?> fields

Jörg Schaible-2
Hi Geoffrey,

Geoffrey De Smet wrote:

>> No, it is thrown because something references the entire list, which has
>> no representation in XML.
>
> Confirmed. Thanks!
>
> Not sure why I didn't grok the error message,
> as it clearly outputs the [] of the List.toString().
>
> Suggestion: It might help to improve the error message from
>    "Cannot reference implicit element"
> to
>    "The same instance used twice and at least once as implicit element
> collection: cannot reference implicit element collection"
> so the keywords "twice" and "collection" might make it dawn on people
> like me :)

The algorithm just detects that the referenced object is missing, but not
why. There are more possibilities why an element is implicit.

- Jörg


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email