Package org.apache.pivot.json

Examples of org.apache.pivot.json.JSONSerializer.readObject()


     * @throws SerializationException
     */
    @Test
    public void testUntypedList() throws IOException, SerializationException {
        JSONSerializer listSerializer = new JSONSerializer(ArrayList.class);
        List<?> list = (List<?>)listSerializer.readObject(new StringReader("[1, 2, 3, 4, 5]"));
        assertEquals(list.get(0), 1);
    }

    /**
     * Tests returning a typed list using {@code org.apache.pivot.util.TypeLiteral}.
View Full Code Here


    @Test
    @SuppressWarnings("unchecked")
    public void testTypedList() throws IOException, SerializationException {
        JSONSerializer listSerializer = new JSONSerializer();
        List<Object> list =
            (List<Object>)listSerializer.readObject(getClass().getResourceAsStream("list.json"));

        JSONSerializer typedListSerializer =
            new JSONSerializer((new TypeLiteral<ArrayList<SampleBean2>>() {
                // empty block
            }).getType());
View Full Code Here

        JSONSerializer typedListSerializer =
            new JSONSerializer((new TypeLiteral<ArrayList<SampleBean2>>() {
                // empty block
            }).getType());
        ArrayList<SampleBean2> typedList =
            (ArrayList<SampleBean2>)typedListSerializer.readObject(getClass().getResourceAsStream("list.json"));

        Object item0 = typedList.get(0);
        assertTrue(item0 instanceof SampleBean2);
        assertEquals(typedList.get(0).getA(), JSON.get(list, "[0].a"));
    }
View Full Code Here

    @Test
    @SuppressWarnings("unchecked")
    public void testListSubclass() throws IOException, SerializationException {
        JSONSerializer listSerializer = new JSONSerializer();
        List<Object> list =
            (List<Object>)listSerializer.readObject(getClass().getResourceAsStream("list.json"));

        JSONSerializer typedListSerializer = new JSONSerializer(SampleBean2ListSubclass.class);
        SampleBean2List typedList =
            (SampleBean2List)typedListSerializer.readObject(getClass().getResourceAsStream("list.json"));
View Full Code Here

        List<Object> list =
            (List<Object>)listSerializer.readObject(getClass().getResourceAsStream("list.json"));

        JSONSerializer typedListSerializer = new JSONSerializer(SampleBean2ListSubclass.class);
        SampleBean2List typedList =
            (SampleBean2List)typedListSerializer.readObject(getClass().getResourceAsStream("list.json"));

        Object item0 = typedList.get(0);
        assertTrue(item0 instanceof SampleBean2);
        assertEquals(typedList.get(0).getA(), JSON.get(list, "[0].a"));
    }
View Full Code Here

    @Test
    @SuppressWarnings("unchecked")
    public void testSequence() throws IOException, SerializationException {
        JSONSerializer listSerializer = new JSONSerializer();
        List<Object> list =
            (List<Object>)listSerializer.readObject(getClass().getResourceAsStream("list.json"));

        JSONSerializer sequenceSerializer = new JSONSerializer(SampleBean2SequenceSubclass.class);
        SampleBean2Sequence sequence =
            (SampleBean2Sequence)sequenceSerializer.readObject(getClass().getResourceAsStream("list.json"));
View Full Code Here

        List<Object> list =
            (List<Object>)listSerializer.readObject(getClass().getResourceAsStream("list.json"));

        JSONSerializer sequenceSerializer = new JSONSerializer(SampleBean2SequenceSubclass.class);
        SampleBean2Sequence sequence =
            (SampleBean2Sequence)sequenceSerializer.readObject(getClass().getResourceAsStream("list.json"));

        Object item0 = sequence.get(0);
        assertNotNull(item0);
        // assertTrue(item0 instanceof SampleBean2);  // true but superfluous
        assertEquals(sequence.get(0).getA(), JSON.get(list, "[0].a"));
View Full Code Here

     */
    @Test
    @SuppressWarnings("unchecked")
    public void testUntypedMap() throws IOException, SerializationException {
        JSONSerializer mapSerializer = new JSONSerializer(HashMap.class);
        HashMap<String, ?> map = (HashMap<String, ?>)mapSerializer.readObject(new StringReader("{a:1, b:2, c:'3'}"));
        assertEquals(map.get("a"), 1);
    }

    /**
     * Tests returning a typed map using {@code org.apache.pivot.util.TypeLiteral}.
View Full Code Here

            new JSONSerializer((new TypeLiteral<HashMap<String, SampleBean2>>() {
                // empty block
            }).getType());

        HashMap<String, SampleBean2> map =
            (HashMap<String, SampleBean2>)typedMapSerializer.readObject(new StringReader("{foo: {a:1, b:2, c:'3'}}"));

        assertTrue(JSON.get(map, "foo") instanceof SampleBean2);
        assertEquals(JSON.get(map, "foo.c"), "3");
    }
View Full Code Here

    @Test
    public void testMapSubclass() throws IOException, SerializationException {
        JSONSerializer typedMapSerializer = new JSONSerializer(SampleBean2MapSubclass.class);

        SampleBean2Map map =
            (SampleBean2Map)typedMapSerializer.readObject(new StringReader("{foo: {a:1, b:2, c:'3'}}"));

        assertTrue(JSON.get(map, "foo") instanceof SampleBean2);
        assertEquals(JSON.get(map, "foo.c"), "3");
    }
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.