Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

The object model has an element ended of type String

public class LifeSpan {

protected String begin;
protected String end;
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
protected String ended;
....

but its actually only ever a boolean, (I dont know the significance of the XmlJavaTypeAdapter annotation)

When output as XML gives

<life-span><begin>1999-04</begin><ended>true</ended></life-span>

so doesn't really matter if defined as boolean or string

but JSON output is

"life-span" : {
         "begin" : "1999-04",
         "ended" : "true"
      },

when I need it to be

 "life-span" : {
         "begin" : "1999-04",
         "ended" : true
      },

I cannot really change the object model so thought I might be able to map to the correct type in oxml.xml file , and tried

<java-type name="LifeSpan">
            <java-attributes>
                <xml-element java-attribute="ended"  type="boolean"/>
            </java-attributes>
        </java-type>

but it didnt like that.

like image 240
Paul Taylor Avatar asked Oct 08 '22 06:10

Paul Taylor


1 Answers

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
   }
}
like image 90
bdoughan Avatar answered Oct 13 '22 00:10

bdoughan