Package org.apache.pivot.json

Examples of org.apache.pivot.json.JSONSerializer


        try {
            InputStream inputStream = location.openStream();

            try {
                JSONSerializer serializer = new JSONSerializer();
                Map<String, ?> properties = (Map<String, ?>)serializer.readObject(inputStream);

                font = Font.decode((String)properties.get("font"));

                List<String> colorCodes = (List<String>)properties.get("colors");
                numberOfPaletteColors = colorCodes.getLength();
View Full Code Here


    public void setTableData(URL tableData) {
        if (tableData == null) {
            throw new IllegalArgumentException("tableData is null.");
        }

        JSONSerializer jsonSerializer = new JSONSerializer();

        try {
            setTableData((List<?>)jsonSerializer.readObject(tableData.openStream()));
        } catch (SerializationException exception) {
            throw new IllegalArgumentException(exception);
        } catch (IOException exception) {
            throw new IllegalArgumentException(exception);
        }
View Full Code Here

        JSONSerializer.toString(Double.POSITIVE_INFINITY);
    }

    @Test
    public void testEquals() throws IOException, SerializationException {
        JSONSerializer jsonSerializer = new JSONSerializer();
        JSONSerializerListener jsonSerializerListener = new JSONSerializerListener() {
            @Override
            public void beginDictionary(JSONSerializer jsonSerializer, Dictionary<String, ?> value) {
                System.out.println("Begin dictionary: " + value);
            }

            @Override
            public void endDictionary(JSONSerializer jsonSerializer) {
                System.out.println("End dictionary");
            }

            @Override
            public void readKey(JSONSerializer jsonSerializer, String key) {
                System.out.println("Read key: " + key);
            }

            @Override
            public void beginSequence(JSONSerializer jsonSerializer, Sequence<?> value) {
                System.out.println("Begin sequence: " + value);
            }

            @Override
            public void endSequence(JSONSerializer jsonSerializer) {
                System.out.println("End sequence");
            }

            @Override
            public void readString(JSONSerializer jsonSerializer, String value) {
                System.out.println("Read string: " + value);
            }

            @Override
            public void readNumber(JSONSerializer jsonSerializer, Number value) {
                System.out.println("Read number: " + value);
            }

            @Override
            public void readBoolean(JSONSerializer jsonSerializer, Boolean value) {
                System.out.println("Read boolean: " + value);
            }

            @Override
            public void readNull(JSONSerializer jsonSerializer) {
                System.out.println("Read null");
            }
        };

        jsonSerializer.getJSONSerializerListeners().add(jsonSerializerListener);
        Object o1 = jsonSerializer.readObject(getClass().getResourceAsStream("map.json"));
        assertEquals(JSON.get(o1, "count"), 8);

        jsonSerializer.getJSONSerializerListeners().remove(jsonSerializerListener);
        Object o2 = jsonSerializer.readObject(getClass().getResourceAsStream("map.json"));

        assertTrue(o1.equals(o2));

        List<?> d = JSON.get(o1, "d");
        d.remove(0, 1);
View Full Code Here

     * @throws IOException
     * @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);
    }
View Full Code Here

     * @throws SerializationException
     */
    @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>>() {}).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

     * @throws SerializationException
     */
    @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"));

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

     * @throws SerializationException
     */
    @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"));

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

     * @throws SerializationException
     */
    @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);
    }
View Full Code Here

     * @throws SerializationException
     */
    @Test
    @SuppressWarnings("unchecked")
    public void testTypedMap() throws IOException, SerializationException {
        JSONSerializer typedMapSerializer =
            new JSONSerializer((new TypeLiteral<HashMap<String, SampleBean2>>() {}).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

     * @throws IOException
     * @throws SerializationException
     */
    @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

Related Classes of org.apache.pivot.json.JSONSerializer

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.