Search in sources :

Example 1 with JaxRsJsonReader

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()));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) FormClass(org.activityinfo.model.form.FormClass) JsonValue(org.activityinfo.json.JsonValue) JaxRsJsonReader(org.activityinfo.server.endpoint.rest.JaxRsJsonReader) FormClass(org.activityinfo.model.form.FormClass) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Annotation(java.lang.annotation.Annotation) Test(org.junit.Test)

Example 2 with JaxRsJsonReader

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"));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) JsonValue(org.activityinfo.json.JsonValue) JaxRsJsonReader(org.activityinfo.server.endpoint.rest.JaxRsJsonReader) FormClass(org.activityinfo.model.form.FormClass) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Annotation(java.lang.annotation.Annotation) Test(org.junit.Test)

Example 3 with JaxRsJsonReader

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));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) JsonValue(org.activityinfo.json.JsonValue) JaxRsJsonReader(org.activityinfo.server.endpoint.rest.JaxRsJsonReader) FormClass(org.activityinfo.model.form.FormClass) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Annotation(java.lang.annotation.Annotation) Test(org.junit.Test)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Annotation (java.lang.annotation.Annotation)3 JsonValue (org.activityinfo.json.JsonValue)3 FormClass (org.activityinfo.model.form.FormClass)3 JaxRsJsonReader (org.activityinfo.server.endpoint.rest.JaxRsJsonReader)3 Test (org.junit.Test)3