Search in sources :

Example 16 with JSONSerializer

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

the class BindTest method testTypedList.

/**
 * Tests returning a typed list using
 * {@code org.apache.pivot.util.TypeLiteral}.
 *
 * @throws IOException
 * @throws SerializationException
 */
@Test
public void testTypedList() throws IOException, SerializationException {
    JSONSerializer listSerializer = new JSONSerializer();
    @SuppressWarnings("unchecked") List<Object> list = (List<Object>) listSerializer.readObject(getClass().getResourceAsStream("list.json"));
    JSONSerializer typedListSerializer = new JSONSerializer((new TypeLiteral<ArrayList<SampleBean2>>() {
    }).getType());
    @SuppressWarnings("unchecked") ArrayList<SampleBean2> typedList = (ArrayList<SampleBean2>) typedListSerializer.readObject(getClass().getResourceAsStream("list.json"));
    Object item0 = typedList.get(0);
    assertTrue(item0 instanceof SampleBean2);
    assertEquals((Integer) typedList.get(0).getA(), (Integer) JSON.get(list, "[0].a"));
}
Also used : ArrayList(org.apache.pivot.collections.ArrayList) ArrayList(org.apache.pivot.collections.ArrayList) List(org.apache.pivot.collections.List) JSONSerializer(org.apache.pivot.json.JSONSerializer) Test(org.junit.Test)

Example 17 with JSONSerializer

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

the class BindTest method testTypedMap.

/**
 * Tests returning a typed map using
 * {@code org.apache.pivot.util.TypeLiteral}.
 *
 * @throws IOException
 * @throws SerializationException
 */
@Test
public void testTypedMap() throws IOException, SerializationException {
    JSONSerializer typedMapSerializer = new JSONSerializer((new TypeLiteral<HashMap<String, SampleBean2>>() {
    }).getType());
    @SuppressWarnings("unchecked") 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");
}
Also used : HashMap(org.apache.pivot.collections.HashMap) StringReader(java.io.StringReader) JSONSerializer(org.apache.pivot.json.JSONSerializer) Test(org.junit.Test)

Example 18 with JSONSerializer

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

the class BindTest method testDictionary.

/**
 * Tests returning a class that implements
 * {@code org.apache.pivot.collections.Dictionary}.
 *
 * @throws IOException
 * @throws SerializationException
 */
@Test
public void testDictionary() throws IOException, SerializationException {
    JSONSerializer dictionarySerializer = new JSONSerializer(SampleBean2DictionarySubclass.class);
    SampleBean2Dictionary dictionary = (SampleBean2Dictionary) dictionarySerializer.readObject(new StringReader("{foo: {a:1, b:2, c:'3'}}"));
    assertTrue(JSON.get(dictionary, "foo") instanceof SampleBean2);
    assertEquals(JSON.get(dictionary, "foo.c"), "3");
}
Also used : StringReader(java.io.StringReader) JSONSerializer(org.apache.pivot.json.JSONSerializer) Test(org.junit.Test)

Example 19 with JSONSerializer

use of org.apache.pivot.json.JSONSerializer 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)

Example 20 with JSONSerializer

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

the class BeanAdapterTest method setUp.

@Before
public void setUp() {
    srcTest = new BeanAdapterSampleObject();
    jsonSerializer = new JSONSerializer(BeanAdapterSampleObject.class);
    writer = new StringWriter();
}
Also used : StringWriter(java.io.StringWriter) JSONSerializer(org.apache.pivot.json.JSONSerializer) Before(org.junit.Before)

Aggregations

JSONSerializer (org.apache.pivot.json.JSONSerializer)25 IOException (java.io.IOException)11 Test (org.junit.Test)11 SerializationException (org.apache.pivot.serialization.SerializationException)8 StringReader (java.io.StringReader)6 ArrayList (org.apache.pivot.collections.ArrayList)5 HashMap (org.apache.pivot.collections.HashMap)5 List (org.apache.pivot.collections.List)5 File (java.io.File)4 InputStream (java.io.InputStream)4 URL (java.net.URL)3 Map (org.apache.pivot.collections.Map)3 QueryException (org.apache.pivot.web.QueryException)3 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 Color (java.awt.Color)1 StringWriter (java.io.StringWriter)1 MalformedURLException (java.net.MalformedURLException)1 BXMLSerializer (org.apache.pivot.beans.BXMLSerializer)1 BeanAdapter (org.apache.pivot.beans.BeanAdapter)1