use of org.sagebionetworks.schema.adapter.org.json.JSONObjectAdapterImpl in project Synapse-Repository-Services by Sage-Bionetworks.
the class MessageUtils method extractMessageBody.
/**
* Extract a ChangeMessage from an Amazon Message
* @param message
* @return
*/
public static ChangeMessage extractMessageBody(Message message) {
if (message == null)
throw new IllegalArgumentException("Message cannot be null");
try {
JSONObject object = new JSONObject(message.getBody());
if (object.has("objectId")) {
// This is a message pushed directly to a queue
JSONObjectAdapterImpl adapter = new JSONObjectAdapterImpl(object);
return new ChangeMessage(adapter);
}
if (object.has("TopicArn") && object.has("Message")) {
// This is a message that was pushed to a topic then forwarded to a queue.
JSONObject innerObject = new JSONObject(object.getString("Message"));
JSONObjectAdapterImpl adapter = new JSONObjectAdapterImpl(innerObject);
return new ChangeMessage(adapter);
} else {
throw new IllegalArgumentException("Unknown message type: " + message.getBody());
}
} catch (JSONException e) {
throw new RuntimeException(e);
} catch (JSONObjectAdapterException e) {
throw new RuntimeException(e);
}
}
use of org.sagebionetworks.schema.adapter.org.json.JSONObjectAdapterImpl in project Synapse-Repository-Services by Sage-Bionetworks.
the class NodeTranslationUtils method objectToBytes.
/**
* Convert an object to bytes.
*
* @param encoding
* @param annos
* @param value
*/
public static byte[] objectToBytes(Object value, ObjectSchema schema) {
if (value == null)
throw new IllegalArgumentException("Value cannot be null");
if (schema == null)
throw new IllegalArgumentException("Schema cannot be null");
// Is this a string
if (TYPE.STRING == schema.getType()) {
// String are stored as UTF-8 bytes
try {
// String are stored as UTF-8 bytes
return ((String) value).getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
} else if (TYPE.OBJECT == schema.getType()) {
// Extract the JSON string from this entity
JSONEntity valueEntity = (JSONEntity) value;
// Get the JSONString
try {
String jsonString = EntityFactory.createJSONStringForEntity(valueEntity);
// Save the UTF-8 bytes of this string
return jsonString.getBytes("UTF-8");
} catch (JSONObjectAdapterException e) {
throw new RuntimeException(e);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
} else if (TYPE.ARRAY == schema.getType()) {
// Extract the JSON string from this entity
Collection<JSONEntity> valueList = (Collection<JSONEntity>) value;
// Get the JSONString
try {
// Build up an array using the object.
JSONArrayAdapterImpl adapterArray = new JSONArrayAdapterImpl();
int index = 0;
for (JSONEntity entity : valueList) {
adapterArray.put(index, entity.writeToJSONObject(new JSONObjectAdapterImpl()));
index++;
}
String jsonString = adapterArray.toJSONString();
// Save the UTF-8 bytes of this string
return jsonString.getBytes("UTF-8");
} catch (JSONObjectAdapterException e) {
throw new RuntimeException(e);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
} else {
// Unknown binary type
throw new IllegalArgumentException("Unknown schema type: " + schema.getType() + ". Can only convert Strings, JSONEntity objects to byte[]");
}
}
use of org.sagebionetworks.schema.adapter.org.json.JSONObjectAdapterImpl in project Synapse-Repository-Services by Sage-Bionetworks.
the class ServletTestHelper method deserializePaginatedResults.
public static <T extends JSONEntity> PaginatedResults<T> deserializePaginatedResults(String json, Class<T> clazz) {
try {
PaginatedResults<T> prs = new PaginatedResults<T>(clazz);
prs.initializeFromJSONObject(new JSONObjectAdapterImpl(json));
return prs;
} catch (JSONObjectAdapterException e) {
throw new RuntimeException(e);
}
}
use of org.sagebionetworks.schema.adapter.org.json.JSONObjectAdapterImpl in project Synapse-Repository-Services by Sage-Bionetworks.
the class PreviewTest method testRoundTripPreview.
@Test
public void testRoundTripPreview() throws JSONObjectAdapterException {
Preview p1 = new Preview();
JSONObjectAdapter adapter1 = new JSONObjectAdapterImpl();
JSONObjectAdapter adapter2 = new JSONObjectAdapterImpl();
Date d = new Date();
p1.setAccessControlList("/acl");
p1.setAnnotations("/annotations");
p1.setCreatedBy("createdBy");
p1.setCreatedOn(d);
p1.setDescription("description");
p1.setEtag("1");
p1.setId("1");
p1.setModifiedBy("modifiedBy");
p1.setModifiedOn(d);
p1.setName("name");
p1.setParentId("0");
p1.setUri("uri");
List<String> headers = new ArrayList<String>();
headers.add("header1");
headers.add("header2");
p1.setHeaders(headers);
p1.setPreviewString("previewString");
List<Row> rows = new ArrayList<Row>();
Row r = new Row();
List<String> cells = new ArrayList<String>();
cells.add("cell1");
cells.add("cell2");
r.setCells(cells);
r.setCells(cells);
rows.add(r);
p1.setRows(rows);
adapter1 = p1.writeToJSONObject(adapter1);
String s = adapter1.toJSONString();
adapter2 = new JSONObjectAdapterImpl(s);
Preview p2 = new Preview(adapter2);
assertEquals(p1, p2);
return;
}
use of org.sagebionetworks.schema.adapter.org.json.JSONObjectAdapterImpl in project Synapse-Repository-Services by Sage-Bionetworks.
the class DBOUserProfileDAOImplTest method setUp.
@Before
public void setUp() throws Exception {
individualGroup = userGroupDAO.findGroup(TEST_USER_NAME, true);
if (individualGroup == null) {
individualGroup = new UserGroup();
individualGroup.setName(TEST_USER_NAME);
individualGroup.setIsIndividual(true);
individualGroup.setCreationDate(new Date());
individualGroup.setId(userGroupDAO.create(individualGroup));
}
String jsonString = (String) UserProfile.class.getField(JSONEntity.EFFECTIVE_SCHEMA).get(null);
schema = new ObjectSchema(new JSONObjectAdapterImpl(jsonString));
}
Aggregations