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

io.logspace.jvm.agent.api.json.AgentControllerDescriptionJsonSerializer 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 static io.logspace.jvm.agent.api.AgentControllerDescription.FIELD_CLASS_NAME;
import static io.logspace.jvm.agent.api.AgentControllerDescription.FIELD_ID;
import static io.logspace.jvm.agent.api.AgentControllerDescription.FIELD_PARAMETERS;
import io.logspace.jvm.agent.api.AgentControllerDescription;
import io.logspace.jvm.agent.api.AgentControllerDescription.Parameter;

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

public final class AgentControllerDescriptionJsonSerializer extends AbstractJsonSerializer {

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

    public static String toJson(AgentControllerDescription description) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        toJson(description, baos);

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

    public static void toJson(AgentControllerDescription description, OutputStream outputStream) throws IOException {
        new AgentControllerDescriptionJsonSerializer(outputStream).serialize(description);
    }

    private void serialize(AgentControllerDescription description) throws IOException {
        this.startObject();

        this.writeAttributes(description);
        this.writeParameters(description);

        this.endObject();

        this.finish();
    }

    private void writeAttributes(AgentControllerDescription description) throws IOException {
        this.writeMandatoryStringField(FIELD_ID, description.getId());
        this.writeMandatoryStringField(FIELD_CLASS_NAME, description.getClassName());
    }

    private void writeParameters(AgentControllerDescription description) throws IOException {
        if (description.getParameterCount() == 0) {
            return;
        }

        this.writeFieldName(FIELD_PARAMETERS);

        this.startObject();

        for (Parameter eachParameter : description.getParameters()) {
            this.writeMandatoryStringField(eachParameter.getName(), eachParameter.getValue());
        }

        this.endObject();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy