NamedMapConverter not using my valueType #36

Closed
nitroamos opened this Issue Jan 14, 2016 · 3 comments

Comments

Projects
None yet
2 participants
@nitroamos

I'm registering a NamedMapConverter for my HashMap like this:

xstream.registerConverter(new MyValueConverter());
xstream.registerConverter(new NamedMapConverter(xstream.getMapper(),
                "mol", "i", Long.class, null, MyValue.class,
                true, false, xstream.getConverterLookup()){
            public boolean canConvert(Class type){
                return type.equals(MoleculeCache.class);
            }
        });

where MyValueConverter is implements SingleValueConverter. Using print statements, I can see MyValueConverter.toString being called, but MyValueConverter.fromString is not when I'm deserializing. If I switch to this type of NamedMapConverter, then my MyValueConverter.fromString is being called:

new NamedMapConverter(xstream.getMapper(), null, "key", String.class, "value", MyValue.class)

Am I doing something wrong?

@joehni

This comment has been minimized.

Show comment
Hide comment
@joehni

joehni Jan 15, 2016

Member

Your code looks OK on first sight. Can you show the implementation of MyValueConverter.canConvert? How does the generated XML look like for a MoleculeCache?

Member

joehni commented Jan 15, 2016

Your code looks OK on first sight. Can you show the implementation of MyValueConverter.canConvert? How does the generated XML look like for a MoleculeCache?

@joehni joehni self-assigned this Jan 15, 2016

@joehni joehni added the question label Jan 15, 2016

@nitroamos

This comment has been minimized.

Show comment
Hide comment
@nitroamos

nitroamos Jan 15, 2016

My canConvert is really simple:

    xstream.registerConverter(new SingleValueConverter(){

        @Override
        public boolean canConvert(Class arg0) {
            return arg0.equals(MyValue.class);
        }

        @Override
        public Object fromString(String arg0) {
            //not called
            return new MyValue(arg0);
        }

        @Override
        public String toString(Object arg0) {  
            //called
            return ((MyValue)arg0).stringRepresentation();
        }
    });

It doesn't seem to matter if I use an anonymous class for this converter.
and the output looks like this (as expected):

  <molecules>
    <mol i="0">string0</mol>
    <mol i="1">string1</mol>
    <mol i="2">string2</mol>
    <mol i="3">string3</mol>
    <mol i="4">string4</mol>
  </molecules>

I think the text "molecules" is coming from the name of the field containing the MoleculeCache:

public class MoleculeCache extends HashMap<Long, MyValue>

My canConvert is really simple:

    xstream.registerConverter(new SingleValueConverter(){

        @Override
        public boolean canConvert(Class arg0) {
            return arg0.equals(MyValue.class);
        }

        @Override
        public Object fromString(String arg0) {
            //not called
            return new MyValue(arg0);
        }

        @Override
        public String toString(Object arg0) {  
            //called
            return ((MyValue)arg0).stringRepresentation();
        }
    });

It doesn't seem to matter if I use an anonymous class for this converter.
and the output looks like this (as expected):

  <molecules>
    <mol i="0">string0</mol>
    <mol i="1">string1</mol>
    <mol i="2">string2</mol>
    <mol i="3">string3</mol>
    <mol i="4">string4</mol>
  </molecules>

I think the text "molecules" is coming from the name of the field containing the MoleculeCache:

public class MoleculeCache extends HashMap<Long, MyValue>

@joehni joehni added bug and removed question labels Feb 5, 2016

@joehni joehni added this to the 1.4.x milestone Feb 5, 2016

@joehni

This comment has been minimized.

Show comment
Hide comment
@joehni

joehni Feb 5, 2016

Member

Sorry for coming back so late. However, I can confirm that this is simply a bug. Our unit test uses String objects as values and therefore missed the error.

Member

joehni commented Feb 5, 2016

Sorry for coming back so late. However, I can confirm that this is simply a bug. Our unit test uses String objects as values and therefore missed the error.

joehni added a commit that referenced this issue Feb 8, 2016

@joehni joehni closed this in 60e683a Feb 8, 2016

@joehni joehni modified the milestones: 1.4.9, 1.4.x Mar 16, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment