Hi Jörg,
I found error in my code. Now I can skip fields which are new. It is ok.
Can I write skipped fields into log file, if I use override wrapMapper?

Best Regards
Petr
Hi Jörg,
thanks for answer.
Dne 20.9.2014 v 22:46 Jörg Schaible napsal(a):
Hi Petr,

Petr Adamec wrote:

Good morning,
I have a problem with convert from xml to data (unmarshall) when I use
xml which has unknown tags. Xstream write me Exception in thread "main"

com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$UnknownFieldException:
No such field.
This is the expected behavior.

Ok. I understand.
In the general case it is ok. I know if I serialize one class and deserialize to other class (or the same but older), it is a problem. My case is that I serialize newer class and deserialize to the older (class). I know that it is non-standard situation.


I was looking for solution for my problem on internet. I find some it
but all of them do not working on my case.

I override method wrapMapper but it still no working.

XStream xstream = new XStream() {

              // to enable ignoring of unknown elements

              @Override

              protected MapperWrapper wrapMapper(MapperWrapper next) {

                  return new MapperWrapper(next) {

                      @Override

public boolean shouldSerializeMember(Class definedIn,
                      String fieldName) {

                          if (definedIn == Object.class) {

                              try {

return this.realClass(fieldName) != null;

                              } catch (Exception e) {

                                  return false;

                              }

                          }

                          return super.shouldSerializeMember(definedIn,
                          fieldName);

                      }

                  };

              }

          };

Which version of XStream are you using?

I use version 1.4.7.

Is it maybe because I have a collection at class. But every options
which I try not works.

My class which I need convert to/from  is

public class Configurations implements Serializable{

private static final long serialVersionUID = 4392719277296957995L;

      private List<Class<? extends Updatable>> tableNames;

private Map<Class<? extends Updatable>, List<? extends Updatable>>
      tables;

      ...

      }


Updatable is only interface. I need  convert to/from xml every class
which implements this interface.

In normal case, when I has right xml (get from marshell data) it is ok.
I can convert data to this xml and xml back to data.

<mypackage.Configurations>

    <tableNames>

<java-class>mypackage.configuration.MyClass</java-class>

    </tableNames>

    <tables>

      <entry>

<java-class>mypackage.configuration.MyClass</java-class>

        <list>

          <mypackage.configuration.MyClass>

            <id>0</id>

            <created>2014-09-19 08:49:34.649 UTC</created>

            <active>true</active>

          </mypackage.configuration.MyClass>

        </list>

      </entry>

    </tables>

</mypackage.Configurations>


But when I change xml (forr example add new tag newTag)

<mypackage.Configurations>

    <tableNames>

<java-class>mypackage.configuration.MyClass</java-class>

    </tableNames>

    <tables>

      <entry>

<java-class>mypackage.configuration.MyClass</java-class>

        <list>

          <mypackage.configuration.MyClass>

            <id>0</id>

            <created>2014-09-19 08:49:34.649 UTC</created>

            <active>true</active>

            <newTag>exam</newTag>

          </mypackage.configuration.MyClass>

        </list>

      </entry>

    </tables>

</mypackage.Configurations>


Xstream ends with error
AbstractReflectionConverter$UnknownFieldException: No such field. Is
exists solution how can I solve this problem? I need convert to data
(class) and if xml has unknown tags which does not exist at class, logg
some message and skip tag.

Ok. I read faq.
I understand from http://xstream.codehaus.org/faq.html#Serialization_newer_class_versions that If I have a new version class which has one or more fields newer that when I serialize it to xml and deserialize it to older version where new fields is missing, field will be uninitialized. It is ok. But in this case (the first case from http://xstream.codehaus.org/faq.html#Serialization_newer_class_versions ) no exception should be throwed, if i undestand right.

See http://xstream.codehaus.org/faq.html#Serialization_newer_class_versions

The topics also direct you to the proper location of the custom mapper from
above.

Cheers,
Jörg


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

     http://xircles.codehaus.org/manage_email



Best Regards
Petr



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

   http://xircles.codehaus.org/manage_email


Reply via email to