use of org.ballerinalang.model.util.JsonGenerator in project ballerina by ballerina-lang.
the class BJSON method value.
/**
* Get value associated with this {@link BJSON} object.
*
* @return JSON object associated with this {@link BJSON} object
*/
@Override
public JsonNode value() {
if (this.value == null) {
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
try {
JsonGenerator gen = new JsonGenerator(byteOut);
this.datasource.serialize(gen);
gen.flush();
this.value = JsonParser.parse(new ByteArrayInputStream(byteOut.toByteArray()));
} catch (Throwable t) {
handleJsonException("Error in building JSON node: ", t);
}
}
return this.value;
}
use of org.ballerinalang.model.util.JsonGenerator in project ballerina by ballerina-lang.
the class BJSON method serializeData.
@Override
public void serializeData(OutputStream outputStream) {
try {
/* the below order is important, where if the value is generated from a streaming data source,
* it should be able to serialize the data out again using the value */
if (this.value != null) {
this.value.serialize(outputStream);
} else {
JsonGenerator gen = new JsonGenerator(outputStream);
this.datasource.serialize(gen);
gen.flush();
}
} catch (Throwable t) {
handleJsonException("error occurred during writing the message to the output stream: ", t);
}
}
use of org.ballerinalang.model.util.JsonGenerator in project ballerina by ballerina-lang.
the class JSONLibraryTest method testBasicJsonObjectGenValues.
@Test
public void testBasicJsonObjectGenValues() throws IOException {
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);
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
JsonGenerator gen = new JsonGenerator(byteOut);
node.serialize(gen);
gen.flush();
Assert.assertEquals(json, new String(byteOut.toByteArray()));
}
use of org.ballerinalang.model.util.JsonGenerator in project ballerina by ballerina-lang.
the class DebugMsgUtil method getMsgString.
/**
* Method to generate json message from the MessageDTO object instance.
*
* @param msg object instance.
* @return json msg.
*/
public static String getMsgString(MessageDTO msg) {
if (msg == null) {
return DebugConstants.ERROR_JSON;
}
StringWriter writer = new StringWriter();
JsonGenerator gen = new JsonGenerator(writer);
try {
parseMessageDTO(msg, gen);
gen.flush();
return writer.toString();
} catch (IOException e) {
log.error("error parsing MessageDTO to json, " + e.getMessage(), e);
return DebugConstants.ERROR_JSON;
}
}
Aggregations