Package com.sun.jersey.api.json

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


    @Override
    protected void writeTo(Object t, MediaType mediaType, Charset c,
            Marshaller m, OutputStream entityStream)
            throws JAXBException {
        JSONMarshaller jsonMarshaller = JSONJAXBContext.getJSONMarshaller(m);
        if(isFormattedOutput())
            jsonMarshaller.setProperty(JSONMarshaller.FORMATTED, true);
        jsonMarshaller.marshallToJSON(t, new OutputStreamWriter(entityStream, c));
    }
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);
        Cat c2 = new Cat("Bar", "Puss");
        one.addCat(c2);
       
        one.setProp("testProp");
       
        jm.marshallToJSON(one, sw);
        String jsonResult = sw.toString();
        System.out.println(jsonResult);
       
        String excpectedResult = "{\"cats\":[{\"name\":\"Foo\",\"nickName\":\"Kitty\"},{\"name\":\"Bar\",\"nickName\":\"Puss\"}],\"prop\":\"testProp\"}";
        assertEquals(excpectedResult, jsonResult);
View Full Code Here

public class IntArrayTest extends TestCase {

    public void testBracketsAreNotMissingAndNumbersAreNotQuoted() throws Exception {
       
        final JSONJAXBContext ctx = new JSONJAXBContext(JSONConfiguration.natural().build(), IntArray.class);
        final JSONMarshaller jm = ctx.createJSONMarshaller();
        final StringWriter sw = new StringWriter();

        IntArray one = new IntArray();
        one.intArray = new int[] {1};
        one.integerArray = new Integer[] {2};
        one.number = 3;
       
        jm.marshallToJSON(one, sw);
        String jsonResult = sw.toString();
        System.out.println(jsonResult);
       
        String expectedResult = "{\"intArray\":[1].\"integerArray\":[2],\"number\":3}";
        assertEquals(expectedResult, jsonResult);
View Full Code Here

    }

    private void tryConfiguration(JSONConfiguration configuration) throws Exception {

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


        NamespaceBean beanTwo;

        final StringWriter sw = new StringWriter();

        jm.marshallToJSON(one, sw);

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

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

        _testNamespaces(ctx);
    }

    public void _testNamespaces(JSONJAXBContext ctx) throws Exception {

        final JSONMarshaller jm =  ctx.createJSONMarshaller();
        final JSONUnmarshaller ju = ctx.createJSONUnmarshaller();
        final StringWriter sw = new StringWriter();

        final MyResponse one = JSONTestHelper.createTestInstance(MyResponse.class);

        jm.marshallToJSON(one, sw);

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

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

        allBeansTest(new JSONJAXBContext(classes, props), beans);
    }
   
    public synchronized void allBeansTest(JSONJAXBContext context, Collection<Object> beans) throws Exception {

        JSONMarshaller marshaller = context.createJSONMarshaller();
        JSONUnmarshaller unmarshaller = context.createJSONUnmarshaller();

        for (Object originalBean : beans) {
            printAsXml(originalBean);

            StringWriter sWriter = new StringWriter();
            marshaller.marshallToJSON(originalBean, sWriter);

            System.out.println(sWriter.toString());
            assertEquals(originalBean, unmarshaller.unmarshalFromJSON(new StringReader(sWriter.toString()), originalBean.getClass()));
        }
    }
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.