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
}
}
}
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);
}
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);
}
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"));
}
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");
}
Aggregations