Commit 65e97636 authored by Sören Henning's avatar Sören Henning

added BetterJSONExporter to use Jackson JSON library like Spring Boot

parent 163cf1c5
......@@ -26,7 +26,7 @@ public class AnomalyDetectionConfiguration extends Configuration {
new RegressionForecaster());
final SimpleAlertStage alerter = new SimpleAlertStage();
final PrinterStage printer = new PrinterStage(); // TODO Temp
final JSONExporter jsonExporter = new JSONExporter(new File("values.json")); // TODO Temp
final BetterJSONExporter jsonExporter = new BetterJSONExporter(new File("values.json")); // TODO Temp
// Connect the stages
super.connectPorts(generator.getOutputPort(), anomalyDetector.getInputPort());
......
package anomalydetection;
import java.io.File;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import anomalydetection.measurement.AnomalyScoredMeasurement;
import teetime.framework.AbstractConsumerStage;
public class BetterJSONExporter extends AbstractConsumerStage<AnomalyScoredMeasurement> {
private final JsonGenerator jsonGenerator;
public BetterJSONExporter(final File file) {
try {
JsonFactory jsonFactory = new JsonFactory();
this.jsonGenerator = jsonFactory.createGenerator(file, JsonEncoding.UTF8);
this.jsonGenerator.writeStartArray();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new IllegalStateException(e);
}
}
@Override
protected void execute(final AnomalyScoredMeasurement measurement) {
try {
this.jsonGenerator.writeStartObject();
this.jsonGenerator.writeNumberField("time", measurement.getTime().toEpochMilli());
this.jsonGenerator.writeNumberField("measurement", measurement.getValue());
this.jsonGenerator.writeNumberField("prediction", measurement.getPrediction());
this.jsonGenerator.writeNumberField("anomalyscore", measurement.getAnomalyScore());
this.jsonGenerator.writeEndObject();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new IllegalStateException(e);
}
}
@Override
public void onTerminating() throws Exception {
jsonGenerator.writeEndArray();
jsonGenerator.close();
super.onTerminating();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment