/** * Get a {@link org.glassfish.jersey.jettison.JettisonMarshaller} from a {@link javax.xml.bind.Marshaller}. * * @param marshaller the JAXB marshaller. * @return the JSON marshaller. */ public static org.glassfish.jersey.jettison.JettisonMarshaller getJSONMarshaller(Marshaller marshaller) { if (marshaller instanceof org.glassfish.jersey.jettison.JettisonMarshaller) { return (org.glassfish.jersey.jettison.JettisonMarshaller) marshaller; } else { return new BaseJsonMarshaller(marshaller, JettisonConfig.DEFAULT); } }
/** * Get a {@link org.glassfish.jersey.jettison.JettisonUnmarshaller} from a {@link javax.xml.bind.Unmarshaller}. * * @param unmarshaller the JAXB unmarshaller. * @return the JSON unmarshaller. */ public static org.glassfish.jersey.jettison.JettisonUnmarshaller getJSONUnmarshaller(Unmarshaller unmarshaller) { if (unmarshaller instanceof org.glassfish.jersey.jettison.JettisonUnmarshaller) { return (org.glassfish.jersey.jettison.JettisonUnmarshaller) unmarshaller; } else { return new BaseJsonUnmarshaller(unmarshaller, JettisonConfig.DEFAULT); } }
public <T> JAXBElement<T> unmarshalJAXBElementFromJSON(InputStream inputStream, Class<T> declaredType) throws JAXBException { return unmarshalJAXBElementFromJSON(new InputStreamReader(inputStream, UTF8), declaredType); }
@SuppressWarnings("unchecked") public <T> T unmarshalFromJSON(Reader reader, Class<T> expectedType) throws JAXBException { if (!expectedType.isAnnotationPresent(XmlRootElement.class)) { return unmarshalJAXBElementFromJSON(reader, expectedType).getValue(); } else { return (T) jaxbUnmarshaller.unmarshal(createXmlStreamReader(reader)); } }
@Override protected final XMLStreamReader getXMLStreamReader(Class<?> elementType, MediaType mediaType, Unmarshaller u, InputStream entityStream) throws XMLStreamException { JettisonConfig c = JettisonConfig.DEFAULT; final Charset charset = getCharset(mediaType); if (u instanceof JettisonConfigured) { c = ((JettisonConfigured) u).getJSONConfiguration(); } return Stax2JettisonFactory.createReader(new InputStreamReader(entityStream, charset), c); } }
@Override public JSONArray readFrom( Class<JSONArray> type, Type genericType, Annotation annotations[], MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException { try { return new JSONArray(readFromAsString(entityStream, mediaType)); } catch (JSONException je) { throw new WebApplicationException( new Exception(LocalizationMessages.ERROR_PARSING_JSON_ARRAY(), je), 400); } }
@Override public JSONObject readFrom( Class<JSONObject> type, Type genericType, Annotation annotations[], MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException { try { return new JSONObject(readFromAsString(entityStream, mediaType)); } catch (JSONException je) { throw new WebApplicationException( new Exception(LocalizationMessages.ERROR_PARSING_JSON_OBJECT(), je), 400); } }
@Produces("*/*") @Consumes("*/*") public static final class General extends JettisonArrayProvider { @Override protected boolean isSupported(MediaType m) { return m.getSubtype().endsWith("+json"); } }
@Produces("*/*") @Consumes("*/*") public static final class General extends JettisonObjectProvider { @Override protected boolean isSupported(MediaType m) { return m.getSubtype().endsWith("+json"); } }
@Override public final void writeCollection(Class<?> elementType, Collection<?> t, MediaType mediaType, Charset c, Marshaller m, OutputStream entityStream) throws JAXBException, IOException { final OutputStreamWriter osw = new OutputStreamWriter(entityStream, c); JettisonConfig origJsonConfig = JettisonConfig.DEFAULT; if (m instanceof JettisonConfigured) { origJsonConfig = ((JettisonConfigured) m).getJSONConfiguration(); } final JettisonConfig unwrappingJsonConfig = JettisonConfig.createJSONConfiguration(origJsonConfig); final XMLStreamWriter jxsw = Stax2JettisonFactory.createWriter(osw, unwrappingJsonConfig); final String invisibleRootName = getRootElementName(elementType); try { jxsw.writeStartDocument(); jxsw.writeStartElement(invisibleRootName); for (Object o : t) { m.marshal(o, jxsw); } jxsw.writeEndElement(); jxsw.writeEndDocument(); jxsw.flush(); } catch (XMLStreamException ex) { Logger.getLogger(JettisonListElementProvider.class.getName()).log(Level.SEVERE, null, ex); throw new JAXBException(ex.getMessage(), ex); } }
/** * Overrides underlaying createMarshaller method and returns * a marshaller which is capable of JSON serialization. * * @return marshaller instance with JSON capabilities * @throws javax.xml.bind.JAXBException */ @Override public Marshaller createMarshaller() throws JAXBException { return new JettisonJaxbMarshaller(jaxbContext, getJSONConfiguration()); }
/** * Overrides underlying createUnmarshaller method and returns * an unmarshaller which is capable of JSON deserialization. * * @return unmarshaller instance with JSON capabilities * @throws javax.xml.bind.JAXBException */ @Override public Unmarshaller createUnmarshaller() throws JAXBException { return new JettisonJaxbUnmarshaller(jaxbContext, getJSONConfiguration()); }
public <T> T unmarshalFromJSON(InputStream inputStream, Class<T> expectedType) throws JAXBException { return unmarshalFromJSON(new InputStreamReader(inputStream, UTF8), expectedType); }
public <T> JAXBElement<T> unmarshalJAXBElementFromJSON(Reader reader, Class<T> declaredType) throws JAXBException { return jaxbUnmarshaller.unmarshal(createXmlStreamReader(reader), declaredType); }
public void marshallToJSON(Object o, OutputStream outputStream) throws JAXBException { if (outputStream == null) { throw new IllegalArgumentException("The output stream is null"); } marshallToJSON(o, new OutputStreamWriter(outputStream, UTF8)); }
public void marshallToJSON(Object o, Writer writer) throws JAXBException { if (o == null) { throw new IllegalArgumentException("The JAXB element is null"); } if (writer == null) { throw new IllegalArgumentException("The writer is null"); } jaxbMarshaller.marshal(o, getXMLStreamWriter(writer)); }
private XMLStreamWriter getXMLStreamWriter(Writer writer) throws JAXBException { try { return Stax2JettisonFactory.createWriter(writer, jsonConfig); } catch (IOException ex) { throw new JAXBException(ex); } }
private XMLStreamReader createXmlStreamReader(Reader reader) throws JAXBException { try { return Stax2JettisonFactory.createReader(reader, jsonConfig); } catch (XMLStreamException ex) { throw new UnmarshalException("Error creating JSON-based XMLStreamReader", ex); } } }
/** * Create a JSON marshaller. * * @return the JSON marshaller. * * @throws javax.xml.bind.JAXBException if there is an error creating the marshaller. */ public org.glassfish.jersey.jettison.JettisonMarshaller createJsonMarshaller() throws JAXBException { return new JettisonJaxbMarshaller(this, getJSONConfiguration()); }
/** * Create a JSON unmarshaller. * * @return the JSON unmarshaller * * @throws javax.xml.bind.JAXBException if there is an error creating the unmarshaller. */ public org.glassfish.jersey.jettison.JettisonUnmarshaller createJsonUnmarshaller() throws JAXBException { return new JettisonJaxbUnmarshaller(this, getJSONConfiguration()); }