/** * Constructs a new immutable {@link JettisonConfig} object based on options set on this Builder * * @return a non-null {@link JettisonConfig} instance */ public JettisonConfig build() { return new JettisonConfig(this); }
/** * A static method for obtaining {@link JettisonConfig} instance with humanReadableFormatting * set according to formatted parameter. * * @param c original instance of {@link JettisonConfig}, can't be null * @return copy of provided {@link JettisonConfig} with humanReadableFormatting set to formatted. * @throws IllegalArgumentException when provided {@code JettisonConfig} is null. */ public static JettisonConfig createJSONConfiguration(final JettisonConfig c) throws IllegalArgumentException { if (c == null) { throw new IllegalArgumentException("JettisonConfig can't be null"); } final Builder b = copyBuilder(c); return b.build(); }
@Override public void setProperty(String name, Object value) throws PropertyException { if (name == null) { throw new IllegalArgumentException("Name can't be null."); } if (name.equals(org.glassfish.jersey.jettison.JettisonMarshaller.FORMATTED)) { if (!(value instanceof Boolean)) { throw new PropertyException("property " + name + " must be an instance of type " + "boolean, not " + value.getClass().getName()); } jsonConfig = JettisonConfig.createJSONConfiguration(jsonConfig); } else { jaxbMarshaller.setProperty(name, value); } }
private void copyAttributes(final JettisonConfig jc) { jsonXml2JsonNs.putAll(jc.getXml2JsonNs()); serializeAsArray.addAll(jc.getArrayElements()); } }
public static XMLStreamWriter createWriter(final Writer writer, final JettisonConfig config) throws IOException { switch (config.getNotation()) { case BADGERFISH: return new BadgerFishXMLStreamWriter(writer); case MAPPED_JETTISON: Configuration jmConfig; if (null == config.getXml2JsonNs()) { jmConfig = new Configuration(); } else { jmConfig = new Configuration(config.getXml2JsonNs()); } final MappedXMLStreamWriter result = new MappedXMLStreamWriter(new MappedNamespaceConvention(jmConfig), writer); for (String array : config.getArrayElements()) { result.serializeAsArray(array); } return result; default: return null; } }
public static XMLStreamReader createReader(final Reader reader, final JettisonConfig config) throws XMLStreamException { Reader nonEmptyReader = ensureNonEmptyReader(reader); switch (config.getNotation()) { case MAPPED_JETTISON: try { Configuration jmConfig; if (null == config.getXml2JsonNs()) { jmConfig = new Configuration(); } else { jmConfig = new Configuration(config.getXml2JsonNs()); } return new MappedXMLStreamReader( new JSONObject(new JSONTokener(ReaderWriter.readFromAsString(nonEmptyReader))), new MappedNamespaceConvention(jmConfig)); } catch (Exception ex) { throw new XMLStreamException(ex); } case BADGERFISH: try { return new BadgerFishXMLStreamReader( new JSONObject(new JSONTokener(ReaderWriter.readFromAsString(nonEmptyReader)))); } catch (Exception ex) { throw new XMLStreamException(ex); } } // This should not occur throw new IllegalArgumentException("Unknown JSON config"); }
public static Builder copyBuilder(final JettisonConfig jc) { Builder result = new Builder(jc.getNotation()); switch (jc.notation) { case BADGERFISH: result = new Builder(jc.getNotation()); break; case MAPPED_JETTISON: result = new MappedJettisonBuilder(jc.getNotation()); break; } result.copyAttributes(jc); return result; }
public static XMLStreamWriter createWriter(final Writer writer, final JettisonConfig config) throws IOException { switch (config.getNotation()) { case BADGERFISH: return new BadgerFishXMLStreamWriter(writer); case MAPPED_JETTISON: Configuration jmConfig; if (null == config.getXml2JsonNs()) { jmConfig = new Configuration(); } else { jmConfig = new Configuration(config.getXml2JsonNs()); } final MappedXMLStreamWriter result = new MappedXMLStreamWriter(new MappedNamespaceConvention(jmConfig), writer); for (String array : config.getArrayElements()) { result.serializeAsArray(array); } return result; default: return null; } }
private void copyAttributes(final JettisonConfig jc) { jsonXml2JsonNs.putAll(jc.getXml2JsonNs()); serializeAsArray.addAll(jc.getArrayElements()); } }
public static XMLStreamReader createReader(final Reader reader, final JettisonConfig config) throws XMLStreamException { Reader nonEmptyReader = ensureNonEmptyReader(reader); switch (config.getNotation()) { case MAPPED_JETTISON: try { Configuration jmConfig; if (null == config.getXml2JsonNs()) { jmConfig = new Configuration(); } else { jmConfig = new Configuration(config.getXml2JsonNs()); } return new MappedXMLStreamReader( new JSONObject(new JSONTokener(ReaderWriter.readFromAsString(nonEmptyReader))), new MappedNamespaceConvention(jmConfig)); } catch (Exception ex) { throw new XMLStreamException(ex); } case BADGERFISH: try { return new BadgerFishXMLStreamReader( new JSONObject(new JSONTokener(ReaderWriter.readFromAsString(nonEmptyReader)))); } catch (Exception ex) { throw new XMLStreamException(ex); } } // This should not occur throw new IllegalArgumentException("Unknown JSON config"); }
public static Builder copyBuilder(final JettisonConfig jc) { Builder result = new Builder(jc.getNotation()); switch (jc.notation) { case BADGERFISH: result = new Builder(jc.getNotation()); break; case MAPPED_JETTISON: result = new MappedJettisonBuilder(jc.getNotation()); break; } result.copyAttributes(jc); return result; }
@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); } }
/** * Constructs a new immutable {@link JettisonConfig} object based on options set on this Builder * * @return a non-null {@link JettisonConfig} instance */ public JettisonConfig build() { return new JettisonConfig(this); }
/** * A static method for obtaining {@link JettisonConfig} instance with humanReadableFormatting * set according to formatted parameter. * * @param c original instance of {@link JettisonConfig}, can't be null * @return copy of provided {@link JettisonConfig} with humanReadableFormatting set to formatted. * @throws IllegalArgumentException when provided {@code JettisonConfig} is null. */ public static JettisonConfig createJSONConfiguration(final JettisonConfig c) throws IllegalArgumentException { if (c == null) { throw new IllegalArgumentException("JettisonConfig can't be null"); } final Builder b = copyBuilder(c); return b.build(); }
@Override public void setProperty(String name, Object value) throws PropertyException { if (name == null) { throw new IllegalArgumentException("Name can't be null."); } if (name.equals(org.glassfish.jersey.jettison.JettisonMarshaller.FORMATTED)) { if (!(value instanceof Boolean)) { throw new PropertyException("property " + name + " must be an instance of type " + "boolean, not " + value.getClass().getName()); } jsonConfig = JettisonConfig.createJSONConfiguration(jsonConfig); } else { jaxbMarshaller.setProperty(name, value); } }
@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); } }