use of org.platformlayer.metrics.MetricTreeBase.MetricTreeArray 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();
}
Aggregations