Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » MOXy: JSON marshalling not using XMLType's propOrder
MOXy: JSON marshalling not using XMLType's propOrder [message #792240] Mon, 06 February 2012 14:44 Go to next message
David Vree is currently offline David Vree
Messages: 45
Registered: July 2010
Member
I can see that MOXy respects the propOrder attribute of XMLType when marshalling to XML. I have seen this work with both JAX-B annotations and OXM mapping files.

However, when marshalling to JSON it does not appear to retain the order. I realize that this doesn't matter to Javascript, but it does affect the human readability of payloads.

Is this a bug or is there a setting somewhere I need to configure?
Re: MOXy: JSON marshalling not using XMLType's propOrder [message #792930 is a reply to message #792240] Tue, 07 February 2012 10:27 Go to previous messageGo to next message
Blaise Doughan is currently offline Blaise Doughan
Messages: 163
Registered: July 2009
Senior Member

MOXy does respect the ordering from the propOrder attribute on the XMLType annotation when marshalling to JSON. As with the XML binding the propOrder attribute does not apply to any property mapped with @XmlAttribute.

import javax.xml.bind.annotation.XmlType;

@XmlType(propOrder={"c", "a", "b"})
public class Root {

    private String a;
    private String b;
    private String c;

    public String getA() {
        return a;
    }

    public void setA(String a) {
        this.a = a;
    }

    public String getB() {
        return b;
    }

    public void setB(String b) {
        this.b = b;
    }

    public String getC() {
        return c;
    }

    public void setC(String c) {
        this.c = c;
    }

}


Demo

import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Root.class);

        Root root = new Root();
        root.setA("A");
        root.setB("B");
        root.setC("C");

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty("eclipselink.media-type", "application/json");
        marshaller.marshal(root, System.out);
    }

}


Output

{"c" : "C","a" : "A","b" : "B"}
Re: MOXy: JSON marshalling not using XMLType's propOrder [message #792937 is a reply to message #792240] Tue, 07 February 2012 10:27 Go to previous messageGo to next message
Blaise Doughan is currently offline Blaise Doughan
Messages: 163
Registered: July 2009
Senior Member

MOXy does respect the ordering from the propOrder attribute on the XMLType annotation when marshalling to JSON. As with the XML binding the propOrder attribute does not apply to any property mapped with @XmlAttribute.

import javax.xml.bind.annotation.XmlType;

@XmlType(propOrder={"c", "a", "b"})
public class Root {

private String a;
private String b;
private String c;

public String getA() {
return a;
}

public void setA(String a) {
this.a = a;
}

public String getB() {
return b;
}

public void setB(String b) {
this.b = b;
}

public String getC() {
return c;
}

public void setC(String c) {
this.c = c;
}

}

Demo

import javax.xml.bind.*;

public class Demo {

public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Root.class);

Root root = new Root();
root.setA("A");
root.setB("B");
root.setC("C");

Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty("eclipselink.media-type", "application/json");
marshaller.marshal(root, System.out);
}

}

Output

{"c" : "C","a" : "A","b" : "B"}
Re: MOXy: JSON marshalling not using XMLType's propOrder [message #792981 is a reply to message #792930] Tue, 07 February 2012 11:25 Go to previous message
David Vree is currently offline David Vree
Messages: 45
Registered: July 2010
Member
Thanks. I don't know why this forum keeps double posting...its wierd.

FWIW - I ran some tests and the XML marshaller is sorting the XMLAttributes even if it is not supposed to...I can change the propOrder and it changes the attribute order in XML. I am also now seeing the correct order in JSON, I don't know why I wasn't seeing it earlier.
Re: MOXy: JSON marshalling not using XMLType's propOrder [message #792982 is a reply to message #792937] Tue, 07 February 2012 11:25 Go to previous message
David Vree is currently offline David Vree
Messages: 45
Registered: July 2010
Member
Thanks. I don't know why this forum keeps double posting...its wierd.

FWIW - I ran some tests and the XML marshaller is sorting the XMLAttributes even if it is not supposed to...I can change the propOrder and it changes the attribute order in XML. I am also now seeing the correct order in JSON, I don't know why I wasn't seeing it earlier.
Previous Topic:MOXy: JSON marshalling not using XMLType's propOrder
Next Topic:JPA Partitioning and Spring
Goto Forum:
  


Current Time: Thu Aug 28 01:36:46 EDT 2014

Powered by FUDForum. Page generated in 0.35054 seconds