use of org.ballerinalang.model.values.BNewArray in project ballerina by ballerina-lang.
the class JSONUtils method convertStructToJSON.
/**
* Convert {@link BStruct} to {@link BJSON}.
*
* @param struct {@link BStruct} to be converted to {@link BJSON}
* @return JSON representation of the provided array
*/
@SuppressWarnings("unchecked")
public static BJSON convertStructToJSON(BStruct struct) {
BJSON bjson = new BJSON(new JsonNode(Type.OBJECT));
JsonNode jsonNode = bjson.value();
BStructType structType = (BStructType) struct.getType();
int longRegIndex = -1;
int doubleRegIndex = -1;
int stringRegIndex = -1;
int booleanRegIndex = -1;
int refRegIndex = -1;
for (BStructType.StructField structField : structType.getStructFields()) {
String key = structField.getFieldName();
BType fieldType = structField.getFieldType();
try {
switch(fieldType.getTag()) {
case TypeTags.INT_TAG:
jsonNode.set(key, struct.getIntField(++longRegIndex));
break;
case TypeTags.FLOAT_TAG:
jsonNode.set(key, struct.getFloatField(++doubleRegIndex));
break;
case TypeTags.STRING_TAG:
jsonNode.set(key, struct.getStringField(++stringRegIndex));
break;
case TypeTags.BOOLEAN_TAG:
jsonNode.set(key, struct.getBooleanField(++booleanRegIndex) == 1);
break;
case TypeTags.BLOB_TAG:
throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_TYPE_FOR_CASTING, BTypes.typeJSON, BTypes.typeBlob);
default:
BValue value = struct.getRefField(++refRegIndex);
if (value == null) {
jsonNode.set(key, new BJSON(NULL).value());
} else if (value.getType().getTag() == TypeTags.MAP_TAG) {
jsonNode.set(key, convertMapToJSON((BMap<String, BValue>) value).value());
} else if (value instanceof BJSON) {
jsonNode.set(key, ((BJSON) value).value());
} else if (value instanceof BNewArray) {
jsonNode.set(key, convertArrayToJSON((BNewArray) value).value());
} else if (value instanceof BStruct) {
jsonNode.set(key, convertStructToJSON((BStruct) value).value());
} else {
throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_TYPE_FOR_CASTING, BTypes.typeJSON, value.getType());
}
}
} catch (Exception e) {
handleError(e, key);
}
}
return bjson;
}
use of org.ballerinalang.model.values.BNewArray in project ballerina by ballerina-lang.
the class AbstractSQLAction method createProcessedQueryString.
/**
* If there are any arrays of parameter for types other than sql array, the given query is expanded by adding "?" s
* to match with the array size.
*/
private String createProcessedQueryString(String query, BRefValueArray parameters) {
String currentQuery = query;
if (parameters != null) {
int start = 0;
Object[] vals;
int count;
int paramCount = (int) parameters.size();
for (int i = 0; i < paramCount; i++) {
BStruct paramValue = (BStruct) parameters.get(i);
if (paramValue != null) {
String sqlType = getSQLType(paramValue);
BValue value = paramValue.getRefField(1);
if (value != null && value.getType().getTag() == TypeTags.ARRAY_TAG && !Constants.SQLDataTypes.ARRAY.equalsIgnoreCase(sqlType)) {
count = (int) ((BNewArray) value).size();
} else {
count = 1;
}
vals = this.expandQuery(start, count, currentQuery);
start = (Integer) vals[0];
currentQuery = (String) vals[1];
}
}
}
return currentQuery;
}
use of org.ballerinalang.model.values.BNewArray in project ballerina by ballerina-lang.
the class VariableDTO method getStringValue.
private String getStringValue(BValue bValue) {
String bValueString;
if (bValue instanceof BValueType || bValue instanceof BXML || bValue instanceof BJSON) {
bValueString = bValue.stringValue();
} else if (bValue instanceof BNewArray) {
BNewArray bArray = (BNewArray) bValue;
bValueString = "Array[" + bArray.size() + "] ";
bValueString = bValueString + bArray.stringValue();
} else if (bValue instanceof BMap) {
BMap bmap = (BMap) bValue;
bValueString = "Map[" + bmap.size() + "] ";
bValueString = bValueString + bmap.stringValue();
} else if (bValue instanceof BStruct) {
BStruct bStruct = (BStruct) bValue;
bValueString = "struct " + bStruct.getType().getName() + " ";
bValueString = bValueString + bStruct.stringValue();
} else {
bValueString = "<Complex_Value>";
}
return bValueString;
}
use of org.ballerinalang.model.values.BNewArray in project ballerina by ballerina-lang.
the class Equals method isEqual.
/**
* Deep equal or strict equal implementation for any value type.
*
* @param lhsValue The value on the left side.
* @param rhsValue The value on the right side.
* @return True if values are equal, else false.
*/
@SuppressWarnings("rawtypes")
private boolean isEqual(BValue lhsValue, BValue rhsValue) {
if (null == lhsValue && null == rhsValue) {
return true;
}
if (null == lhsValue || null == rhsValue) {
return false;
}
// Required for any == any.
if (lhsValue.getType().getTag() != rhsValue.getType().getTag()) {
return false;
}
if (null != lhsValue.getType().getPackagePath() && null != rhsValue.getType().getPackagePath() && !lhsValue.getType().getPackagePath().equals(rhsValue.getType().getPackagePath())) {
return false;
}
if (null != lhsValue.getType().getName() && null != rhsValue.getType().getName() && !lhsValue.getType().getName().equals(rhsValue.getType().getName())) {
return false;
}
// Handling JSON.
if (lhsValue instanceof BJSON && rhsValue instanceof BJSON) {
JsonNode lhsJSON = ((BJSON) lhsValue).value();
JsonNode rhsJSON = ((BJSON) rhsValue).value();
return isEqual(lhsJSON, rhsJSON);
}
switch(lhsValue.getType().getTag()) {
case TypeTags.STRING_TAG:
case TypeTags.INT_TAG:
case TypeTags.FLOAT_TAG:
case TypeTags.BOOLEAN_TAG:
case TypeTags.TYPEDESC_TAG:
BRefType lhsRef = (BRefType) lhsValue;
BRefType rhsRef = (BRefType) rhsValue;
if (null == lhsRef.value() && null == rhsRef.value()) {
return true;
}
if (null == lhsRef.value() || null == rhsRef.value()) {
return false;
}
return lhsRef.value().equals(rhsRef.value());
case TypeTags.STRUCT_TAG:
BStructType lhsStructType = (BStructType) lhsValue.getType();
BStructType rhsStructType = (BStructType) rhsValue.getType();
if (!Arrays.equals(lhsStructType.getFieldTypeCount(), rhsStructType.getFieldTypeCount())) {
return false;
}
return isEqual((BStruct) lhsValue, (BStruct) rhsValue, lhsStructType);
case TypeTags.MAP_TAG:
return isEqual((BMap) lhsValue, (BMap) rhsValue);
case TypeTags.ARRAY_TAG:
case TypeTags.ANY_TAG:
// TODO: This block should ideally be in the ARRAY_TAG case only. Not ANY_TAG. #4505.
if (lhsValue instanceof BNewArray) {
BNewArray lhsArray = (BNewArray) lhsValue;
BNewArray rhsArray = (BNewArray) rhsValue;
if (lhsArray.size() != rhsArray.size()) {
return false;
}
for (int i = 0; i < lhsArray.size(); i++) {
if (!isEqual(lhsArray.getBValue(i), rhsArray.getBValue(i))) {
return false;
}
}
}
break;
case TypeTags.XML_TAG:
// TODO: https://www.pixelstech.net/article/1453272563-Canonicalize-XML-in-Java
break;
default:
return false;
}
return true;
}
Aggregations