Search in sources :

Example 1 with JSONArrayAdapterImpl

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[]");
    }
}
Also used : JSONObjectAdapter(org.sagebionetworks.schema.adapter.JSONObjectAdapter) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JSONObjectAdapterException(org.sagebionetworks.schema.adapter.JSONObjectAdapterException) JSONArrayAdapterImpl(org.sagebionetworks.schema.adapter.org.json.JSONArrayAdapterImpl) JSONException(org.json.JSONException) JSONObjectAdapterException(org.sagebionetworks.schema.adapter.JSONObjectAdapterException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JSONEntity(org.sagebionetworks.schema.adapter.JSONEntity)

Example 2 with JSONArrayAdapterImpl

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[]");
    }
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) JSONObjectAdapterException(org.sagebionetworks.schema.adapter.JSONObjectAdapterException) Collection(java.util.Collection) JSONArrayAdapterImpl(org.sagebionetworks.schema.adapter.org.json.JSONArrayAdapterImpl) JSONEntity(org.sagebionetworks.schema.adapter.JSONEntity) JSONObjectAdapterImpl(org.sagebionetworks.schema.adapter.org.json.JSONObjectAdapterImpl)

Example 3 with JSONArrayAdapterImpl

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;
}
Also used : JSONObjectAdapter(org.sagebionetworks.schema.adapter.JSONObjectAdapter) ArrayList(java.util.ArrayList) JSONArrayAdapterImpl(org.sagebionetworks.schema.adapter.org.json.JSONArrayAdapterImpl) JSONArrayAdapter(org.sagebionetworks.schema.adapter.JSONArrayAdapter) NamedAnnotations(org.sagebionetworks.repo.model.NamedAnnotations) Annotations(org.sagebionetworks.repo.model.Annotations) LocationData(org.sagebionetworks.repo.model.LocationData) ArrayList(java.util.ArrayList) List(java.util.List) AttachmentData(org.sagebionetworks.repo.model.attachment.AttachmentData) StorageLocations(org.sagebionetworks.repo.model.StorageLocations)

Aggregations

JSONArrayAdapterImpl (org.sagebionetworks.schema.adapter.org.json.JSONArrayAdapterImpl)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 JSONEntity (org.sagebionetworks.schema.adapter.JSONEntity)2 JSONObjectAdapter (org.sagebionetworks.schema.adapter.JSONObjectAdapter)2 JSONObjectAdapterException (org.sagebionetworks.schema.adapter.JSONObjectAdapterException)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 List (java.util.List)1 JSONException (org.json.JSONException)1 Annotations (org.sagebionetworks.repo.model.Annotations)1 LocationData (org.sagebionetworks.repo.model.LocationData)1 NamedAnnotations (org.sagebionetworks.repo.model.NamedAnnotations)1 StorageLocations (org.sagebionetworks.repo.model.StorageLocations)1 AttachmentData (org.sagebionetworks.repo.model.attachment.AttachmentData)1 JSONArrayAdapter (org.sagebionetworks.schema.adapter.JSONArrayAdapter)1 JSONObjectAdapterImpl (org.sagebionetworks.schema.adapter.org.json.JSONObjectAdapterImpl)1