Search in sources :

Example 16 with JsonNode

use of org.ballerinalang.model.util.JsonNode in project ballerina by ballerina-lang.

the class JSONLibraryTest method testJsonEscapeChars.

@Test
public void testJsonEscapeChars() throws IOException {
    String json = "[{\"a\":\"abc\\\"\",\"x\":\"1\\b\\f\",\"c\":3.14,\"d\":true,\"e\":false,\"f\":null,\"g\":" + "{\"1\\n2\":\"a\\r\",\"2\":\"b\"},\"h\":[\"A\\tB\",20,30,\"D\\\\\"]}]";
    JsonNode node = JsonParser.parse(json);
    String result = node.toString();
    Assert.assertEquals(result, json);
}
Also used : JsonNode(org.ballerinalang.model.util.JsonNode) Test(org.testng.annotations.Test)

Example 17 with JsonNode

use of org.ballerinalang.model.util.JsonNode in project ballerina by ballerina-lang.

the class DebugMsgUtil method buildBreakPoints.

private static List<BreakPointDTO> buildBreakPoints(JsonNode node) {
    if (node == null || !node.isArray()) {
        return null;
    }
    List<BreakPointDTO> bPoints = new ArrayList<>();
    for (int i = 0; i < node.size(); i++) {
        JsonNode element = node.get(i);
        BreakPointDTO bPoint = new BreakPointDTO();
        bPoint.setPackagePath(element.get(PACKAGE_PATH) == null ? null : element.get(PACKAGE_PATH).stringValue());
        bPoint.setFileName(element.get(FILE_NAME) == null ? null : element.get(FILE_NAME).stringValue());
        bPoint.setLineNumber(element.get(LINE_NUMBER) == null ? -1 : (int) element.get(LINE_NUMBER).longValue());
        bPoints.add(bPoint);
    }
    return bPoints;
}
Also used : BreakPointDTO(org.ballerinalang.util.debugger.dto.BreakPointDTO) ArrayList(java.util.ArrayList) JsonNode(org.ballerinalang.model.util.JsonNode)

Example 18 with JsonNode

use of org.ballerinalang.model.util.JsonNode in project ballerina by ballerina-lang.

the class NativeConversionTest method testStructToJson.

@Test
public void testStructToJson() {
    BValue[] returns = BRunUtil.invoke(compileResult, "testStructToJson");
    Assert.assertTrue(returns[0] instanceof BJSON);
    JsonNode child = ((BJSON) returns[0]).value();
    Assert.assertEquals(child.get("name").stringValue(), "Child");
    Assert.assertEquals(child.get("age").longValue(), 25);
    JsonNode parent = child.get("parent");
    Assert.assertTrue(parent.isObject());
    Assert.assertEquals(parent.get("name").stringValue(), "Parent");
    Assert.assertEquals(parent.get("age").longValue(), 50);
    Assert.assertTrue(parent.get("parent").isNull());
    Assert.assertEquals(parent.get("info").toString(), "{}");
    Assert.assertTrue(parent.get("address").isNull());
    Assert.assertTrue(parent.get("marks").isNull());
    JsonNode info = child.get("info");
    Assert.assertTrue(info.isObject());
    Assert.assertEquals(info.get("status").stringValue(), "single");
    JsonNode address = child.get("address");
    Assert.assertTrue(info.isObject());
    Assert.assertEquals(address.get("country").stringValue(), "SriLanka");
    Assert.assertEquals(address.get("city").stringValue(), "Colombo");
    JsonNode marks = child.get("marks");
    Assert.assertTrue(marks.isArray());
    Assert.assertEquals(marks.size(), 3);
    Assert.assertEquals(marks.get(0).longValue(), 87);
    Assert.assertEquals(marks.get(1).longValue(), 94);
    Assert.assertEquals(marks.get(2).longValue(), 72);
}
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 19 with JsonNode

use of org.ballerinalang.model.util.JsonNode in project ballerina by ballerina-lang.

the class Equals method isEqual.

/**
 * Deep equal or strict equal implementation for any value type.
 *
 * @param lhsValue The value on the left side.
 * @param rhsValue The value on the right side.
 * @return True if values are equal, else false.
 */
@SuppressWarnings("rawtypes")
private boolean isEqual(BValue lhsValue, BValue rhsValue) {
    if (null == lhsValue && null == rhsValue) {
        return true;
    }
    if (null == lhsValue || null == rhsValue) {
        return false;
    }
    // Required for any == any.
    if (lhsValue.getType().getTag() != rhsValue.getType().getTag()) {
        return false;
    }
    if (null != lhsValue.getType().getPackagePath() && null != rhsValue.getType().getPackagePath() && !lhsValue.getType().getPackagePath().equals(rhsValue.getType().getPackagePath())) {
        return false;
    }
    if (null != lhsValue.getType().getName() && null != rhsValue.getType().getName() && !lhsValue.getType().getName().equals(rhsValue.getType().getName())) {
        return false;
    }
    // Handling JSON.
    if (lhsValue instanceof BJSON && rhsValue instanceof BJSON) {
        JsonNode lhsJSON = ((BJSON) lhsValue).value();
        JsonNode rhsJSON = ((BJSON) rhsValue).value();
        return isEqual(lhsJSON, rhsJSON);
    }
    switch(lhsValue.getType().getTag()) {
        case TypeTags.STRING_TAG:
        case TypeTags.INT_TAG:
        case TypeTags.FLOAT_TAG:
        case TypeTags.BOOLEAN_TAG:
        case TypeTags.TYPEDESC_TAG:
            BRefType lhsRef = (BRefType) lhsValue;
            BRefType rhsRef = (BRefType) rhsValue;
            if (null == lhsRef.value() && null == rhsRef.value()) {
                return true;
            }
            if (null == lhsRef.value() || null == rhsRef.value()) {
                return false;
            }
            return lhsRef.value().equals(rhsRef.value());
        case TypeTags.STRUCT_TAG:
            BStructType lhsStructType = (BStructType) lhsValue.getType();
            BStructType rhsStructType = (BStructType) rhsValue.getType();
            if (!Arrays.equals(lhsStructType.getFieldTypeCount(), rhsStructType.getFieldTypeCount())) {
                return false;
            }
            return isEqual((BStruct) lhsValue, (BStruct) rhsValue, lhsStructType);
        case TypeTags.MAP_TAG:
            return isEqual((BMap) lhsValue, (BMap) rhsValue);
        case TypeTags.ARRAY_TAG:
        case TypeTags.ANY_TAG:
            // TODO: This block should ideally be in the ARRAY_TAG case only. Not ANY_TAG. #4505.
            if (lhsValue instanceof BNewArray) {
                BNewArray lhsArray = (BNewArray) lhsValue;
                BNewArray rhsArray = (BNewArray) rhsValue;
                if (lhsArray.size() != rhsArray.size()) {
                    return false;
                }
                for (int i = 0; i < lhsArray.size(); i++) {
                    if (!isEqual(lhsArray.getBValue(i), rhsArray.getBValue(i))) {
                        return false;
                    }
                }
            }
            break;
        case TypeTags.XML_TAG:
            // TODO: https://www.pixelstech.net/article/1453272563-Canonicalize-XML-in-Java
            break;
        default:
            return false;
    }
    return true;
}
Also used : BRefType(org.ballerinalang.model.values.BRefType) BStructType(org.ballerinalang.model.types.BStructType) BNewArray(org.ballerinalang.model.values.BNewArray) JsonNode(org.ballerinalang.model.util.JsonNode) BJSON(org.ballerinalang.model.values.BJSON)

Example 20 with JsonNode

use of org.ballerinalang.model.util.JsonNode in project ballerina by ballerina-lang.

the class MatchStatementTest method testMatchStatementBasics14.

@Test(description = "Test basics of match statement")
public void testMatchStatementBasics14() {
    JsonNode jsonNode = new JsonNode(10);
    BJSON jsonValue = new BJSON(jsonNode);
    BValue[] returns = BRunUtil.invoke(result, "testMatchStatementBasics14", new BValue[] { jsonValue });
    Assert.assertEquals(returns.length, 1);
    Assert.assertSame(returns[0].getClass(), BString.class);
    Assert.assertEquals(returns[0].stringValue(), "int| null 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)

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