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