Search in sources :

Example 6 with JsonNode

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

Example 7 with JsonNode

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

Example 8 with JsonNode

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

Example 9 with JsonNode

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

Example 10 with JsonNode

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