Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Why does JAXB change null to "", when @XmlValue is used for JSON?
Why does JAXB change null to "", when @XmlValue is used for JSON? [message #1717855] Thu, 17 December 2015 11:50
Slarti Bartfast is currently offline Slarti BartfastFriend
Messages: 1
Registered: December 2015
Junior Member
I posted this question on Stackoverflow first, but now I'm a bit unsure, whether it's a bug in MOXy or not.

I have this JSON-Content:

And I want to make these Java-Objects out of it (and vice-versa):
|- List<Entry> entries
   |- Entry
      |- String key = "color"
      |- String value = null

My current solution always deserializes "color":null to an empty String.

Serialization works fine.

How can I get MOXy / jaxb to always deserialize null as null?

I am using this code:
import java.util.*;

import javax.xml.bind.*;
import javax.xml.bind.annotation.*;

import org.eclipse.persistence.internal.oxm.ByteArraySource;
import org.eclipse.persistence.jaxb.JAXBContextProperties;
import org.eclipse.persistence.oxm.annotations.*;
import org.junit.*;

class Container {

    List<Entry> entries = new ArrayList<Entry>();   

class Entry {

    public String key;

    @XmlNullPolicy(nullRepresentationForXml=XmlMarshalNullRepresentation.XSI_NIL, xsiNilRepresentsNull=false)
    public String value;

public class D {

    /** THIS TEST FAILS!!! */

    public void unmarshallNull() throws Exception {
        Assert.assertEquals(null, unmarshall("{\"color\":null}"));

    /* All other tests are passing. */

    public void unmarshallEmpty() throws Exception {
        Assert.assertEquals("", unmarshall("{\"color\":\"\"}"));

    public void unmarshallValue() throws Exception {
        Assert.assertEquals("red", unmarshall("{\"color\":\"red\"}"));

    public void marshallNull() throws Exception {
        Assert.assertEquals("{\"color\":null}", marshall(null));

    public void marshallEmpty() throws Exception {
        Assert.assertEquals("{\"color\":\"\"}", marshall(""));

    public void marshallValue() throws Exception {
        Assert.assertEquals("{\"color\":\"red\"}", marshall("red"));

    private static String marshall(String value) throws JAXBException {

        // prepare
        JAXBContext jc = JAXBContext.newInstance(Container.class);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(JAXBContextProperties.MEDIA_TYPE, "application/json");
        marshaller.setProperty(JAXBContextProperties.JSON_INCLUDE_ROOT, false);

        // define example data
        Container detail = new Container();
        Entry entry = new Entry();
        entry.key = "color";
        entry.value = value;

        // marshall
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        marshaller.marshal(detail, outputStream);
        return outputStream.toString();

    private static String unmarshall(String raw) throws JAXBException {

        // prepare
        JAXBContext jc = JAXBContext.newInstance(Container.class);
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        unmarshaller.setProperty(JAXBContextProperties.MEDIA_TYPE, "application/json");
        unmarshaller.setProperty(JAXBContextProperties.JSON_INCLUDE_ROOT, false);

        // unmarshall
        Container container = unmarshaller.unmarshal(new ByteArraySource(raw.getBytes()), Container.class).getValue();
        return container.entries.get(0).value;

It fails:
Test failure: unmarshallNull
java.lang.AssertionError: expected:<null> but was:<>

I also configured MOXy as my jaxb-provider for the relevant package (

I'm using MOXy 2.22.1 and Java8 (but same behaviour with 2.18, 2.19, 2.20, 2.21, 2.22). My Maven pom.xml:

<project xmlns="..." xmlns:xsi="..."

Previous Topic:EclipseLink selects a wrong MongoDB collection for querying
Next Topic:NPE with @PreUpdate entity listener
Goto Forum:

Current Time: Thu Feb 25 17:13:56 GMT 2021

Powered by FUDForum. Page generated in 0.02118 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top