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

The object model has an element terminated by a String.

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

but actually this is just a boolean value (I don't know the value of the XmlJavaTypeAdapter annotation)

When XML output gives

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

therefore, it doesnโ€™t matter if it is defined as logical or string

but the json output is

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

when i need it

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

I cannot change the object model, so I thought that I could map the correct type in the oxml.xml file and try

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

but he didnโ€™t like it.

+4
source share
1 answer

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

BooleanStringAdapter

An XmlAdapter allows XmlAdapter to march 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 value of String as a Boolean .

 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 use the external MOXy mapping file to expand the metadata provided via annotations to enable 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> 

Lifepan

The following is a model of your domain 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 add a file called jaxb.properties followed by a record (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 use an 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

The following is the result of running the example. As you can see, true displayed without quotes:

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

All Articles