use of org.ballerinalang.model.values.BJSON in project ballerina by ballerina-lang.
the class CPU method castJSONToString.
private static void castJSONToString(WorkerExecutionContext ctx, int[] operands, WorkerData sf) {
int i = operands[0];
int j = operands[1];
BJSON jsonValue = (BJSON) sf.refRegs[i];
if (jsonValue == null) {
handleNullRefError(ctx);
return;
}
JsonNode jsonNode;
try {
jsonNode = jsonValue.value();
} catch (BallerinaException e) {
sf.stringRegs[j] = BLangConstants.STRING_EMPTY_VALUE;
String errorMsg = BLangExceptionHelper.getErrorMessage(RuntimeErrors.CASTING_FAILED_WITH_CAUSE, BTypes.typeJSON, BTypes.typeString, e.getMessage());
ctx.setError(BLangVMErrors.createError(ctx, errorMsg));
handleError(ctx);
return;
}
if (jsonNode.isString()) {
sf.stringRegs[j] = jsonNode.stringValue();
return;
}
sf.stringRegs[j] = STRING_NULL_VALUE;
// handleTypeCastError(ctx, sf, j, JSONUtils.getTypeName(jsonNode), TypeConstants.STRING_TNAME);
}
use of org.ballerinalang.model.values.BJSON in project ballerina by ballerina-lang.
the class JSONUtils method convertJSON.
private static Object convertJSON(JsonNode jsonValue, BType targetType) {
switch(targetType.getTag()) {
case TypeTags.INT_TAG:
return jsonNodeToInt(jsonValue);
case TypeTags.FLOAT_TAG:
return jsonNodeToFloat(jsonValue);
case TypeTags.STRING_TAG:
if (jsonValue.isString()) {
return jsonValue.stringValue();
} else {
return jsonValue.toString();
}
case TypeTags.BOOLEAN_TAG:
return jsonNodeToBool(jsonValue);
case TypeTags.UNION_TAG:
BUnionType type = (BUnionType) targetType;
if (jsonValue.isNull() && type.isNullable()) {
return null;
}
List<BType> matchingTypes = type.getMemberTypes().stream().filter(memberType -> memberType != BTypes.typeNull).collect(Collectors.toList());
if (matchingTypes.size() == 1) {
return convertJSON(jsonValue, matchingTypes.get(0));
}
break;
case TypeTags.STRUCT_TAG:
return convertJSONNodeToStruct(jsonValue, (BStructType) targetType);
case TypeTags.ANY_TAG:
case TypeTags.JSON_TAG:
if (jsonValue.isNull()) {
return null;
}
return new BJSON(jsonValue);
case TypeTags.ARRAY_TAG:
return jsonNodeToBArray(jsonValue, (BArrayType) targetType);
case TypeTags.MAP_TAG:
return jsonNodeToBMap(jsonValue, (BMapType) targetType);
case TypeTags.NULL_TAG:
if (jsonValue.isNull()) {
return null;
}
break;
default:
break;
}
throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_TYPE_FOR_CASTING, targetType, getTypeName(jsonValue));
}
use of org.ballerinalang.model.values.BJSON in project ballerina by ballerina-lang.
the class JSONUtils method convertArrayToJSON.
/**
* Convert {@link BIntArray} to {@link BJSON}.
*
* @param intArray {@link BIntArray} to be converted to {@link BJSON}
* @return JSON representation of the provided intArray
*/
public static BJSON convertArrayToJSON(BIntArray intArray) {
BJSON bjson = new BJSON(new JsonNode(Type.ARRAY));
JsonNode arrayNode = bjson.value();
for (int i = 0; i < intArray.size(); i++) {
long value = intArray.get(i);
arrayNode.add(value);
}
return bjson;
}
use of org.ballerinalang.model.values.BJSON in project ballerina by ballerina-lang.
the class JSONUtils method convertArrayToJSON.
/**
* Convert {@link BStringArray} to {@link BJSON}.
*
* @param stringArray {@link BStringArray} to be converted to {@link BJSON}
* @return JSON representation of the provided stringArray
*/
public static BJSON convertArrayToJSON(BStringArray stringArray) {
BJSON bjson = new BJSON(new JsonNode(Type.ARRAY));
JsonNode arrayNode = bjson.value();
for (int i = 0; i < stringArray.size(); i++) {
String value = stringArray.get(i);
arrayNode.add(value);
}
return bjson;
}
use of org.ballerinalang.model.values.BJSON in project ballerina by ballerina-lang.
the class TableIterator method generateNext.
@Override
public BStruct generateNext() {
BStruct bStruct = new BStruct(type);
int longRegIndex = -1;
int doubleRegIndex = -1;
int stringRegIndex = -1;
int booleanRegIndex = -1;
int refRegIndex = -1;
int blobRegIndex = -1;
int index = 0;
try {
BStructType.StructField[] structFields = type.getStructFields();
for (BStructType.StructField sf : structFields) {
BType type = sf.getFieldType();
++index;
switch(type.getTag()) {
case TypeTags.INT_TAG:
long iValue = rs.getInt(index);
bStruct.setIntField(++longRegIndex, iValue);
break;
case TypeTags.STRING_TAG:
String sValue = rs.getString(index);
bStruct.setStringField(++stringRegIndex, sValue);
break;
case TypeTags.FLOAT_TAG:
double dalue = rs.getDouble(index);
bStruct.setFloatField(++doubleRegIndex, dalue);
break;
case TypeTags.BOOLEAN_TAG:
boolean boolValue = rs.getBoolean(index);
bStruct.setBooleanField(++booleanRegIndex, boolValue ? 1 : 0);
break;
case TypeTags.JSON_TAG:
String jsonValue = rs.getString(index);
bStruct.setRefField(++refRegIndex, new BJSON(jsonValue));
break;
case TypeTags.XML_TAG:
String xmlValue = rs.getString(index);
bStruct.setRefField(++refRegIndex, new BXMLItem(xmlValue));
break;
case TypeTags.BLOB_TAG:
Blob blobValue = rs.getBlob(index);
bStruct.setBlobField(++blobRegIndex, blobValue.getBytes(1L, (int) blobValue.length()));
break;
case TypeTags.ARRAY_TAG:
Array arrayValue = rs.getArray(index);
bStruct.setRefField(++refRegIndex, getDataArray(arrayValue));
break;
}
}
} catch (SQLException e) {
throw new BallerinaException("error in generating next row of data :" + e.getMessage());
}
return bStruct;
}
Aggregations