Package com.sun.jersey.api.json

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


    }

    private void tryConfiguration(JSONConfiguration configuration) throws Exception {

        final JSONJAXBContext ctx = new JSONJAXBContext(configuration, NamespaceBeanWithAttribute.class);
        final JSONMarshaller jm = ctx.createJSONMarshaller();
        final JSONUnmarshaller ju = ctx.createJSONUnmarshaller();


        NamespaceBeanWithAttribute beanTwo;

        final StringWriter sw = new StringWriter();

        jm.marshallToJSON(one, sw);

        System.out.println(String.format("%s", sw));

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


public class XmlTypeTest extends TestCase {

    public void testSimpleXmlTypeBean() throws Exception {
       
        final JSONJAXBContext ctx = new JSONJAXBContext(SimpleXmlTypeBean.class);
        final JSONMarshaller jm = ctx.createJSONMarshaller();
        final JSONUnmarshaller ju = ctx.createJSONUnmarshaller();
        final StringWriter sw = new StringWriter();

        final SimpleXmlTypeBean one= JSONTestHelper.createTestInstance(SimpleXmlTypeBean.class);
        SimpleXmlTypeBean two;
        jm.marshallToJSON(new JAXBElement<SimpleXmlTypeBean>(new QName("test"), SimpleXmlTypeBean.class, one), sw);
        two = ju.unmarshalFromJSON(new StringReader(sw.toString()), SimpleXmlTypeBean.class);
        assertEquals(one, two);
    }
View Full Code Here

public class FormattingOptionTest extends TestCase {

    public void testNoNewLinesAdded() throws Exception {

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

        final User one = JSONTestHelper.createTestInstance(User.class);
        jm.marshallToJSON(one, sw);

        assertFalse(sw.toString().contains("\n"));
    }
View Full Code Here

    }

    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);

        assertTrue(sw.toString().contains("\n"));
    }
View Full Code Here

*/
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);

        String jsonResult = sw.toString();
        System.out.println(jsonResult);

        assertEquals("{\"fruit\":{\"name\":\"apple\",\"color\":\"red\"}}", jsonResult);
View Full Code Here

    }

    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);

        assertEquals(one, two);
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());

        assertFalse(sw.toString().contains("@"));
    }
View Full Code Here

    }

    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());

        assertTrue(sw.toString().contains("@"));
    }
View Full Code Here

    }

    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());
       
        SimpleBeanWithAttributes unmarshalledOne = ju.unmarshalFromJSON(new StringReader(sw.toString()), SimpleBeanWithAttributes.class);

        assertEquals(one, unmarshalledOne);
View Full Code Here

    }

    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))
                    throw new IOException();
                super.write(str);
            }
        };

        Bean b = new Bean("A", "B", "C");
        jm.marshallToJSON(b, w);
    }
View Full Code Here

TOP

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

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.