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);
}
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;
}
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);
}
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;
}
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");
}
Aggregations