Examples of JSONMarshaller


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

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

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

        MyResponse two;
View Full Code Here

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

public class FakeArrayTest extends TestCase {

    public void testSimpleXmlTypeBean() throws Exception {
       
        final JSONJAXBContext ctx = new JSONJAXBContext(JSONConfiguration.mapped().arrays("color").build(), FakeArrayBean.class);
        final JSONMarshaller jm = ctx.createJSONMarshaller();
        final StringWriter sw = new StringWriter();

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

        jm.marshallToJSON(one, sw);
        String jsonResult = sw.toString();

        assertEquals("{\"weight\":[\"1kg\",\"2kg\"],\"color\":[\"red\"],\"name\":\"bumper\"}", jsonResult);
    }
View Full Code Here

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

            final Class<? extends Object> beanClass = originalBean.getClass();
            final Class<?>[] classesToBeBound = {beanClass};
            JSONJAXBContext context = properties.isEmpty() ?
                    new JSONJAXBContext(configuration, classesToBeBound) : new JSONJAXBContext(classesToBeBound, properties);

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

            printAsXml(originalBean);

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

            System.out.println(sWriter.toString());
            final Object actual = unmarshaller.unmarshalFromJSON(new StringReader(sWriter.toString()), beanClass);

            if (useDefaultConfiguration) {
View Full Code Here

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

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

        final StringWriter sw1 = new StringWriter();
        final JAXBContext jaxbContext = JAXBContext.newInstance(AnotherArrayTestBean.class);
        final Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(one, sw1);

        final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        final JAXBElement<AnotherArrayTestBean> unmarshal = unmarshaller.unmarshal(new StreamSource(new StringReader(sw1
                .toString())), AnotherArrayTestBean.class);

        marshaller.marshal(unmarshal, System.out);

        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

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

    }

    private void tryListWithConfiguration(JSONConfiguration configuration) throws Exception {

        final JSONJAXBContext ctx = new JSONJAXBContext(configuration, AnimalList.class, Animal.class, Dog.class, Cat.class);
        final JSONMarshaller jm = ctx.createJSONMarshaller();
        final JSONUnmarshaller ju = ctx.createJSONUnmarshaller();
        final StringWriter sw = new StringWriter();

        AnimalList two;
        jm.marshallToJSON(one, sw);

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

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

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

    }

    private void tryIndividualsWithConfiguration(JSONConfiguration configuration) throws Exception {

        final JSONJAXBContext ctx = new JSONJAXBContext(configuration, AnimalList.class, Animal.class, Dog.class, Cat.class);
        final JSONMarshaller jm = ctx.createJSONMarshaller();
        final JSONUnmarshaller ju = ctx.createJSONUnmarshaller();


        Animal animalTwo;

        for (int i = 0; i < one.animals.size(); i++) {

            final StringWriter sw = new StringWriter();
            Animal animalOne = one.animals.get(i);

            jm.marshallToJSON(animalOne, sw);

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

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

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

    @Override
    protected final void writeTo(JAXBElement<?> t, MediaType mediaType, Charset c,
            Marshaller m, OutputStream entityStream)
            throws JAXBException {

        JSONMarshaller jsonMarshaller = JSONJAXBContext.getJSONMarshaller(m, getStoredJAXBContext(t.getDeclaredType()));
        if(isFormattedOutput())
            jsonMarshaller.setProperty(JSONMarshaller.FORMATTED, true);
        jsonMarshaller.marshallToJSON(t, new OutputStreamWriter(entityStream, c));
    }
View Full Code Here

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

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.integerList = new ArrayList<Integer>() {{ add(4); }};
        one.number = 3;
       
        jm.marshallToJSON(one, sw);
        String jsonResult = sw.toString();
        System.out.println(jsonResult);
       
        String expectedResult = "{\"intArray\":[1],\"integerArray\":[2],\"integerList\":[4],\"number\":3}";
        assertEquals(expectedResult, jsonResult);
View Full Code Here

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, getStoredJAXBContext(t.getClass()));
        if(isFormattedOutput())
            jsonMarshaller.setProperty(JSONMarshaller.FORMATTED, true);
        jsonMarshaller.marshallToJSON(t, new OutputStreamWriter(entityStream, c));
    }
View Full Code Here

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

public class RegisterMessageTest extends TestCase {

    public void testRegisterMessage() throws Exception {

        final JSONJAXBContext ctx = new JSONJAXBContext(JSONConfiguration.mapped().rootUnwrapping(false).attributeAsElement("agentUID", "requestTime").nonStrings("requestTime").build(), RegisterMessage.class);
        final JSONMarshaller jm = ctx.createJSONMarshaller();
        final JSONUnmarshaller ju = ctx.createJSONUnmarshaller();
        final StringWriter sw = new StringWriter();

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

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

        two = ju.unmarshalFromJSON(new StringReader(sw.toString()), RegisterMessage.class);
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.