Skip to main content
Jump to: navigation, search

EclipseLink/Examples/MOXy/JSON Twitter

JSON Binding: Twitter Example

This example will demonstrate using MOXy to read and write JSON. In this example our JSON corresponds to the results of a query to get recent tweets about JAXB. We will use the Twitter Search API for this.

Twitter Query

Below is the URL corresponding to the search. We are looking for posts containing the word "jaxb", and want the result returned as JSON:

JSON Result

Below is an example excerpt of the JSON returned from Twitter:

         "created_at":"Fri, 12 Aug 2011 09:00:26 +0000",
         "source":"<a href="" rel="nofollow">SOICHA</a>",
         "text":"How do you pronounce JAXB?: You know, URL is earl...",
         "created_at":"Fri, 12 Aug 2011 01:14:57 +0000",
         "source":"<a href="" rel="nofollow">twitterfeed</a>",
         "text":"How do you customise the XML output of Jersey JAXB serialization?",

Domain Model

MOXy leverages the same annotations used to map objects to/from XML for its JSON binding.


package org.example;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
public class SearchResults {
    private String query;
    private float completedIn;
    List<Result> results;
    public String getQuery() {
        return query;
    public void setQuery(String query) {
        this.query = query;
    public float getCompletedIn() {
        return completedIn;
    public void setCompletedIn(float completedIn) {
        this.completedIn = completedIn;
    public List<Result> getResults() {
        return results;
    public void setResults(List<Result> results) {
        this.results = results;


package blog.json.twitter;
import java.util.Date;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
public class Result {
    private Date createdAt;
    private String fromUser;
    private String text;
    public Date getCreatedAt() {
        return createdAt;
    public void setCreatedAt(Date createdAt) {
        this.createdAt = createdAt;
    public String getFromUser() {
        return fromUser;
    public void setFromUser(String fromUser) {
        this.fromUser = fromUser;
    public String getText() {
        return text;
    public void setText(String text) {
        this.text = text;


package org.example;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class DateAdapter extends XmlAdapter<String, Date> {
    private SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
    public String marshal(Date date) throws Exception {
        return dateFormat.format(date);
    public Date unmarshal(String string) throws Exception {
        return dateFormat.parse(string);


The standard JAXB APIs are used the process the JSON message. Only two properties need to be set: to specify application/json, and eclipselink.json.include-root to indicate there is no root node. These properties are set on the Marshaller and Unmarshaller. Since the JSON message does not contain a root node that can be used to determine the corresponding object type, we will leverage one of the Unmarshaller methods that lets us specify it.

package org.example;
import java.util.Date; 
import javax.xml.bind.*;
public class Demo {
    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(SearchResults.class);
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        unmarshaller.setProperty("", "application/json");
        unmarshaller.setProperty("eclipselink.json.include-root", false);
        StreamSource source = new StreamSource("");
        JAXBElement<SearchResults> jaxbElement = unmarshaller.unmarshal(source, SearchResults.class);
        Result result = new Result();
        result.setCreatedAt(new Date());
        result.setText("You can now use EclipseLink JAXB (MOXy) with JSON :)");
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty("", "application/json");
        marshaller.setProperty("eclipselink.json.include-root", false);
        marshaller.marshal(jaxbElement, System.out);

In order to use this feature, you must use MOXy as your JAXB provider. To enable MOXy, simply add a file named in the same package as your domain classes with the following entry:



Below is an excerpt of the output from the Demo:

   "completed_in" : 0.153,
   "query" : "jaxb",
   "results" : [ {
      "created_at" : "Fri, 12 Aug 2011 01:14:57 +0000",
      "from_user" : "stackfeed",
      "text" : "How do you customise the XML output of Jersey JAXB serialization?"
   }, {
      "created_at" : "Fri, 12 Aug 2011 09:00:26 +0000",
      "from_user" : "44aki110",
      "text" : "How do you pronounce JAXB?: You know, URL is earl..."
   }, {
   }, {
      "created_at" : "Wed, 28 Mar 2012 15:56:55 -0400",
      "from_user" : "bsmith",
      "text" : "You can now use EclipseLink JAXB (MOXy) with JSON :)"
   } ]

Back to the top