All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.logspace.jvm.agent.api.json.AbstractJsonSerializer Maven / Gradle / Ivy

The newest version!
/**
 * Logspace
 * Copyright (c) 2015 Indoqa Software Design und Beratung GmbH. All rights reserved.
 * This program and the accompanying materials are made available under the terms of
 * the Eclipse Public License Version 1.0, which accompanies this distribution and
 * is available at http://www.eclipse.org/legal/epl-v10.html.
 */
package io.logspace.jvm.agent.api.json;

import static io.logspace.jvm.agent.shaded.jackson.core.JsonEncoding.UTF8;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;

import io.logspace.jvm.agent.shaded.slf4j.Logger;
import io.logspace.jvm.agent.shaded.slf4j.LoggerFactory;

import io.logspace.jvm.agent.shaded.jackson.core.JsonFactory;
import io.logspace.jvm.agent.shaded.jackson.core.JsonGenerator;
import io.logspace.jvm.agent.shaded.jackson.core.JsonGenerator.Feature;
import io.logspace.jvm.agent.shaded.jackson.core.util.DefaultPrettyPrinter;

/**
 * Base class for JSON serializers. Simplifies handling of the {@link JsonGenerator}.
 */
public abstract class AbstractJsonSerializer {

    private static final JsonFactory JSON_FACTORY = new JsonFactory();

    private final Logger logger = LoggerFactory.getLogger(EventJsonSerializer.class);

    private JsonGenerator jsonGenerator;

    protected AbstractJsonSerializer(JsonGenerator jsonGenerator) {
        this.jsonGenerator = jsonGenerator;
    }

    protected AbstractJsonSerializer(OutputStream outputStream) throws IOException {
        super();

        this.jsonGenerator = this.createJsonGenerator(outputStream);
    }

    protected void endArray() throws IOException {
        this.jsonGenerator.writeEndArray();
    }

    protected void endObject() throws IOException {
        this.jsonGenerator.writeEndObject();
    }

    protected void finish() throws IOException {
        this.jsonGenerator.flush();
        this.jsonGenerator.close();
    }

    protected JsonGenerator getJsonGenerator() {
        return this.jsonGenerator;
    }

    protected void startArray() throws IOException {
        this.jsonGenerator.writeStartArray();
    }

    protected void startObject() throws IOException {
        this.jsonGenerator.writeStartObject();
    }

    protected void writeFieldName(String fieldName) throws IOException {
        this.jsonGenerator.writeFieldName(fieldName);
    }

    protected void writeMandatoryDateField(String fieldName, Date value) throws IOException {
        JacksonUtils.writeMandatoryDateField(this.jsonGenerator, fieldName, value);
    }

    protected void writeMandatoryDoubleField(String fieldName, double value) throws IOException {
        JacksonUtils.writeMandatoryDoubleField(this.jsonGenerator, fieldName, value);
    }

    protected void writeMandatoryLongField(String fieldName, long value) throws IOException {
        JacksonUtils.writeMandatoryLongField(this.jsonGenerator, fieldName, value);
    }

    protected void writeMandatoryStringField(String fieldName, String value) throws IOException {
        JacksonUtils.writeMandatoryStringField(this.jsonGenerator, fieldName, value);
    }

    protected void writeOptionalField(String fieldName, String value) throws IOException {
        JacksonUtils.writeOptionalField(this.jsonGenerator, fieldName, value);
    }

    protected void writeOptionalIntField(String fieldName, Integer value) throws IOException {
        JacksonUtils.writeOptionalIntField(this.jsonGenerator, fieldName, value);
    }

    protected void writeString(String value) throws IOException {
        this.jsonGenerator.writeString(value);
    }

    private JsonGenerator createJsonGenerator(OutputStream baos) throws IOException {
        JsonGenerator result = JSON_FACTORY.createGenerator(baos, UTF8);

        result.configure(Feature.QUOTE_NON_NUMERIC_NUMBERS, false);
        result.configure(Feature.AUTO_CLOSE_TARGET, false);

        if (this.logger.isDebugEnabled()) {
            result.setPrettyPrinter(new DefaultPrettyPrinter());
        }

        return result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy