[xstream-user] Re: Inheritance with interface and their implementations but without class attribute

2012-10-25 Thread Jörg Schaible
Hi Marcin,

Stefaniuk Marcin wrote:

 I was searching for the answer through the list but haven't found
 anything.
 
 I have a class with an attribute of interface type.
 @XStreamAlias(given)
 public class Given {
@XStreamAsAttribute
public String name;
public Mock mock;
 }
 
 Interface is very simple:
 @XStreamInclude({ExactMock.class})
 public interface Mock {
void execute();
 }
 
 I have implementations of the interface:
 @XStreamAlias(exact-mock)
 public class ExactMock implements Mock {
@XStreamAsAttribute
String endpoint;
@XStreamAsAttribute
String name;
 
@Override
public void execute() {
  // TODO Auto-generated method stub
}
 }
 
 It is easy to marshal as:
 given that=My entry condition
mock type=exact-mock/
 /given
 
 Given the above class structure (type annotations are part of my
 experiments) I would like to receive below XML structure: given that=My
 entry condition
exact-mock endpoint=my-endpoint name=name-so-so /
 /given
 
 Important is to put aliases to element names but not any attribute. I have
 attempt to make own converter but I would like to change main element
 generation only. My implementations of interface would radically vary so
 rest of marshall / unmarshall I would like to delegate to XStream again.
 It should allow to write interface implementations without change of
 converter. Is it possible?

First you have to understand that the tag name is produced by the converter 
that handles the container i.e. in your case it is the converter (probably 
the ReflectionConverter) that handles Given. The ReflectionConverter uses 
the field names as tag names to assign the individual elements at 
deserialization time again to the appropriate field.

What you can do now is to derive a custom converter from the 
ReflectionConverter and declare the mock field to be omitted, handle it 
yourself in the custom converter and call super afterwards (see my example 
in the Hibernate thread some days ago).

Another approach is the usage of an implicit array here, but you would have 
to change Given:

 % ===
@XStreamAlias(given)
public class Given {
  @XStreamAsAttribute
  public String name;
  @XStreamImplicit
  public Mock[] mock;
}
 % ===

In this case the ReflectionProvider will associate any element that is not a 
field name, but represents a class (resp. an alias for a class) of type 
Mock with the array in the field mock - matching your XML exactly.

- Jörg


-
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email




RE: [xstream-user] Re: Inheritance with interface and their implementations but without class attribute

2012-10-25 Thread Stefaniuk Marcin
Jörg,
Many thanks. The latter approach is good enough for now and is in fact expected 
future requirement.

Best regards
Marcin Stefaniuk
Starszy programista


PKO Bank Polski
Departament Rozwoju Aplikacji Sprzedażowych i Wsparcia
Wydział Integracji Procesów i Usług
02-515 Warszawa, ul. Puławska 15
t: 22 580 30 39

Dbaj z nami o środowisko!
Nie drukuj tej wiadomości, ani innych dokumentów, jeśli nie jest to konieczne.


-Original Message-
From: Jörg Schaible [mailto:joerg.schai...@scalaris.com] 
Sent: Thursday, October 25, 2012 9:07 AM
To: user@xstream.codehaus.org
Subject: [xstream-user] Re: Inheritance with interface and their 
implementations but without class attribute

Hi Marcin,

Stefaniuk Marcin wrote:

 I was searching for the answer through the list but haven't found
 anything.
 
 I have a class with an attribute of interface type.
 @XStreamAlias(given)
 public class Given {
@XStreamAsAttribute
public String name;
public Mock mock;
 }
 
 Interface is very simple:
 @XStreamInclude({ExactMock.class})
 public interface Mock {
void execute();
 }
 
 I have implementations of the interface:
 @XStreamAlias(exact-mock)
 public class ExactMock implements Mock {
@XStreamAsAttribute
String endpoint;
@XStreamAsAttribute
String name;
 
@Override
public void execute() {
  // TODO Auto-generated method stub
}
 }
 
 It is easy to marshal as:
 given that=My entry condition
mock type=exact-mock/
 /given
 
 Given the above class structure (type annotations are part of my
 experiments) I would like to receive below XML structure: given that=My
 entry condition
exact-mock endpoint=my-endpoint name=name-so-so /
 /given
 
 Important is to put aliases to element names but not any attribute. I have
 attempt to make own converter but I would like to change main element
 generation only. My implementations of interface would radically vary so
 rest of marshall / unmarshall I would like to delegate to XStream again.
 It should allow to write interface implementations without change of
 converter. Is it possible?

First you have to understand that the tag name is produced by the converter 
that handles the container i.e. in your case it is the converter (probably 
the ReflectionConverter) that handles Given. The ReflectionConverter uses 
the field names as tag names to assign the individual elements at 
deserialization time again to the appropriate field.

What you can do now is to derive a custom converter from the 
ReflectionConverter and declare the mock field to be omitted, handle it 
yourself in the custom converter and call super afterwards (see my example 
in the Hibernate thread some days ago).

Another approach is the usage of an implicit array here, but you would have 
to change Given:

 % ===
@XStreamAlias(given)
public class Given {
  @XStreamAsAttribute
  public String name;
  @XStreamImplicit
  public Mock[] mock;
}
 % ===

In this case the ReflectionProvider will associate any element that is not a 
field name, but represents a class (resp. an alias for a class) of type 
Mock with the array in the field mock - matching your XML exactly.

- Jörg


-
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email