Package com.sun.jersey.api.json

Examples of com.sun.jersey.api.json.JSONJAXBContext


        assertFalse(sw.toString().contains("\n"));
    }

    public void testNewLineAdded() throws Exception {

        final JSONJAXBContext ctx = new JSONJAXBContext(JSONConfiguration.natural().rootUnwrapping(false).humanReadableFormatting(true).build(), User.class);
        final JSONMarshaller jm = ctx.createJSONMarshaller();
        final StringWriter sw = new StringWriter();

        final User one = JSONTestHelper.createTestInstance(User.class);
        jm.marshallToJSON(one, sw);
View Full Code Here


* @author japod
*/
public class UnboundedAnyTest extends TestCase {

    public void testUnboundedAny() throws Exception {
        final JSONJAXBContext ctx = new JSONJAXBContext(JSONConfiguration.natural().build(), Fruit.class, Fruits.class);
        final JSONMarshaller jm = ctx.createJSONMarshaller();
        final JSONUnmarshaller ju = ctx.createJSONUnmarshaller();
        final StringWriter sw = new StringWriter();

        Fruits fruits = createFruits();

        jm.marshallToJSON(fruits, sw);
View Full Code Here

    /**
     * JAXB RI marshals the given bean to a JSON representation as expected even though it does not handle contents of
     * {@code ComplexXmlEventBean#cdata} field as a CData section but as a pure XML element with text data in it.
     */
    public void _testXmlCdataAnnotation(final JSONConfiguration configuration) throws Exception {
        final JAXBContext jaxbContext = new JSONJAXBContext(ComplexXmlEventBean.class);

        // Marshal
        final Marshaller marshaller = jaxbContext.createMarshaller();
        final StringWriter writer = new StringWriter();

        final Object testInstance = ComplexXmlEventBean.createTestInstance();
        marshaller.marshal(testInstance,
                Stax2JsonFactory.createWriter(writer, configuration, ComplexXmlEventBean.class, jaxbContext));

        System.out.println("Marshalled XML:");
        marshaller.marshal(testInstance, System.out);

        System.out.println("\nMarshalled JSON:");
        System.out.println(writer.toString());

        // Unmarshal
        final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        final JAXBElement<ComplexXmlEventBean> unmarshal = unmarshaller.unmarshal(
                Stax2JsonFactory.createReader(new StringReader(writer.toString()), configuration, null,
                        ComplexXmlEventBean.class, jaxbContext, false),
                ComplexXmlEventBean.class);

View Full Code Here

        ns2json.put("http://www.w3.org/2001/XMLSchema-instance", "xsi");
        tryConfigurationWithStringInput(JSONConfiguration.mapped().rootUnwrapping(true).xml2JsonNs(ns2json).build(), "{\"nullString\":null}");
    }

    private void tryConfigurationWithStringInput(JSONConfiguration configuration, String inputJSON) throws Exception {
        final JSONJAXBContext ctx = new JSONJAXBContext(configuration, NullStringBean.class);
        final JSONUnmarshaller ju = ctx.createJSONUnmarshaller();

        NullStringBean two = ju.unmarshalFromJSON(new StringReader(inputJSON), NullStringBean.class);

        assertEquals(one, two);
    }
View Full Code Here

        assertEquals(one, two);
    }

    private void tryWithConfiguration(JSONConfiguration configuration) throws Exception {

        final JSONJAXBContext ctx = new JSONJAXBContext(configuration, NullStringBean.class);
        final JSONMarshaller jm = ctx.createJSONMarshaller();
        final JSONUnmarshaller ju = ctx.createJSONUnmarshaller();
        StringWriter sw = new StringWriter();
        jm.marshallToJSON(one, sw);
        System.out.println(String.format("Marshalled: %s", sw));

        NullStringBean two = ju.unmarshalFromJSON(new StringReader(sw.toString()), NullStringBean.class);
View Full Code Here

*/
public class NaturalAttributesPrefixTest extends TestCase {

    public void testNoPrefixAdded() throws Exception {

        final JSONJAXBContext ctx = new JSONJAXBContext(JSONConfiguration.natural().build(), SimpleBeanWithAttributes.class);
        final JSONMarshaller jm = ctx.createJSONMarshaller();
        final StringWriter sw = new StringWriter();

        final SimpleBeanWithAttributes one = JSONTestHelper.createTestInstance(SimpleBeanWithAttributes.class);
        jm.marshallToJSON(one, sw);
        System.out.println(sw.toString());
View Full Code Here

        assertFalse(sw.toString().contains("@"));
    }

    public void testPrefixAdded() throws Exception {

        final JSONJAXBContext ctx = new JSONJAXBContext(JSONConfiguration.natural().usePrefixesAtNaturalAttributes().build(), SimpleBeanWithAttributes.class);
        final JSONMarshaller jm = ctx.createJSONMarshaller();
        final StringWriter sw = new StringWriter();

        final SimpleBeanWithAttributes one= JSONTestHelper.createTestInstance(SimpleBeanWithAttributes.class);
        jm.marshallToJSON(one, sw);
        System.out.println(sw.toString());
View Full Code Here

        assertTrue(sw.toString().contains("@"));
    }

    public void testRoundtripWithPrefixAdded() throws Exception {

        final JSONJAXBContext ctx = new JSONJAXBContext(JSONConfiguration.natural().usePrefixesAtNaturalAttributes().build(), SimpleBeanWithAttributes.class);
        final JSONMarshaller jm = ctx.createJSONMarshaller();
        final JSONUnmarshaller ju = ctx.createJSONUnmarshaller();
        final StringWriter sw = new StringWriter();

        final SimpleBeanWithAttributes one = JSONTestHelper.createTestInstance(SimpleBeanWithAttributes.class);
        jm.marshallToJSON(one, sw);
        System.out.println(sw.toString());
View Full Code Here

*/
public class AnotherArrayTest extends TestCase {

    public void testAnotherSimpleXmlTypeBean() throws Exception {

        final JSONJAXBContext ctx = new JSONJAXBContext(JSONConfiguration.mapped().arrays("cats").build(), AnotherArrayTestBean.class);
        final JSONMarshaller jm = ctx.createJSONMarshaller();
        final StringWriter sw = new StringWriter();

        AnotherArrayTestBean one = new AnotherArrayTestBean();
        Cat c1 = new Cat("Foo", "Kitty");
        one.addCat(c1);
View Full Code Here

            assertTrue(caught);
        }
    }

    public void _testException(final String value) throws Exception {
        final JSONJAXBContext ctx = new JSONJAXBContext(
                JSONConfiguration.mapped().build(), Bean.class);
        final JSONMarshaller jm = ctx.createJSONMarshaller();
        final StringWriter sw = new StringWriter();
        final Writer w = new FilterWriter(sw) {
            @Override
            public void write(String str) throws IOException {
                if (str.contains(value))
View Full Code Here

TOP

Related Classes of com.sun.jersey.api.json.JSONJAXBContext

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.