use of org.sagebionetworks.schema.adapter.org.json.JSONArrayAdapterImpl in project Synapse-Repository-Services by Sage-Bionetworks.
the class NodeTranslationUtils method bytesToObject.
/**
* Convert bytes to an object.
*
* @param bytes
* @param clazz
* @return
*/
public static Object bytesToObject(byte[] bytes, ObjectSchema schema) {
if (bytes == null)
throw new IllegalArgumentException("Bytes cannot be null");
if (schema == null)
throw new IllegalArgumentException("Schema cannot be null");
// Is this a string
if (TYPE.STRING == schema.getType()) {
try {
return new String(bytes, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
} else if (TYPE.OBJECT == schema.getType()) {
if (schema.getId() == null)
throw new IllegalArgumentException("The schema does not have an ID so we cannot lookup the class");
String json;
try {
json = new String(bytes, "UTF-8");
return EntityFactory.createEntityFromJSONString(json, (Class<? extends JSONEntity>) Class.forName(schema.getId()));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
} catch (JSONObjectAdapterException e) {
throw new RuntimeException(e);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
} else if (TYPE.ARRAY == schema.getType()) {
if (schema.getItems() == null)
throw new IllegalArgumentException("The schema items cannot be null for type array");
if (schema.getItems().getId() == null)
throw new IllegalArgumentException("The schema items.getId() cannot be null for type array");
String json;
try {
json = new String(bytes, "UTF-8");
JSONArrayAdapterImpl adapterArray = new JSONArrayAdapterImpl(json);
Collection<JSONEntity> collection = null;
if (schema.getUniqueItems()) {
collection = new HashSet<JSONEntity>();
} else {
collection = new ArrayList<JSONEntity>();
}
Class<? extends JSONEntity> clazz = (Class<? extends JSONEntity>) Class.forName(schema.getItems().getId());
for (int index = 0; index < adapterArray.length(); index++) {
JSONObjectAdapter adapter = adapterArray.getJSONObject(index);
JSONEntity newInstance = clazz.newInstance();
newInstance.initializeFromJSONObject(adapter);
collection.add(newInstance);
}
return collection;
} catch (Exception 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.JSONArrayAdapterImpl 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.JSONArrayAdapterImpl in project Synapse-Repository-Services by Sage-Bionetworks.
the class JDOSecondaryPropertyUtils method getStorageLocations.
/**
* Extracts storage locations from annotations.
*
* @throws UnsupportedEncodingException When the blob is not encoded by UTF-8
* @throws JSONObjectAdapterException When the blob is not a JSON array
*
* @return Storage locations or null if the annotations do not contain any storage locations
*/
public static StorageLocations getStorageLocations(NamedAnnotations namedAnnos, Long node, Long user) throws UnsupportedEncodingException, JSONObjectAdapterException {
if (node == null) {
throw new NullPointerException();
}
if (user == null) {
throw new NullPointerException();
}
if (namedAnnos == null) {
throw new NullPointerException();
}
final Annotations primaryAnnos = namedAnnos.getPrimaryAnnotations();
final Map<String, List<byte[]>> blobs = primaryAnnos.getBlobAnnotations();
// Read attachment data from annotation blobs
List<AttachmentData> attachmentList = new ArrayList<AttachmentData>();
List<byte[]> attachments = blobs.get("attachments");
if (attachments != null) {
for (byte[] byteArray : attachments) {
// Packed at NodeTranslationUtils.objectToBytes(). Now unpack it.
String jsonStr = new String(byteArray, "UTF-8");
JSONArrayAdapter jsonArray = new JSONArrayAdapterImpl(jsonStr);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObjectAdapter jsonObj = jsonArray.getJSONObject(i);
AttachmentData attachment = new AttachmentData();
attachment.initializeFromJSONObject(jsonObj);
attachmentList.add(attachment);
}
}
}
// Read location data from annotation blobs
List<LocationData> locationList = new ArrayList<LocationData>();
List<byte[]> locations = blobs.get("locations");
if (locations != null) {
for (byte[] byteArray : locations) {
// Packed at NodeTranslationUtils.objectToBytes(). Now unpack it.
String jsonStr = new String(byteArray, "UTF-8");
JSONArrayAdapter jsonArray = new JSONArrayAdapterImpl(jsonStr);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObjectAdapter jsonObj = jsonArray.getJSONObject(i);
LocationData location = new LocationData();
location.initializeFromJSONObject(jsonObj);
locationList.add(location);
}
}
}
Map<String, List<String>> strAnnotations = primaryAnnos.getStringAnnotations();
StorageLocations sl = new StorageLocations(node, user, Collections.unmodifiableList(attachmentList), Collections.unmodifiableList(locationList), strAnnotations);
return sl;
}
Aggregations