Search in sources :

Example 1 with JsonGenerator

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;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) JsonGenerator(org.ballerinalang.model.util.JsonGenerator) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 2 with JsonGenerator

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

Example 3 with JsonGenerator

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

Example 4 with JsonGenerator

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;
    }
}
Also used : StringWriter(java.io.StringWriter) JsonGenerator(org.ballerinalang.model.util.JsonGenerator) IOException(java.io.IOException)

Aggregations

JsonGenerator (org.ballerinalang.model.util.JsonGenerator)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 JsonNode (org.ballerinalang.model.util.JsonNode)1 Test (org.testng.annotations.Test)1