How to get Json generated by MOXy to understand when a model is a number

To solve another problem, I switched from Jersey to EclipseLink MOXy to create JSON from the JAXB-generated object model (created by Sun JAXB 2.1.12). One difference that I noticed is that in an object model a numerical attribute is defined as

@XmlSchemaType(name = "nonNegativeInteger")
protected BigInteger count;

Jersey turns into

"count":1,

but moxy gives

"count" : "1",

How can I get MOXy to implement my number field, rather than quote it.

+1
source share
1 answer

UPDATE

EclipseLink 2.4.1 2.5.0. , , 13 2012 . :


EclipseLink JAXB (MOXy) JSON . @XmlSchemaType . , :

MOXy /. , count, @XmlSchemaType.

oxm.xml

<?xml version="1.0"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="forum11448966">
    <java-types>
        <java-type name="Root">
            <java-attributes>
                <xml-element java-attribute="count"/>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

Root

package forum11448966;

import java.math.BigInteger;

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class Root {

    @XmlSchemaType(name = "nonNegativeInteger")
    protected BigInteger count;

}

jaxb.properties

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

Demo

package forum11448966;

import java.math.BigInteger;
import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextProperties;

public class Demo {

    public static void main(String[] args) throws Exception {
        Map<String,Object> properties = new HashMap<String, Object>(3);
        properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "forum11448966/oxm.xml");
        properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
        properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
        JAXBContext jc = JAXBContext.newInstance(new Class[] {Root.class}, properties);

        Root root = new Root();
        root.count = BigInteger.TEN;

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(root, System.out);
    }

}

{
   "count" : 10
}
+1

All Articles