use of org.activityinfo.json.JsonValue 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.json.JsonValue 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.json.JsonValue 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));
}
use of org.activityinfo.json.JsonValue in project activityinfo by bedatadriven.
the class MySqlRecordHistoryBuilder method queryLabels.
private List<String> queryLabels(ReferenceType type, ReferenceValue value) {
Map<ResourceId, String> labelMap = new HashMap<>();
for (ResourceId formId : type.getRange()) {
Optional<FormStorage> form = catalog.getForm(formId);
if (form.isPresent()) {
Optional<ResourceId> labelFieldId = findLabelField(form.get().getFormClass());
for (RecordRef ref : value.getReferences()) {
Optional<FormRecord> record = form.get().get(ref.getRecordId());
if (record.isPresent()) {
JsonValue labelValue = Json.createNull();
if (labelFieldId.isPresent()) {
labelValue = record.get().getFields().get(labelFieldId.get().asString());
}
if (labelValue.isJsonNull()) {
labelValue = record.get().getFields().get(CuidAdapter.field(formId, CuidAdapter.NAME_FIELD).asString());
}
if (labelValue.isJsonPrimitive()) {
labelMap.put(ref.getRecordId(), labelValue.asString());
}
}
}
}
}
List<String> list = new ArrayList<>();
for (RecordRef ref : value.getReferences()) {
String label = labelMap.get(ref.getRecordId());
if (label == null) {
list.add(ref.toQualifiedString());
} else {
list.add(label);
}
}
return list;
}
use of org.activityinfo.json.JsonValue in project activityinfo by bedatadriven.
the class FormConverter method fromPropertyValue.
public static JsonValue fromPropertyValue(Object propertyValue) {
if (propertyValue == null) {
return Json.createNull();
} else if (propertyValue instanceof EmbeddedEntity) {
return fromEmbeddedEntity(((EmbeddedEntity) propertyValue));
} else if (propertyValue instanceof List) {
List<Object> propertyValueList = (List<Object>) propertyValue;
JsonValue convertedList = Json.createArray();
for (Object propertyValueListItem : propertyValueList) {
convertedList.add(fromPropertyValue(propertyValueListItem));
}
return convertedList;
} else if (propertyValue instanceof String) {
return Json.create((String) propertyValue);
} else if (propertyValue instanceof Number) {
return Json.create(((Number) propertyValue).doubleValue());
} else if (propertyValue instanceof Boolean) {
return Json.create((Boolean) propertyValue);
} else {
throw new UnsupportedOperationException("type: " + propertyValue.getClass().getName());
}
}
Aggregations