Question

I would like to use JAXB in a JavaSE standalone application to read and write Json files. I managed to do it for XML files with my snippet below, but I don't understand how to switch to Json:

public class Main {

  public static void main(String[] args) throws Exception {
    Book book = new Book();
    book.title = "hello";

    JAXBContext context = JAXBContext.newInstance(Book.class);
    Marshaller marshaller = context.createMarshaller();
    marshaller.marshal(book, System.out);
  }

}

Notes:

  • Performance is not an issue.
  • Web services / REST is out of topic.
Was it helpful?

Solution

Using EclipseLink MOXy, you can add properties to manipulate the output.

    public class Main {

    public static void main(String[] args) {
        Book book = new Book();
        book.title = "hello";

        JAXBContext context;
        try {
            context = JAXBContextFactory.createContext(new Class[] {Book.class}, null);
            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(book, System.out);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }

}

Will result in: { "book" : { "title" : "hello" } }

Both org.eclipse.persistence.moxy-2.5.1.jar and org.eclipse.persistence.core-2.5.1.jar are needed on the classpath. While playing around with this myself, I ran into: hottest JAXB answers. Especially Blaise Doughan answers where very helpful. Search for

MarshallerProperties.MEDIA_TYPE, "application/json" 

for more of his examples.

OTHER TIPS

If you want to , you can create a separate method just like this very simple (if you already have the JAXB libs)

the main class

import java.io.IOException;
import javax.xml.bind.JAXBException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

public class ConvertJson {

    public static void main(String[] args) {
        final JavaObject javaObject = new JavaObject();
        javaObject.setName("Json");
        javaObject.setRole("Moderator");
        javaObject.setAge(28);

        try {
            pojo2Json(javaObject);
        } catch (JAXBException | IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    private static String pojo2Json(Object obj) throws JAXBException,
            JsonParseException, JsonMappingException, IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonString = objectMapper.writeValueAsString(obj);
        System.out.print(jsonString);
        return jsonString;
    }

here is the pojo

import org.codehaus.jackson.annotate.JsonProperty;
public class JavaObject{
    @JsonProperty(value = "Name")
    private String name;
    @JsonProperty(value = "Role")
    private String role;
    @JsonProperty(value = "Age")
    private int age;
    public JavaObject(){
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getRole() {
        return role;
    }
    public void setRole(String role) {
        this.role = role;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

}

}

You would do it like so:

public class Main {
 public static void main(String[] args) throws IOException {
  Book book = new Book();
  book.title = "hello";

  ObjectMapper mapper = new ObjectMapper();
  mapper.writeValue(System.out, book);
 }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top