Search in sources :

Example 1 with JsonNode

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);
}
Also used : JsonNode(org.ballerinalang.model.util.JsonNode) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) BString(org.ballerinalang.model.values.BString) BJSON(org.ballerinalang.model.values.BJSON)

Example 2 with JsonNode

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);
}
Also used : JsonNode(org.ballerinalang.model.util.JsonNode) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) BString(org.ballerinalang.model.values.BString) BJSON(org.ballerinalang.model.values.BJSON)

Example 3 with JsonNode

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();
}
Also used : BStructType(org.ballerinalang.model.types.BStructType) StructField(org.ballerinalang.model.types.BStructType.StructField) JsonNode(org.ballerinalang.model.util.JsonNode) StringJoiner(java.util.StringJoiner)

Example 4 with JsonNode

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;
}
Also used : JsonNode(org.ballerinalang.model.util.JsonNode) CommandDTO(org.ballerinalang.util.debugger.dto.CommandDTO)

Example 5 with JsonNode

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()));
}
Also used : JsonGenerator(org.ballerinalang.model.util.JsonGenerator) JsonNode(org.ballerinalang.model.util.JsonNode) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.testng.annotations.Test)

Aggregations

JsonNode (org.ballerinalang.model.util.JsonNode)25 Test (org.testng.annotations.Test)13 BJSON (org.ballerinalang.model.values.BJSON)10 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)5 BString (org.ballerinalang.model.values.BString)4 BValue (org.ballerinalang.model.values.BValue)4 BStructType (org.ballerinalang.model.types.BStructType)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 BigDecimal (java.math.BigDecimal)1 SQLException (java.sql.SQLException)1 Struct (java.sql.Struct)1 ArrayList (java.util.ArrayList)1 StringJoiner (java.util.StringJoiner)1 StructField (org.ballerinalang.model.types.BStructType.StructField)1 BType (org.ballerinalang.model.types.BType)1 JsonGenerator (org.ballerinalang.model.util.JsonGenerator)1 BNewArray (org.ballerinalang.model.values.BNewArray)1 BRefType (org.ballerinalang.model.values.BRefType)1 BStruct (org.ballerinalang.model.values.BStruct)1 BreakPointDTO (org.ballerinalang.util.debugger.dto.BreakPointDTO)1