use of org.ballerinalang.model.util.JsonNode in project ballerina by ballerina-lang.
the class JSONLibraryTest method testMismatchQuotes.
@Test(expectedExceptions = { BallerinaException.class }, expectedExceptionsMessageRegExp = "expected , or ] at line: 1 column: 32")
public void testMismatchQuotes() throws IOException {
String json = "{'fruits':[\"apple', 'orange', \"grapes\"]}";
JsonNode node = JsonParser.parse(json);
Assert.assertEquals(node.toString(), "{\"fruits\":[\"apple\",\"orange\",\"grapes\"]}");
}
use of org.ballerinalang.model.util.JsonNode in project ballerina by ballerina-lang.
the class JSONLibraryTest method testBasicJsonObjectParseValues.
@Test
public void testBasicJsonObjectParseValues() {
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);
Assert.assertEquals(node.get("a").stringValue(), "abc");
Assert.assertEquals(node.get("b").longValue(), 1);
Assert.assertEquals(node.get("c").doubleValue(), 3.14);
Assert.assertEquals(node.get("d").booleanValue(), true);
Assert.assertEquals(node.get("e").booleanValue(), false);
Assert.assertEquals(node.get("f").isNull(), true);
JsonNode objNode = node.get("g");
Assert.assertEquals(objNode.get("1").stringValue(), "a");
Assert.assertEquals(objNode.get("2").stringValue(), "b");
JsonNode arrayNode = node.get("h");
Assert.assertEquals(arrayNode.size(), 4);
Assert.assertEquals(arrayNode.get(0).stringValue(), "A");
Assert.assertEquals(arrayNode.get(1).longValue(), 20);
Assert.assertEquals(arrayNode.get(2).longValue(), 30);
Assert.assertEquals(arrayNode.get(3).stringValue(), "D");
}
use of org.ballerinalang.model.util.JsonNode in project ballerina by ballerina-lang.
the class JSONLibraryTest method testSingleQuoteInArrayElements.
@Test
public void testSingleQuoteInArrayElements() throws IOException {
String json = "{'fruits':['apple', 'orange', \"grapes\"]}";
JsonNode node = JsonParser.parse(json);
Assert.assertEquals(node.toString(), "{\"fruits\":[\"apple\",\"orange\",\"grapes\"]}");
}
use of org.ballerinalang.model.util.JsonNode in project ballerina by ballerina-lang.
the class ParseJson method execute.
@Override
public void execute(Context context) {
try {
String value = context.getStringArgument(0);
JsonNode jsonNode = JsonParser.parse(value);
context.setReturnValues(new BJSON(jsonNode));
} catch (Throwable e) {
BStruct error = Utils.createConversionError(context, "Failed to parse json string: " + e.getMessage());
context.setReturnValues(error);
}
}
use of org.ballerinalang.model.util.JsonNode in project ballerina by ballerina-lang.
the class MatchStatementTest method testMatchStatementBasics15.
@Test(description = "Test basics of match statement")
public void testMatchStatementBasics15() {
JsonNode jsonNode = new JsonNode(false);
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(), "json string | boolean matched");
}
Aggregations