Can I get MOXy to convert a string to a boolean when generating json Can I get MOXy to convert a string to a boolean when generating json json json

Can I get MOXy to convert a string to a boolean when generating json


Below is how you can support this use case with EclipseLink JAXB (MOXy):

BooleanStringAdapter

An XmlAdapter allows you to marshal one type of object as another type (see http://blog.bdoughan.com/2010/07/xmladapter-jaxbs-secret-weapon.html). In this example we want to treat the String value as a Boolean one.

package forum11451880;import javax.xml.bind.annotation.adapters.XmlAdapter;public class BooleanStringAdapter extends XmlAdapter<Boolean, String> {    @Override    public String unmarshal(Boolean v) throws Exception {        return v.toString();    }    @Override    public Boolean marshal(String v) throws Exception {        return Boolean.valueOf(v);    }}

oxm.xml

We can leverage MOXy's external mapping document to augment the metadata supplied via annotations to hook in our XmlAadapter (see http://blog.bdoughan.com/2010/12/extending-jaxb-representing-annotations.html).

<?xml version="1.0"?><xml-bindings    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"    package-name="forum11451880">    <java-types>        <java-type name="LifeSpan">            <java-attributes>                <xml-element java-attribute="end">                    <xml-java-type-adapter value="forum11451880.BooleanStringAdapter"/>                </xml-element>            </java-attributes>        </java-type>    </java-types></xml-bindings>

LifeSpan

Below is your domain model with an end property of type String.

package forum11451880;import javax.xml.bind.annotation.*;import javax.xml.bind.annotation.adapters.*;@XmlRootElement@XmlAccessorType(XmlAccessType.FIELD)public class LifeSpan {    protected String begin;    protected String end;    @XmlJavaTypeAdapter(CollapsedStringAdapter.class)    protected String ended;}

jaxb.properties

To specify MOXy as your JAXB provider you need to include a file called jaxb.properties with the followin entry (see http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html):

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

Demo

The demo code below demonstrates how to leverage the external mapping document.

package forum11451880;import java.util.*;import javax.xml.bind.*;import org.eclipse.persistence.jaxb.JAXBContextProperties;import org.eclipse.persistence.jaxb.MarshallerProperties;public class Demo {    public static void main(String[] args) throws Exception {        Map<String, Object> properties = new HashMap<String, Object>(1);        properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "forum11451880/oxm.xml");        JAXBContext jc = JAXBContext.newInstance(new Class[] {LifeSpan.class}, properties);        LifeSpan lifeSpan = new LifeSpan();        lifeSpan.begin = "1999-04";        lifeSpan.end = "true";        Marshaller marshaller = jc.createMarshaller();        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);        marshaller.marshal(lifeSpan, System.out);        marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");        marshaller.marshal(lifeSpan, System.out);    }}

Output

Below is the output from running the example. As you can see the true value appears without quotes:

<?xml version="1.0" encoding="UTF-8"?><lifeSpan>   <begin>1999-04</begin>   <end>true</end></lifeSpan>{   "lifeSpan" : {      "begin" : "1999-04",      "end" : true   }}