Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » How can I exclude the type element from marshalled generic type in Moxy?(Excluding type element from marshalled generic type in Moxy)
How can I exclude the type element from marshalled generic type in Moxy? [message #1452084] Fri, 24 October 2014 19:27
John Douglass is currently offline John DouglassFriend
Messages: 1
Registered: October 2014
Junior Member
I am marshalling an object to json using Moxy 2.6.0-M3 (and have the same issue with 2.5.1). The object extends a generic class. I don't want the type element output, so I tried following the example in Blaise Doughan's article called "Ignoring Inheritance with @XmlTransient" (sorry, can't link to it because it's not on eclipse.org.)

That procedure works if the class marked @XmlTransient is not generic, but if it is generic, the type element is always output.

Here's an admittedly artificial example:
public class AB {
    @XmlElement
    String a = "A";
    @XmlElement
    String b = "B";
}

@XmlTransient
public class Value {
    @XmlElement
    @XmlPath(".")
    AB value = new AB();
}

@XmlRootElement
public class Record extends Value {
    @XmlElement
    int id = 1;
}

I marshal a record using the following code:
JAXBContext jaxbContext =
    org.eclipse.persistence.jaxb.JAXBContextFactory
        .createContext(
            new Class[]{ Record.class, AB.class}, null);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON);
marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
Record record = new Record();
marshaller.marshal(record, System.out);

and get the following output:
{"a":"A","b":"B","id":1}

Now if I try the following:
@XmlTransient
public class ValueGeneric<T> {
    @XmlElement
    @XmlPath(".")
    T value;
}

@XmlRootElement
public class RecordAB extends ValueGeneric<AB> {
    @XmlElement
    int id = 1;
}

with this marshalling code (basically the same as above but registering RecordAB.class):
JAXBContext jaxbContext =
    org.eclipse.persistence.jaxb.JAXBContextFactory
        .createContext(
            new Class[]{RecordAB.class, AB.class}, null);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON);
marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
RecordAB recordAB = new RecordAB();
recordAB.value = new AB();
marshaller.marshal(recordAB, System.out);

it marshals as:
{"type":"ab","a":"A","b":"B","id":1}

I want it to marshal like the first one, without the type element.

I don't need to unmarshal, so it's OK if the type information gets lost.

Something similar happens if I output XML; the root element for RecordAB has the xsi:type attribute set to "ab".

Anyone have an idea of how to prevent the type element from being output?
Previous Topic:@CacheIndex with Extended Class
Next Topic:Database Persistence both via JPA and Stored Procedure Calls
Goto Forum:
  


Current Time: Tue Mar 19 02:31:14 GMT 2024

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

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

Back to the top