user20298
user20298

Reputation: 1845

How to let JAXB render boolean as 0 and 1, not true and false

Got a quick question. Does anyone know how to let JAXB (marshall) render boolean fields as 1 and 0 instead of printing out "true" and "false"?

Upvotes: 27

Views: 19070

Answers (5)

mtpettyp
mtpettyp

Reputation: 5569

The adapter class:

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class BooleanAdapter extends XmlAdapter<Integer, Boolean>
{
    @Override
    public Boolean unmarshal( Integer s )
    {
        return s == null ? null : s == 1;
    }

    @Override
    public Integer marshal( Boolean c )
    {
        return c == null ? null : c ? 1 : 0;
    }
}

Usage:

@XmlElement( name = "enabled" )
@XmlJavaTypeAdapter( BooleanAdapter.class )
public Boolean getEnabled()
{
    return enabled;
}

Upvotes: 42

Aim Mistaken
Aim Mistaken

Reputation: 71

Having the same problem as user20298, I followed the hint by mtpettyp, and adapted it for my configuration.

My configuration is: - maven to build the project. - "org.jvnet.jaxb2.maven2" plugin in maven. - jaxb 2.2.6 - In this occasion, I was making Java classes for the kml 2.2 (ogckml22.xsd)

And I stumbled upon the problem of the booleans to be rendered as 'true/false', when Google maps wants them to be as '1/0'

This is the plugin configuration in maven:

<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<executions>
    <execution>
        <phase>generate-sources</phase>
        <goals>
            <goal>generate</goal>
        </goals>
    </execution>
</executions>
<configuration>
    <generateDirectory>src/main/generated</generateDirectory>
    <extension>true</extension>
    <removeOldOutput>true</removeOldOutput>
</configuration>

I added to the src/main/resources folder a jaxb-bindings.xjb file with the following content:

    <?xml version="1.0" ?>
<bindings xmlns="http://java.sun.com/xml/ns/jaxb" 
          version="2.1" 
          xmlns:kml="http://www.opengis.net/kml/2.2"
          xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
          xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <globalBindings>
        <xjc:simple/>
        <xjc:javaType name="java.lang.Boolean" 
                      xmlType="xsd:boolean" 
                      adapter="path.to.my.JaxbBooleanAdapter"/>
    </globalBindings>
    ...
    ...
</bindings>

The adapter class is like:

package path.to.my;

import javax.xml.bind.annotation.adapters.XmlAdapter;

/**
 * Utility class to correctly render the xml types used in JAXB.
 */
public class JaxbBooleanAdapter extends XmlAdapter<String, Boolean>
{
    @Override
    public Boolean unmarshal(String v) throws Exception
    {
        if ("1".equals(v))
        {
            return true;
        }
        return false;
    }

    @Override
    public String marshal(Boolean v) throws Exception
    {
        if (v == null)
        {
            return null;
        }
        if (v)
        {
            return "1";
        }
        return "0";
    }
}

Upvotes: 7

Luc Touraille
Luc Touraille

Reputation: 82121

JAXB provides a flexible way to customize your bindings. You simply have to write an XML file that will indicate how you want to bind your XML and Java types. In your case, you could use a <javaType> declaration, in which you can specify a parseMethod and a printMethod. These methods could be as simple as

public boolean myParseBool(String s)
{
    return s.equals("1");
}

public String myPrintBool(boolean b)
{
    return b ? "1" : "0";
}

There might exist easier ways, maybe using DatatypeConverter, but I'm not enough aware of this subject to help you more !

Upvotes: 6

Gu&#240;mundur Bjarni
Gu&#240;mundur Bjarni

Reputation: 4122

I would probably create a type adapter for converting a boolean to an int. There are some examples in the JAXB users guide.

Upvotes: 1

Dennis C
Dennis C

Reputation: 24747

You can write a pair of parser/writers and define the property mapping in binding JAXB XML.

Upvotes: 0

Related Questions