use of org.activityinfo.server.endpoint.rest.JaxRsJsonReader in project activityinfo by bedatadriven.
the class JaxRsJsonReaderTest method modelClass.
@Test
public void modelClass() throws IOException {
FormClass formClass = new FormClass(ResourceId.valueOf("FORM"));
formClass.setLabel("My Form");
formClass.addField(ResourceId.valueOf("F1")).setLabel("My field").setRequired(true).setType(TextType.SIMPLE).setCode("MY");
JaxRsJsonReader reader = new JaxRsJsonReader();
assertTrue(reader.isReadable(FormClass.class, FormClass.class, new Annotation[0], MediaType.APPLICATION_JSON_TYPE));
assertTrue(reader.isWriteable(FormClass.class, FormClass.class, new Annotation[0], MediaType.APPLICATION_JSON_TYPE));
ByteArrayOutputStream entity = new ByteArrayOutputStream();
reader.writeTo(formClass, formClass.getClass(), formClass.getClass(), new Annotation[0], MediaType.APPLICATION_JSON_TYPE, EMPTY_HEADERS, entity);
JsonValue jsonObject = Json.parse(new String(entity.toByteArray(), Charsets.UTF_8));
System.out.println(Json.stringify(jsonObject));
FormClass reformClass = (FormClass) reader.readFrom((Class) FormClass.class, FormClass.class, new Annotation[0], MediaType.APPLICATION_JSON_TYPE, EMPTY_HEADERS, new ByteArrayInputStream(entity.toByteArray()));
}
use of org.activityinfo.server.endpoint.rest.JaxRsJsonReader in project activityinfo by bedatadriven.
the class JaxRsJsonReaderTest method jsonValue.
@Test
public void jsonValue() throws IOException {
JaxRsJsonReader reader = new JaxRsJsonReader();
assertTrue(reader.isReadable(JsonValue.class, JsonValue.class, new Annotation[0], MediaType.APPLICATION_JSON_TYPE));
assertTrue(reader.isWriteable(JsonValue.class, JsonValue.class, new Annotation[0], MediaType.APPLICATION_JSON_TYPE));
JsonValue object = Json.createObject();
object.put("b", true);
object.put("s", "hello WOrld");
object.put("i", 42);
ByteArrayOutputStream entity = new ByteArrayOutputStream();
reader.writeTo(object, object.getClass(), object.getClass(), new Annotation[0], MediaType.APPLICATION_JSON_TYPE, EMPTY_HEADERS, entity);
JsonValue reobject = (JsonValue) reader.readFrom((Class) JsonValue.class, JsonValue.class, new Annotation[0], MediaType.APPLICATION_JSON_TYPE, EMPTY_HEADERS, new ByteArrayInputStream(entity.toByteArray()));
assertThat(reobject.getBoolean("b"), equalTo(true));
assertThat(reobject.getNumber("i"), equalTo(42.0));
assertThat(reobject.getString("s"), equalTo("hello WOrld"));
}
use of org.activityinfo.server.endpoint.rest.JaxRsJsonReader in project activityinfo by bedatadriven.
the class JaxRsJsonReaderTest method jsTypeTest.
@Test
public void jsTypeTest() throws IOException {
JaxRsJsonReader reader = new JaxRsJsonReader();
assertTrue(reader.isReadable(DummyJsObject.class, DummyJsObject.class, new Annotation[0], MediaType.APPLICATION_JSON_TYPE));
assertTrue(reader.isWriteable(DummyJsObject.class, DummyJsObject.class, new Annotation[0], MediaType.APPLICATION_JSON_TYPE));
DummyJsObject object = new DummyJsObject();
object.b = true;
object.s = "Hello world";
object.i = 99;
ByteArrayOutputStream entity = new ByteArrayOutputStream();
reader.writeTo(object, object.getClass(), object.getClass(), new Annotation[0], MediaType.APPLICATION_JSON_TYPE, EMPTY_HEADERS, entity);
JsonValue jsonValue = Json.parse(new String(entity.toByteArray(), Charsets.UTF_8));
assertThat(jsonValue.getString("s"), equalTo("Hello world"));
DummyJsObject reobject = (DummyJsObject) reader.readFrom((Class) DummyJsObject.class, DummyJsObject.class, new Annotation[0], MediaType.APPLICATION_JSON_TYPE, EMPTY_HEADERS, new ByteArrayInputStream(entity.toByteArray()));
assertThat(reobject.b, equalTo(object.b));
assertThat(reobject.d, equalTo(object.d));
assertThat(reobject.s, equalTo(object.s));
}
Aggregations