use of org.ballerinalang.model.util.JsonNode in project ballerina by ballerina-lang.
the class CPU method castJSONToBoolean.
private static void castJSONToBoolean(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.typeBoolean, e.getMessage());
ctx.setError(BLangVMErrors.createError(ctx, errorMsg));
handleError(ctx);
return;
}
if (jsonNode.isBoolean()) {
sf.intRegs[j] = jsonNode.booleanValue() ? 1 : 0;
return;
}
// Reset the value in the case of an error;
sf.intRegs[j] = 0;
// handleTypeCastError(ctx, sf, j, JSONUtils.getTypeName(jsonNode), TypeConstants.BOOLEAN_TNAME);
}
use of org.ballerinalang.model.util.JsonNode 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.util.JsonNode in project ballerina by ballerina-lang.
the class BJSON method stringValue.
@Override
public String stringValue() {
JsonNode node = this.value();
if (node.isValueNode()) {
return this.value().asText();
} else if (!node.isObject()) {
return node.toString();
}
BStructType constrainedType = (BStructType) ((BJSONType) this.type).getConstrainedType();
if (constrainedType == null) {
return node.toString();
}
// If constrained JSON, print the only the fields in the constrained type.
StringJoiner sj = new StringJoiner(",", "{", "}");
for (StructField field : constrainedType.getStructFields()) {
String key = field.fieldName;
String stringValue = this.value().get(key).toString();
sj.add("\"" + key + "\":" + stringValue);
}
return sj.toString();
}
use of org.ballerinalang.model.util.JsonNode in project ballerina by ballerina-lang.
the class DebugMsgUtil method buildCommandDTO.
/**
* Method to build CommandDTO instance with given json msg.
*
* @param json msg String.
* @return object instance.
*/
public static CommandDTO buildCommandDTO(String json) {
JsonNode node = JsonParser.parse(json);
CommandDTO commandDTO = new CommandDTO();
commandDTO.setCommand(node.get(COMMAND) == null ? null : node.get(COMMAND).stringValue());
commandDTO.setThreadId(node.get(THREAD_ID) == null ? null : node.get(THREAD_ID).stringValue());
commandDTO.setPoints(buildBreakPoints(node.get(POINTS)));
return commandDTO;
}
use of org.ballerinalang.model.util.JsonNode in project ballerina by ballerina-lang.
the class JSONLibraryTest method testBasicJsonObjectGenValues.
@Test
public void testBasicJsonObjectGenValues() throws IOException {
String json = "{\"a\":\"abc\",\"b\":1,\"c\":3.14,\"d\":true,\"e\":false,\"f\":null," + "\"g\":{\"1\":\"a\",\"2\":\"b\"},\"h\":[\"A\",20,30,\"D\"]}";
JsonNode node = JsonParser.parse(json);
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
JsonGenerator gen = new JsonGenerator(byteOut);
node.serialize(gen);
gen.flush();
Assert.assertEquals(json, new String(byteOut.toByteArray()));
}
Aggregations