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

io.logspace.agent.api.json.EventJsonSerializer Maven / Gradle / Ivy

There is a newer version: 0.3.2
Show 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.agent.api.json;

import static io.logspace.agent.shaded.jackson.core.JsonEncoding.UTF8;
import static io.logspace.agent.api.event.Event.*;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Collection;

import io.logspace.agent.shaded.jackson.core.JsonGenerator;

import io.logspace.agent.api.event.Event;
import io.logspace.agent.api.event.EventProperty;

public final class EventJsonSerializer extends AbstractJsonSerializer {

    private EventJsonSerializer(JsonGenerator jsonGenerator) {
        super(jsonGenerator);
    }

    private EventJsonSerializer(OutputStream outputStream) throws IOException {
        super(outputStream);
    }

    public static void eventToJson(Event event, OutputStream outputStream) throws IOException {
        EventJsonSerializer serializer = new EventJsonSerializer(outputStream);
        serializer.serializeSingleEvent(event);
        serializer.finish();
    }

    public static String toJson(Collection events) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        toJson(events, baos);

        return baos.toString(UTF8.getJavaName());
    }

    public static void toJson(Collection events, JsonGenerator jsonGenerator) throws IOException {
        EventJsonSerializer serializer = new EventJsonSerializer(jsonGenerator);
        serializer.serialize(events);
    }

    public static void toJson(Collection events, OutputStream outputStream) throws IOException {
        EventJsonSerializer serializer = new EventJsonSerializer(outputStream);
        serializer.serialize(events);
        serializer.finish();
    }

    private void serialize(Collection events) throws IOException {
        this.startArray();

        for (Event eachEvent : events) {
            this.serializeSingleEvent(eachEvent);
        }

        this.endArray();
    }

    private void serializeSingleEvent(Event event) throws IOException {
        this.startObject();

        this.writeEvent(event);

        this.endObject();
    }

    private void writeEvent(Event event) throws IOException {
        this.writeMandatoryStringField(FIELD_ID, event.getId());
        this.writeOptionalField(FIELD_TYPE, event.getType());
        this.writeMandatoryStringField(FIELD_SYSTEM, event.getSystem());
        this.writeMandatoryStringField(FIELD_AGENT_ID, event.getAgentId());
        this.writeMandatoryDateField(FIELD_TIMESTAMP, event.getTimestamp());
        this.writeOptionalField(FIELD_PARENT_EVENT_ID, event.getParentEventId());
        this.writeOptionalField(FIELD_GLOBAL_EVENT_ID, event.getGlobalEventId());
        this.writeOptionalField(FIELD_MARKER, event.getMarker());

        this.writeProperties(EventPropertyJsonHandlers.getBooleanHandler(), event.getBooleanProperties());
        this.writeProperties(EventPropertyJsonHandlers.getDateHandler(), event.getDateProperties());
        this.writeProperties(EventPropertyJsonHandlers.getDoubleHandler(), event.getDoubleProperties());
        this.writeProperties(EventPropertyJsonHandlers.getFloatHandler(), event.getFloatProperties());
        this.writeProperties(EventPropertyJsonHandlers.getIntegerHandler(), event.getIntegerProperties());
        this.writeProperties(EventPropertyJsonHandlers.getLongHandler(), event.getLongProperties());
        this.writeProperties(EventPropertyJsonHandlers.getStringHandler(), event.getStringProperties());
    }

    private  void writeProperties(EventPropertyJsonHandler handler, Collection> properties)
            throws IOException {
        if (properties == null || properties.isEmpty()) {
            return;
        }

        handler.writeEventProperties(this.getJsonGenerator(), properties);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy