MOXy adds type and uses toString?

筅森魡賤 提交于 2019-12-12 06:40:53

问题


Disclaimer: I'm new to all this, so my terminology may be wrong

I've got some Java POJO's I want to serialize to JSON & XML. I'm using MOXy 2.5.0 for JSON and Jersey 2.4.1.

@XmlRootElement
class Root {
//    @XmlElements({@XmlElement(name = "destination_address", type = LatLong.class),
//                  @XmlElement(name = "destination_address", type = Polygon.class)})
    public Object[]  destination_addresses;
}
public class LatLong {
    public double lat, lng;
}
public class Polygon {
    protected List<LatLong> points = new ArrayList<LatLong>();
    @XmlElements({@XmlElement(name = "lat", type = Lat.class),
                  @XmlElement(name = "lng", type = Lng.class)})
    private LatOrLong[] getLatOrLongs() {
        LatOrLong[] retval = new LatOrLong[points.size() * 2];
        for (int point = 0; point < points.size(); point++) {
            LatLong latLong = points.get(point);
            retval[point * 2] = new Lat(latLong.lat);
            retval[point * 2 + 1] = new Lng(latLong.lng);
        }
        return retval;
    }
    static abstract private class LatOrLong {
        @XmlValue
        private double latOrLong;
        private LatOrLong() {}
        private LatOrLong(double latOrLong) {this.latOrLong = latOrLong;}
    }
    static private class Lat extends LatOrLong {
        private Lat() {}
        private Lat(double lat) {super(lat);}
    }
    static private class Lng extends LatOrLong {
        private Lng() {}
        private Lng(double lng) {super(lng);}
    }
}

This doesn't work in XML with the two lines commented out, but in JSON, MOXy is adding a type: latLong attribute to the destination_addresses array, as well as using the toString() method of Polygon.

  1. How can I hide the type?
  2. How can I get MOXy to use getLatOrLongs() instead of toString()?

EDIT: I've simplified Polygon to just serialize points and changed destination_addresses to be a List<Object> instead of Object[] .


回答1:


The pojo mapping feature is by default enabled using MOXy.

But if for any case, you need to implement a specific marshal/unmarshal (I take here ObjectID from MongoDB which is a t:

import javax.xml.bind.annotation.adapters.XmlAdapter;
import org.bson.types.ObjectId;

public class ObjectIdXmlAdapter extends XmlAdapter<String, ObjectId> {

  @Override
  public String marshal(ObjectId id) throws Exception {
    if(id == null) {
        return null;
    } else {
        return id.toString();
    }
  }

  @Override
  public ObjectId unmarshal(String id) throws Exception {
    return new ObjectId(id);
  }

}

And then, on your POJO:

@XmlJavaTypeAdapter(ObjectIdXmlAdapter.class)
public ObjectId getId() {
    return id;
}

Will serialize your id element as expected...

Hope this helps, this should be the main trouble.



来源:https://stackoverflow.com/questions/20395423/moxy-adds-type-and-uses-tostring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!