Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » MOXY JSON Serialization of Polymorphic List issue
MOXY JSON Serialization of Polymorphic List issue [message #1717810] Wed, 16 December 2015 20:39
Robert Patrick is currently offline Robert PatrickFriend
Messages: 1
Registered: December 2015
Junior Member
I am trying to test MOXY using an the following POJOs:

Company:
@XmlRootElement(name="company")
@XmlSeeAlso({Computer.class, DesktopComputer.class, LaptopComputer.class})
@XmlAccessorType(XmlAccessType.FIELD)
public class Company {

  @XmlElementWrapper(name = "computers")
  @XmlElements({
      @XmlElement(type = DesktopComputer.class, name = "desktop"),
      @XmlElement(type = LaptopComputer.class, name = "laptop")
  })
  private List<Computer> computers;

  public Company() {
    computers = new ArrayList<Computer>();
  }

  public List<Computer> getComputers() {
    return computers;
  }

  public void setComputers(List<Computer> computers) {
    this.computers = computers;
  }

  public Company addComputer(? extends Computer computer) {
    if ( computers == null ) {
      computers = new ArrayList<Computer>();
    }
    computers.add(computer);
    return this;
  }
}

Computer:
@XmlSeeAlso({ LaptopComputer.class, DesktopComputer.class })
@XmlAccessorType(XmlAccessType.FIELD)
public class Computer {

  @XmlAttribute
  @XmlID
  private String id;

  public String getId() {
    return id;
  }
  public void setId(String id) {
    this.id = id;
  }
}


DesktopComputer:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "desktop")
public class DesktopComputer extends Computer {

  @XmlElement(name = "location")
  private String location;

  public String getLocation() {
    return location;
  }
  public void setLocation(String location) {
    this.location = location;
  }
}

LaptopComputer:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "laptop")
public class LaptopComputer extends Computer {

  @XmlElement(name = "vendor")
  private String vendor;

  public String getVendor() {
    return vendor;
  }
  public void setVendor(String vendor) {
    this.vendor = vendor;
  }
}


In XML, this all works as expected and I get the following output:
<?xml version="1.0" encoding="UTF-8"?>
<company>
   <computers>
      <desktop id="computer-1">
         <location>Bangkok</location>
      </desktop>
      <desktop id="computer-2">
         <location>Pattaya</location>
      </desktop>
      <laptop id="computer-3">
         <vendor>Apple</vendor>
      </laptop>
   </computers>
</company>


In JSON, I get inner arrays for each subtype:
{
   "company" : {
      "computers" : {
         "desktop" : [ {
            "id" : "computer-1",
            "location" : "Bangkok"
         }, {
            "id" : "computer-2",
            "location" : "Pattaya"
         } ],
         "laptop" : [ {
            "id" : "computer-3",
            "vendor" : "Apple"
         } ]
      }
   }
}


How can I eliminate the inner arrays to get the following JSON output?
{
    "company" : {
        "computers" : {
            "desktop" : {
                "id" : "computer-1",
                "location" : "Bangkok"
            }, 
            "desktop": {
                "id" : "computer-2",
                "location" : "Pattaya"
            },
            "laptop" : {
                "id" : "computer-3",
                "vendor" : "Apple"
            }
        }
    }
}





Previous Topic:How to do a complex LEFT JOIN FETCH condition in JPQL?
Next Topic:EclipseLink selects a wrong MongoDB collection for querying
Goto Forum:
  


Current Time: Fri Mar 05 05:09:03 GMT 2021

Powered by FUDForum. Page generated in 0.02092 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top