How can I get MOXy for single line output only to save space

To solve another problem, I switched from using Jersey to EclipseLink MOXy to generate JSON from the JAXB-generated object model (created by Sun JAXB 2.1.12). One difference I noticed is formatting the output

what jersey brings

{"artist-list":{"offset":0,"count":1,"artist":[{"score":"100","type":"Group","id":"4302e264-1cf0-4d1f-aca7-2a6f89e34b36","name":"Farming Incident","sort-name":"Incident, Farming","gender":"male","country":"AF","disambiguation":"the real one","ipi-list":{"ipi":["1001","1002"]},"life-span":{"begin":"1999-04","ended":"true"},"tag-list":{"tag":[{"count":5,"name":"thrash"},{"count":11,"name":"gรผth"}]}}]}}

but moxy gives

"count" : "1",
   "offset" : "0",
   "artist" : [ {
      "id" : "4302e264-1cf0-4d1f-aca7-2a6f89e34b36",
      "type" : "Group",
      "score" : "100",
      "name" : "Farming Incident",
      "sort-name" : "Incident, Farming",
      "gender" : "male",
      "country" : "AF",
      "disambiguation" : "the real one",
      "ipis" : [ "1001", "1002" ],
      "life-span" : {
         "begin" : "1999-04",
         "ended" : "true"
      },
      "tags" : [ {
         "count" : "5",
         "name" : "thrash"
      }, {
         "count" : "11",
         "name" : "gรผth"
      } ]
   } ]
}

Moxi is much prettier :) But one of the reasons for switching to providing our data through Json is to reduce the transmission bandwidth, so that you can get MOXy to generate the entire one line and without extra spaces around each :?

+1
source share
1 answer

EclipseLink JAXB (MOXy) JSON . , Marshaller:

marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

Root

package forum11450509;

public class Root {

    private String foo;
    private int bar;

    public String getFoo() {
        return foo;
    }

    public void setFoo(String foo) {
        this.foo = foo;
    }

    public int getBar() {
        return bar;
    }

    public void setBar(int bar) {
        this.bar = bar;
    }

}

jaxb.properties

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

Demo

, :

package forum11450509;

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>(2);
        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.setFoo("ABC");
        root.setBar(123);

        System.out.println("One Line:");
        Marshaller marshaller = jc.createMarshaller();
        marshaller.marshal(root, System.out);

        System.out.println("\nFormatted:");
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(root, System.out);
    }

}

:

One Line
{"bar":123,"foo":"ABC"}
Formatted:
{
   "bar" : 123,
   "foo" : "ABC"
}

JAX-RS

MOXyJsonProvider (. http://blog.bdoughan.com/2012/05/moxy-as-your-jax-rs-json-provider.html).

, MOXyJsonProvider JAX-RS, :

package org.example;

import java.util.*;
import javax.ws.rs.core.Application;
import org.eclipse.persistence.jaxb.rs.MOXyJsonProvider;

public class CustomerApplication  extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        HashSet<Class<?>> set = new HashSet<Class<?>>(2);
        set.add(MOXyJsonProvider.class);
        set.add(CustomerService.class);
        return set;
    }

}

MOXyJsonProvider :

package org.example;

import java.util.*;
import javax.ws.rs.core.Application;
import org.eclipse.persistence.jaxb.rs.MOXyJsonProvider;

public class CustomerApplication  extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        HashSet<Class<?>> set = new HashSet<Class<?>>(1);
        set.add(ExampleService.class);
        return set;
    }

    @Override
    public Set<Object> getSingletons() {
        MOXyJsonProvider moxyJsonProvider = new MOXyJsonProvider();
        moxyJsonProvider.setFormattedOutput(true);
    }

} 
0

All Articles