Search in sources :

Example 1 with JSONSerializer

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

the class ListView method setListData.

/**
 * Sets the list data.
 *
 * @param listData A URL referring to a JSON file containing the data to be
 * presented by the list view.
 * @throws IllegalArgumentException if the list data URL is {@code null}
 * or if there is any kind of error trying to retrieve the list data from
 * that location.
 */
public void setListData(URL listData) {
    Utils.checkNull(listData, "listData");
    JSONSerializer jsonSerializer = new JSONSerializer();
    try {
        setListData((List<?>) jsonSerializer.readObject(listData.openStream()));
    } catch (SerializationException exception) {
        throw new IllegalArgumentException(exception);
    } catch (IOException exception) {
        throw new IllegalArgumentException(exception);
    }
}
Also used : SerializationException(org.apache.pivot.serialization.SerializationException) IOException(java.io.IOException) JSONSerializer(org.apache.pivot.json.JSONSerializer)

Example 2 with JSONSerializer

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

the class TableView method setTableData.

/**
 * Sets the table data.
 *
 * @param tableData A URL referring to a JSON file containing the data to be
 * presented by the table view.
 */
public void setTableData(URL tableData) {
    Utils.checkNull(tableData, "URL for table data");
    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);
    }
}
Also used : SerializationException(org.apache.pivot.serialization.SerializationException) IOException(java.io.IOException) JSONSerializer(org.apache.pivot.json.JSONSerializer)

Example 3 with JSONSerializer

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

the class BindTest method testUntypedList.

/**
 * Tests returning an untyped list.
 *
 * @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);
}
Also used : StringReader(java.io.StringReader) ArrayList(org.apache.pivot.collections.ArrayList) List(org.apache.pivot.collections.List) JSONSerializer(org.apache.pivot.json.JSONSerializer) Test(org.junit.Test)

Example 4 with JSONSerializer

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

the class BindTest method testListSubclass.

/**
 * Tests returning a subclass of a generic
 * {@code org.apache.pivot.collections.List}.
 *
 * @throws IOException
 * @throws SerializationException
 */
@Test
public void testListSubclass() throws IOException, SerializationException {
    JSONSerializer listSerializer = new JSONSerializer();
    @SuppressWarnings("unchecked") 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((Integer) typedList.get(0).getA(), (Integer) JSON.get(list, "[0].a"));
}
Also used : ArrayList(org.apache.pivot.collections.ArrayList) List(org.apache.pivot.collections.List) JSONSerializer(org.apache.pivot.json.JSONSerializer) Test(org.junit.Test)

Example 5 with JSONSerializer

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

the class BindTest method testSequence.

/**
 * Tests returning a class that implements
 * {@code org.apache.pivot.collections.Sequence}.
 *
 * @throws IOException
 * @throws SerializationException
 */
@Test
public void testSequence() throws IOException, SerializationException {
    JSONSerializer listSerializer = new JSONSerializer();
    @SuppressWarnings("unchecked") 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((Integer) sequence.get(0).getA(), (Integer) JSON.get(list, "[0].a"));
}
Also used : ArrayList(org.apache.pivot.collections.ArrayList) List(org.apache.pivot.collections.List) JSONSerializer(org.apache.pivot.json.JSONSerializer) Test(org.junit.Test)

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