use of org.ballerinalang.model.util.JsonNode in project ballerina by ballerina-lang.
the class MatchStatementTest method testMatchStatementBasics9.
@Test(description = "Test basics of match statement")
public void testMatchStatementBasics9() {
JsonNode jsonNode = new JsonNode(10);
BJSON jsonValue = new BJSON(jsonNode);
BValue[] returns = BRunUtil.invoke(result, "testMatchStatementBasics9", new BValue[] { jsonValue });
Assert.assertEquals(returns.length, 1);
Assert.assertSame(returns[0].getClass(), BString.class);
Assert.assertEquals(returns[0].stringValue(), "json int| null matched");
jsonNode = new JsonNode("string value");
jsonValue = new BJSON(jsonNode);
returns = BRunUtil.invoke(result, "testMatchStatementBasics9", new BValue[] { jsonValue });
Assert.assertEquals(returns.length, 1);
Assert.assertSame(returns[0].getClass(), BString.class);
Assert.assertEquals(returns[0].stringValue(), "json string | boolean matched");
jsonNode = new JsonNode(false);
jsonValue = new BJSON(jsonNode);
returns = BRunUtil.invoke(result, "testMatchStatementBasics9", new BValue[] { jsonValue });
Assert.assertEquals(returns.length, 1);
Assert.assertSame(returns[0].getClass(), BString.class);
Assert.assertEquals(returns[0].stringValue(), "json string | boolean matched");
jsonNode = new JsonNode(10.89);
jsonValue = new BJSON(jsonNode);
returns = BRunUtil.invoke(result, "testMatchStatementBasics9", new BValue[] { jsonValue });
Assert.assertEquals(returns.length, 1);
Assert.assertSame(returns[0].getClass(), BString.class);
Assert.assertEquals(returns[0].stringValue(), "json matched");
}
use of org.ballerinalang.model.util.JsonNode in project ballerina by ballerina-lang.
the class CPU method castJSONToFloat.
private static void castJSONToFloat(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) {
String errorMsg = BLangExceptionHelper.getErrorMessage(RuntimeErrors.CASTING_FAILED_WITH_CAUSE, BTypes.typeJSON, BTypes.typeFloat, e.getMessage());
ctx.setError(BLangVMErrors.createError(ctx, errorMsg));
handleError(ctx);
return;
}
if (jsonNode.isDouble()) {
sf.doubleRegs[j] = jsonNode.doubleValue();
return;
}
sf.doubleRegs[j] = 0;
// handleTypeCastError(ctx, sf, j, JSONUtils.getTypeName(jsonNode), TypeConstants.FLOAT_TNAME);
}
use of org.ballerinalang.model.util.JsonNode in project ballerina by ballerina-lang.
the class CPU method castJSONToInt.
private static void castJSONToInt(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) {
String errorMsg = BLangExceptionHelper.getErrorMessage(RuntimeErrors.CASTING_FAILED_WITH_CAUSE, BTypes.typeJSON, BTypes.typeInt, e.getMessage());
ctx.setError(BLangVMErrors.createError(ctx, errorMsg));
handleError(ctx);
return;
}
if (jsonNode.isLong()) {
sf.longRegs[j] = jsonNode.longValue();
return;
}
sf.longRegs[j] = 0;
// handleTypeCastError(ctx, sf, j, JSONUtils.getTypeName(jsonNode), TypeConstants.INT_TNAME);
}
use of org.ballerinalang.model.util.JsonNode in project ballerina by ballerina-lang.
the class TableJSONDataSource method getDataArray.
private static JsonNode getDataArray(BTable df, int columnIndex) {
Object[] dataArray = df.getArray(columnIndex);
int length = dataArray.length;
JsonNode jsonArray = new JsonNode(Type.ARRAY);
if (length > 0) {
Object obj = dataArray[0];
if (obj instanceof String) {
for (Object value : dataArray) {
jsonArray.add((String) value);
}
} else if (obj instanceof Boolean) {
for (Object value : dataArray) {
jsonArray.add((Boolean) value);
}
} else if (obj instanceof Integer) {
for (Object value : dataArray) {
jsonArray.add((int) value);
}
} else if (obj instanceof Long) {
for (Object value : dataArray) {
jsonArray.add((long) value);
}
} else if (obj instanceof Float) {
for (Object value : dataArray) {
jsonArray.add((float) value);
}
} else if (obj instanceof Double) {
for (Object value : dataArray) {
jsonArray.add((double) value);
}
}
}
return jsonArray;
}
use of org.ballerinalang.model.util.JsonNode in project ballerina by ballerina-lang.
the class TableJSONDataSource method getStructData.
private static JsonNode getStructData(Object[] data, BStructType.StructField[] structFields, int index) {
JsonNode jsonData = null;
try {
if (structFields == null) {
jsonData = new JsonNode(Type.ARRAY);
if (data != null) {
for (Object value : data) {
if (value instanceof String) {
jsonData.add((String) value);
} else if (value instanceof Boolean) {
jsonData.add((Boolean) value);
} else if (value instanceof Long) {
jsonData.add((long) value);
} else if (value instanceof Double) {
jsonData.add((double) value);
} else if (value instanceof Integer) {
jsonData.add((int) value);
} else if (value instanceof Float) {
jsonData.add((float) value);
} else if (value instanceof BigDecimal) {
jsonData.add(((BigDecimal) value).doubleValue());
}
}
}
} else {
jsonData = new JsonNode(Type.OBJECT);
boolean structError = true;
if (data != null) {
int i = 0;
for (Object value : data) {
BType internaltType = structFields[index - 1].fieldType;
if (internaltType.getTag() == TypeTags.STRUCT_TAG) {
BStructType.StructField[] interanlStructFields = ((BStructType) internaltType).getStructFields();
if (interanlStructFields != null) {
if (value instanceof String) {
jsonData.set(interanlStructFields[i].fieldName, (String) value);
} else if (value instanceof Boolean) {
jsonData.set(interanlStructFields[i].fieldName, (Boolean) value);
} else if (value instanceof Long) {
jsonData.set(interanlStructFields[i].fieldName, (long) value);
} else if (value instanceof Double) {
jsonData.set(interanlStructFields[i].fieldName, (double) value);
} else if (value instanceof Integer) {
jsonData.set(interanlStructFields[i].fieldName, (int) value);
} else if (value instanceof Float) {
jsonData.set(interanlStructFields[i].fieldName, (float) value);
} else if (value instanceof BigDecimal) {
jsonData.set(interanlStructFields[i].fieldName, ((BigDecimal) value).doubleValue());
} else if (value instanceof Struct) {
jsonData.set(interanlStructFields[i].fieldName, getStructData(((Struct) value).getAttributes(), interanlStructFields, i + 1));
}
structError = false;
}
}
++i;
}
}
if (structError) {
throw new BallerinaException("error in constructing the json object from struct type data");
}
}
} catch (SQLException e) {
throw new BallerinaException("error in retrieving struct data to construct the inner json object:" + e.getMessage());
}
return jsonData;
}
Aggregations