Search in sources :

Example 11 with JSONSerializer

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

the class JSONViewer method startup.

@Override
public void startup(Display display, Map<String, String> properties) throws Exception {
    BXMLSerializer bxmlSerializer = new BXMLSerializer();
    bxmlSerializer.getNamespace().put(APPLICATION_KEY, this);
    window = (Window) bxmlSerializer.readObject(JSONViewer.class, "json_viewer.bxml");
    bxmlSerializer.bind(this);
    Label prompt = new Label("Drag or paste JSON here");
    prompt.getStyles().put(Style.horizontalAlignment, HorizontalAlignment.CENTER);
    prompt.getStyles().put(Style.verticalAlignment, VerticalAlignment.CENTER);
    promptDecorator.setOverlay(prompt);
    treeView.getDecorators().add(promptDecorator);
    window.setTitle(WINDOW_TITLE);
    window.open(display);
    window.requestFocus();
    if (System.in.available() > 0) {
        JSONSerializer jsonSerializer = new JSONSerializer();
        try {
            setValue(jsonSerializer.readObject(System.in));
        } catch (Exception exception) {
        // No-op
        }
    }
}
Also used : Label(org.apache.pivot.wtk.Label) BXMLSerializer(org.apache.pivot.beans.BXMLSerializer) IOException(java.io.IOException) JSONSerializer(org.apache.pivot.json.JSONSerializer)

Example 12 with JSONSerializer

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

the class RESTDemoTest method testCRUD.

@Test
public void testCRUD() throws IOException, SerializationException, QueryException {
    JSONSerializer jsonSerializer = new JSONSerializer();
    Object contact = jsonSerializer.readObject(getClass().getResourceAsStream("contact.json"));
    // Create
    PostQuery postQuery = new PostQuery(hostname, port, "/pivot-demos/rest_demo", secure);
    postQuery.setValue(contact);
    URL location = postQuery.execute();
    assertNotNull(location);
    String path = location.getPath();
    // Read
    GetQuery getQuery = new GetQuery(hostname, port, path, secure);
    Object result = getQuery.execute();
    assertArrayEquals((Object[]) JSON.get(contact, "address.street"), (Object[]) JSON.get(result, "address.street"));
    assertEquals(contact, result);
    // Update
    JSON.put(contact, "name", "Joseph User");
    PutQuery putQuery = new PutQuery(hostname, port, path, secure);
    putQuery.setValue(contact);
    boolean created = putQuery.execute();
    assertFalse(created);
    assertEquals(contact, getQuery.execute());
    // Delete
    DeleteQuery deleteQuery = new DeleteQuery(hostname, port, path, secure);
    deleteQuery.execute();
    assertEquals(deleteQuery.getStatus(), Query.Status.NO_CONTENT);
}
Also used : GetQuery(org.apache.pivot.web.GetQuery) PostQuery(org.apache.pivot.web.PostQuery) DeleteQuery(org.apache.pivot.web.DeleteQuery) PutQuery(org.apache.pivot.web.PutQuery) URL(java.net.URL) JSONSerializer(org.apache.pivot.json.JSONSerializer) Test(org.junit.Test)

Example 13 with JSONSerializer

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

the class BindTest method testUntypedMap.

/**
 * Tests returning an untyped map.
 *
 * @throws IOException
 * @throws SerializationException
 */
@Test
public void testUntypedMap() throws IOException, SerializationException {
    JSONSerializer mapSerializer = new JSONSerializer(HashMap.class);
    @SuppressWarnings("unchecked") HashMap<String, ?> map = (HashMap<String, ?>) mapSerializer.readObject(new StringReader("{a:1, b:2, c:'3'}"));
    assertEquals(map.get("a"), 1);
}
Also used : HashMap(org.apache.pivot.collections.HashMap) StringReader(java.io.StringReader) JSONSerializer(org.apache.pivot.json.JSONSerializer) Test(org.junit.Test)

Example 14 with JSONSerializer

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

the class BindTest method testBean.

/**
 * Tests returning a Java bean value.
 *
 * @throws IOException
 * @throws SerializationException
 */
@Test
public void testBean() throws IOException, SerializationException {
    JSONSerializer mapSerializer = new JSONSerializer();
    @SuppressWarnings("unchecked") Map<String, Object> map = (Map<String, Object>) mapSerializer.readObject(getClass().getResourceAsStream("map.json"));
    JSONSerializer beanSerializer = new JSONSerializer(SampleBean1.class);
    SampleBean1 typedMap = (SampleBean1) beanSerializer.readObject(getClass().getResourceAsStream("map.json"));
    assertEquals((Integer) typedMap.getA(), (Integer) JSON.get(map, "a"));
    assertEquals(typedMap.getB(), JSON.get(map, "b"));
    assertEquals(typedMap.getC(), JSON.get(map, "c"));
    assertEquals(typedMap.getD(), JSON.get(map, "d"));
    assertEquals(typedMap.getE(), JSON.get(map, "e"));
    assertEquals((Integer) typedMap.getI().getA(), (Integer) JSON.get(map, "i.a"));
    Object k0 = typedMap.getK().get(0);
    assertTrue(k0 instanceof SampleBean2);
    assertEquals((Integer) typedMap.getK().get(0).getA(), (Integer) JSON.get(map, "k[0].a"));
}
Also used : Map(org.apache.pivot.collections.Map) HashMap(org.apache.pivot.collections.HashMap) JSONSerializer(org.apache.pivot.json.JSONSerializer) Test(org.junit.Test)

Example 15 with JSONSerializer

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

the class BindTest method testMapSubclass.

/**
 * Tests returning a subclass of a generic
 * {@code org.apache.pivot.collections.Map}.
 *
 * @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");
}
Also used : StringReader(java.io.StringReader) 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