Search in sources :

Example 21 with JsonNode

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");
}
Also used : BValue(org.ballerinalang.model.values.BValue) JsonNode(org.ballerinalang.model.util.JsonNode) BJSON(org.ballerinalang.model.values.BJSON) Test(org.testng.annotations.Test)

Example 22 with JsonNode

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);
}
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 23 with JsonNode

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);
}
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 24 with JsonNode

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

Example 25 with JsonNode

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;
}
Also used : SQLException(java.sql.SQLException) JsonNode(org.ballerinalang.model.util.JsonNode) BigDecimal(java.math.BigDecimal) Struct(java.sql.Struct) BStructType(org.ballerinalang.model.types.BStructType) BType(org.ballerinalang.model.types.BType) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

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