Search in sources :

Example 1 with JSONSerializerListener

use of org.apache.pivot.json.JSONSerializerListener in project pivot by apache.

the class JSONSerializerTest method testEquals.

@Test
public void testEquals() throws IOException, SerializationException {
    JSONSerializer jsonSerializer = new JSONSerializer();
    JSONSerializerListener jsonSerializerListener = new JSONSerializerListener() {

        @Override
        public void beginDictionary(JSONSerializer jsonSerializerArgument, Dictionary<String, ?> value) {
            System.out.println("Begin dictionary: " + value);
        }

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

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

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

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

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

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

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

        @Override
        public void readNull(JSONSerializer jsonSerializerArgument) {
            System.out.println("Read null");
        }
    };
    jsonSerializer.getJSONSerializerListeners().add(jsonSerializerListener);
    Object o1 = jsonSerializer.readObject(getClass().getResourceAsStream("map.json"));
    assertEquals((Integer) JSON.get(o1, "a"), (Integer) 100);
    assertEquals(JSON.get(o1, "b"), "Hello");
    assertEquals(JSON.get(o1, "c"), false);
    assertEquals((Integer) JSON.get(o1, "e.g"), (Integer) 5);
    assertEquals((Integer) JSON.get(o1, "i.a"), (Integer) 200);
    assertEquals(JSON.get(o1, "i.c"), true);
    assertEquals(JSON.get(o1, "m"), "Hello\r\n\tWorld!");
    jsonSerializer.getJSONSerializerListeners().remove(jsonSerializerListener);
    Object o2 = jsonSerializer.readObject(getClass().getResourceAsStream("map.json"));
    assertEquals((Integer) JSON.get(o2, "k[1].a"), (Integer) 10);
    assertEquals((Integer) JSON.get(o2, "k[2].a"), (Integer) 100);
    assertEquals((Integer) JSON.get(o2, "k[2].b"), (Integer) 200);
    assertEquals(JSON.get(o2, "k[2].c"), "300");
    assertEquals((Integer) JSON.get(o2, "j"), (Integer) 200);
    assertEquals(JSON.get(o2, "n"), "This is a \"test\" of the 'quoting' in \\JSON\\");
    assertTrue(o1.equals(o2));
    List<?> d = JSON.get(o1, "d");
    d.remove(0, 1);
    assertFalse(o1.equals(o2));
}
Also used : Dictionary(org.apache.pivot.collections.Dictionary) Sequence(org.apache.pivot.collections.Sequence) JSONSerializerListener(org.apache.pivot.json.JSONSerializerListener) JSONSerializer(org.apache.pivot.json.JSONSerializer) Test(org.junit.Test)

Aggregations

Dictionary (org.apache.pivot.collections.Dictionary)1 Sequence (org.apache.pivot.collections.Sequence)1 JSONSerializer (org.apache.pivot.json.JSONSerializer)1 JSONSerializerListener (org.apache.pivot.json.JSONSerializerListener)1 Test (org.junit.Test)1