Search in sources :

Example 66 with BJSON

use of org.ballerinalang.model.values.BJSON in project ballerina by ballerina-lang.

the class XMLNativeFunctionTest method testToJsonXMLWithSingleEmptyElementAndAttributes.

@Test
public void testToJsonXMLWithSingleEmptyElementAndAttributes() {
    String xmlStr = "<ns0:name test=\"5\" xmlns:ns0=\"http://sample0.com/test\"></ns0:name>";
    BValue[] args = { new BXMLItem(xmlStr) };
    BValue[] returns = BRunUtil.invoke(result, "testToJSON", args);
    Assert.assertTrue(returns[0] instanceof BJSON);
    Assert.assertEquals(returns[0].stringValue(), "{\"ns0:name\":{\"@xmlns:ns0\":\"http://sample0.com/test\"," + "\"@test\":\"5\"}}");
}
Also used : BXMLItem(org.ballerinalang.model.values.BXMLItem) BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BJSON(org.ballerinalang.model.values.BJSON) Test(org.testng.annotations.Test)

Example 67 with BJSON

use of org.ballerinalang.model.values.BJSON in project ballerina by ballerina-lang.

the class XMLNativeFunctionTest method testToJsonForSingleElement.

@Test
public void testToJsonForSingleElement() {
    String xmlStr = "<key>value</key>";
    BValue[] args = { new BXMLItem(xmlStr) };
    BValue[] returns = BRunUtil.invoke(result, "testToJSON", args);
    Assert.assertTrue(returns[0] instanceof BJSON);
    Assert.assertEquals(returns[0].stringValue(), "{\"key\":\"value\"}");
}
Also used : BXMLItem(org.ballerinalang.model.values.BXMLItem) BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BJSON(org.ballerinalang.model.values.BJSON) Test(org.testng.annotations.Test)

Example 68 with BJSON

use of org.ballerinalang.model.values.BJSON in project ballerina by ballerina-lang.

the class BAnyTypeSuccessScenariosTest method testAnyToAnyCast.

@Test(description = "Test any to any explicit cast")
public void testAnyToAnyCast() {
    BValue[] returns = BRunUtil.invoke(result, "anyToAnyExplicitCasting", new BValue[0]);
    Assert.assertEquals(returns.length, 1);
    Assert.assertSame(returns[0].getClass(), BJSON.class);
    BJSON json = (BJSON) returns[0];
    Assert.assertEquals(json.stringValue(), "{\"PropertyName\":\"Value\"}", "Invalid json value returned.");
}
Also used : BValue(org.ballerinalang.model.values.BValue) BJSON(org.ballerinalang.model.values.BJSON) Test(org.testng.annotations.Test)

Example 69 with BJSON

use of org.ballerinalang.model.values.BJSON in project ballerina by ballerina-lang.

the class BAnyTypeSuccessScenariosTest method testMultipleReturnWithAny.

@Test(description = "Test Multiple returns with any")
public void testMultipleReturnWithAny() {
    BValue[] returns = BRunUtil.invoke(result, "multipleReturnWithAny", new BValue[0]);
    Assert.assertEquals(returns.length, 2);
    Assert.assertSame(returns[0].getClass(), BJSON.class);
    Assert.assertSame(returns[1].getClass(), BInteger.class);
    BJSON json = (BJSON) returns[0];
    BInteger intVal = (BInteger) returns[1];
    Assert.assertEquals(json.stringValue(), "{\"PropertyName\":\"Value\"}", "Invalid json value returned.");
    Assert.assertEquals(intVal.intValue(), 7, "Invalid int value returned.");
}
Also used : BValue(org.ballerinalang.model.values.BValue) BInteger(org.ballerinalang.model.values.BInteger) BJSON(org.ballerinalang.model.values.BJSON) Test(org.testng.annotations.Test)

Example 70 with BJSON

use of org.ballerinalang.model.values.BJSON 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)

Aggregations

BJSON (org.ballerinalang.model.values.BJSON)255 Test (org.testng.annotations.Test)214 BValue (org.ballerinalang.model.values.BValue)113 HTTPTestRequest (org.ballerinalang.test.services.testutils.HTTPTestRequest)108 HTTPCarbonMessage (org.wso2.transport.http.netty.message.HTTPCarbonMessage)108 HttpMessageDataStreamer (org.wso2.transport.http.netty.message.HttpMessageDataStreamer)104 BString (org.ballerinalang.model.values.BString)85 BXMLItem (org.ballerinalang.model.values.BXMLItem)37 BStruct (org.ballerinalang.model.values.BStruct)23 BXML (org.ballerinalang.model.values.BXML)17 OMNode (org.apache.axiom.om.OMNode)13 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)13 BMap (org.ballerinalang.model.values.BMap)11 JsonNode (org.ballerinalang.model.util.JsonNode)10 BNewArray (org.ballerinalang.model.values.BNewArray)9 BIntArray (org.ballerinalang.model.values.BIntArray)8 BRefType (org.ballerinalang.model.values.BRefType)8 ArrayList (java.util.ArrayList)7 BStringArray (org.ballerinalang.model.values.BStringArray)7 BStructType (org.ballerinalang.model.types.BStructType)6