View Javadoc

1   package com.atlassian.plugins.rest.common.json;
2   
3   import org.codehaus.jackson.JsonEncoding;
4   
5   import javax.ws.rs.core.MediaType;
6   import javax.xml.bind.JAXBException;
7   import java.io.ByteArrayOutputStream;
8   import java.io.IOException;
9   
10  public class DefaultJaxbJsonMarshaller implements JaxbJsonMarshaller
11  {
12      public String marshal(Object jaxbBean)
13      {
14          try
15          {
16              final ByteArrayOutputStream os = new ByteArrayOutputStream();
17              // Don't use JsonGenerator directly as we want to make sure we use the same
18              // configuration as for REST requests in OsgiResourceConfig
19              new JacksonJsonProviderFactory().create().writeTo(jaxbBean, jaxbBean.getClass(), null, null, MediaType.APPLICATION_JSON_TYPE, null, os);
20              // The encoding used inside JacksonJsonProvider is always UTF-8
21              return new String(os.toByteArray(), JsonEncoding.UTF8.getJavaName());
22          }
23          catch (IOException e)
24          {
25              throw new JsonMarshallingException(e);
26          }
27      }
28  
29      @Deprecated
30      public String marshal(final Object jaxbBean, final Class... jaxbClasses) throws JAXBException
31      {
32          return marshal(jaxbBean);
33      }
34  }