Example usage for com.fasterxml.jackson.core JsonGenerator writeRawValue

List of usage examples for com.fasterxml.jackson.core JsonGenerator writeRawValue

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core JsonGenerator writeRawValue.

Prototype

public abstract void writeRawValue(String text) throws IOException, JsonGenerationException;

Source Link

Document

Method that will force generator to copy input text verbatim without any modifications, but assuming it must constitute a single legal JSON value (number, string, boolean, null, Array or List).

Usage

From source file:com.arpnetworking.logback.serialization.ArrayOfJsonSerialziationStrategy.java

/**
 * Serialize an event.//from   www .j  av a 2 s  . c o m
 *
 * @param event The event.
 * @param eventName The event name.
 * @param keys The message keys.
 * @param jsonValues The message json values.
 * @return Serialization of message as a <code>String</code>.
 */
public String serialize(final ILoggingEvent event, final String eventName, final String[] keys,
        final String[] jsonValues) {
    final StringWriter jsonWriter = new StringWriter();
    try {
        final JsonGenerator jsonGenerator = _jsonFactory.createGenerator(jsonWriter);
        // Start wrapper
        startStenoWrapper(event, eventName, jsonGenerator, _objectMapper);

        // Write event data
        jsonGenerator.writeObjectFieldStart("data");
        final int argsLength = jsonValues == null ? 0 : jsonValues.length;
        if (keys != null) {
            for (int i = 0; i < keys.length; i++) {
                if (i >= argsLength) {
                    jsonGenerator.writeObjectField(keys[i], null);
                } else {
                    jsonGenerator.writeFieldName(keys[i]);
                    jsonGenerator.writeRawValue(jsonValues[i]);
                }
            }
        }
        jsonGenerator.writeEndObject(); // End 'data' field

        // Output throwable
        writeThrowable(event.getThrowableProxy(), jsonGenerator, _objectMapper);

        // End wrapper
        endStenoWrapper(event, eventName, jsonGenerator, _objectMapper);
    } catch (final IOException e) {
        return "Unknown exception: " + e.getMessage();
    }

    return jsonWriter.toString();
}

From source file:de.fraunhofer.iosb.ilt.sta.serialize.EntitySerializer.java

protected void serializeFieldCustomized(Entity entity, JsonGenerator gen, BeanPropertyDefinition property,
        List<BeanPropertyDefinition> properties, CustomSerialization annotation) throws Exception {
    // check if encoding field is present in current bean
    // get calue//  www  .j ava  2  s  . c o  m
    // call CustomSerializationManager
    Optional<BeanPropertyDefinition> encodingProperty = properties.stream()
            .filter(p -> p.getName().equals(annotation.encoding())).findFirst();
    if (!encodingProperty.isPresent()) {
        // TODO use more specific exception type
        throw new Exception("can not serialize instance of class '" + entity.getClass() + "'! \n"
                + "Reason: trying to use custom serialization for field '" + property.getName()
                + "' but field '" + annotation.encoding() + "' specifying enconding is not present!");
    }
    Object value = encodingProperty.get().getAccessor().getValue(entity);
    String encodingType = null;
    if (value != null) {
        encodingType = value.toString();
    }
    String customJson = CustomSerializationManager.getInstance().getSerializer(encodingType)
            .serialize(property.getAccessor().getValue(entity));
    if (customJson != null && !customJson.isEmpty()) {
        gen.writeFieldName(property.getName());
        gen.writeRawValue(customJson);
    }
}

From source file:org.apache.flink.runtime.jobgraph.jsonplan.JsonPlanGenerator.java

public static String generatePlan(JobGraph jg) {
    try {/*w w  w .j a  v  a2s  .  c  o  m*/
        final StringWriter writer = new StringWriter(1024);

        final JsonFactory factory = new JsonFactory();
        final JsonGenerator gen = factory.createGenerator(writer);

        // start of everything
        gen.writeStartObject();
        gen.writeStringField("jid", jg.getJobID().toString());
        gen.writeStringField("name", jg.getName());
        gen.writeArrayFieldStart("nodes");

        // info per vertex
        for (JobVertex vertex : jg.getVertices()) {

            String operator = vertex.getOperatorName() != null ? vertex.getOperatorName() : NOT_SET;

            String operatorDescr = vertex.getOperatorDescription() != null ? vertex.getOperatorDescription()
                    : NOT_SET;

            String optimizerProps = vertex.getResultOptimizerProperties() != null
                    ? vertex.getResultOptimizerProperties()
                    : EMPTY;

            String description = vertex.getOperatorPrettyName() != null ? vertex.getOperatorPrettyName()
                    : vertex.getName();

            // make sure the encoding is HTML pretty
            description = StringEscapeUtils.escapeHtml4(description);
            description = description.replace("\n", "<br/>");
            description = description.replace("\\", "&#92;");

            operatorDescr = StringEscapeUtils.escapeHtml4(operatorDescr);
            operatorDescr = operatorDescr.replace("\n", "<br/>");

            gen.writeStartObject();

            // write the core properties
            gen.writeStringField("id", vertex.getID().toString());
            gen.writeNumberField("parallelism", vertex.getParallelism());
            gen.writeStringField("operator", operator);
            gen.writeStringField("operator_strategy", operatorDescr);
            gen.writeStringField("description", description);

            if (!vertex.isInputVertex()) {
                // write the input edge properties
                gen.writeArrayFieldStart("inputs");

                List<JobEdge> inputs = vertex.getInputs();
                for (int inputNum = 0; inputNum < inputs.size(); inputNum++) {
                    JobEdge edge = inputs.get(inputNum);
                    if (edge.getSource() == null) {
                        continue;
                    }

                    JobVertex predecessor = edge.getSource().getProducer();

                    String shipStrategy = edge.getShipStrategyName();
                    String preProcessingOperation = edge.getPreProcessingOperationName();
                    String operatorLevelCaching = edge.getOperatorLevelCachingDescription();

                    gen.writeStartObject();
                    gen.writeNumberField("num", inputNum);
                    gen.writeStringField("id", predecessor.getID().toString());

                    if (shipStrategy != null) {
                        gen.writeStringField("ship_strategy", shipStrategy);
                    }
                    if (preProcessingOperation != null) {
                        gen.writeStringField("local_strategy", preProcessingOperation);
                    }
                    if (operatorLevelCaching != null) {
                        gen.writeStringField("caching", operatorLevelCaching);
                    }

                    gen.writeStringField("exchange", edge.getSource().getResultType().name().toLowerCase());

                    gen.writeEndObject();
                }

                gen.writeEndArray();
            }

            // write the optimizer properties
            gen.writeFieldName("optimizer_properties");
            gen.writeRawValue(optimizerProps);

            gen.writeEndObject();
        }

        // end of everything
        gen.writeEndArray();
        gen.writeEndObject();

        gen.close();

        return writer.toString();
    } catch (Exception e) {
        throw new RuntimeException("Failed to generate plan", e);
    }
}