Search in sources :

Example 1 with MetricTreeVisitor

use of org.platformlayer.metrics.MetricTreeVisitor in project platformlayer by platformlayer.

the class MetricTreeSerializer method serialize.

public void serialize(MetricTreeBase tree, OutputStream os) throws IOException {
    final JsonGenerator jsonGenerator = jsonFactory.createJsonGenerator(os);
    tree.accept(new MetricTreeVisitor() {

        private void writeKey(MetricTreeBase o) throws JsonGenerationException, IOException {
            if (o.getKey() != null) {
                jsonGenerator.writeFieldName(o.getKey());
            }
        }

        @Override
        public void visit(MetricTreeObject o) {
            try {
                writeKey(o);
                jsonGenerator.writeStartObject();
                o.visitChildren(this);
                jsonGenerator.writeEndObject();
            } catch (IOException e) {
                throw new IllegalStateException("Error serializing to JSON", e);
            }
        }

        @Override
        public void visit(MetricTreeString o) {
            try {
                writeKey(o);
                jsonGenerator.writeString(o.getValue());
            } catch (IOException e) {
                throw new IllegalStateException("Error serializing to JSON", e);
            }
        }

        @Override
        public void visit(MetricTreeArray o) {
            try {
                writeKey(o);
                jsonGenerator.writeStartArray();
                o.visitItems(this);
                jsonGenerator.writeEndArray();
            } catch (IOException e) {
                throw new IllegalStateException("Error serializing to JSON", e);
            }
        }

        @Override
        public void visit(MetricTreeInteger o) {
            try {
                writeKey(o);
                jsonGenerator.writeNumber(o.getValue());
            } catch (IOException e) {
                throw new IllegalStateException("Error serializing to JSON", e);
            }
        }

        @Override
        public void visit(MetricTreeFloat o) {
            try {
                writeKey(o);
                jsonGenerator.writeNumber(o.getValue());
            } catch (IOException e) {
                throw new IllegalStateException("Error serializing to JSON", e);
            }
        }
    });
    jsonGenerator.close();
}
Also used : MetricTreeString(org.platformlayer.metrics.MetricTreeBase.MetricTreeString) MetricTreeVisitor(org.platformlayer.metrics.MetricTreeVisitor) MetricTreeObject(org.platformlayer.metrics.MetricTreeObject) MetricTreeInteger(org.platformlayer.metrics.MetricTreeBase.MetricTreeInteger) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) MetricTreeBase(org.platformlayer.metrics.MetricTreeBase) MetricTreeFloat(org.platformlayer.metrics.MetricTreeBase.MetricTreeFloat) IOException(java.io.IOException) MetricTreeArray(org.platformlayer.metrics.MetricTreeBase.MetricTreeArray) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException)

Aggregations

JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)1 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 IOException (java.io.IOException)1 MetricTreeBase (org.platformlayer.metrics.MetricTreeBase)1 MetricTreeArray (org.platformlayer.metrics.MetricTreeBase.MetricTreeArray)1 MetricTreeFloat (org.platformlayer.metrics.MetricTreeBase.MetricTreeFloat)1 MetricTreeInteger (org.platformlayer.metrics.MetricTreeBase.MetricTreeInteger)1 MetricTreeString (org.platformlayer.metrics.MetricTreeBase.MetricTreeString)1 MetricTreeObject (org.platformlayer.metrics.MetricTreeObject)1 MetricTreeVisitor (org.platformlayer.metrics.MetricTreeVisitor)1