private static JSONObject convertConstraintToJSON(Constraint constraint) throws JSONException {
JSONObject jConstraint = new JSONObject();
jConstraint.put("type", constraint.getType().name());
switch (constraint.getType()) {
case value: //both ValueConstraint and ReferenceConstraint
ValueConstraint valueConstraint = ((ValueConstraint) constraint);
if (valueConstraint.getValue() != null) {
jConstraint.put("value", valueConstraint.getValue());
}
if(constraint instanceof ReferenceConstraint){
//the type "reference" is not present in the ConstraintType
//enum, because internally ReferenceConstraints are just a
//ValueConstraint with a predefined data type, but "reference"
//is still a valid value of the type property in JSON
jConstraint.put("type", "reference");
} else { // valueConstraint
jConstraint.put("type", constraint.getType().name());
//for valueConstraints we need to add also the dataType(s)
Collection<String> dataTypes = valueConstraint.getDataTypes();
if (dataTypes != null && !dataTypes.isEmpty()) {
if(dataTypes.size() == 1) {
jConstraint.put("datatype", NamespaceEnum.getShortName(dataTypes.iterator().next()));
} else {
ArrayList<String> dataTypeValues = new ArrayList<String>(dataTypes.size());