java - XStream: how to marshal/unmarshal lists in a custom converter? -


I have the following class (not legacy, non-usable) that is sorted with a custom converter:

  class tests {// some other variable list & lt; SomeType & gt; SomeTypeList; }  

A fine work converter for someType is already available. Although I want to make the list serial as it was annotated with @extrem alias ("some type").

Finally I expect the following format for some type list:

  & lt; Some types of class = "list-type" & gt; & Lt; SomeType & gt; .... & lt; / SomeType & gt; ... & lt; / SomeTypes & gt;  

How to apply martial / upper case method to get the desired output? Calling context.convert Some other (some type lists) did not return expected results because the surrounding & lt; SomeTypes & gt; The tag was unavailable.

You are wise to get the structure:

   

See the following code: You need to tag your list for:

  @XStreamImplicit (itemFieldName = "someType") list & lt; SomeType & gt; List;  

Now, depending on what's inside you, you may need to create a custom converter. To give this context, you can change a bit like this:

  @XStreamImplicit (itemFieldName = "someType") @ XStreamConverter (YourOwnConverter.class) list & lt; SomeType & gt; SomeTypeList;  

Then create a converter class ( YourOwnConverter ) which is how to find / Marshall:

  public boolean canConvert (class type ) {Return type.equals (SomeType.class); } Public Zero Marshal (object source, hierarchical serial author, Marshall Contact Contracts) {SomeType mytype = (SomeType) source; Author. AddAttribute ("status", mytype.getPosition ()); Writer.setValue (mytype.getId ()); } Public Objects unmarshal (Hierarchical Streamer Reader, Unmixed Contact Context) {SomeType mytype = new SomeType (); String position = ReaderGet attribute ("status"); ...... come back to my friends; }  

Use this as an example:


Comments