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

org.icij.extract.json.ReportSerializer Maven / Gradle / Ivy

There is a newer version: 7.4.0
Show newest version
package org.icij.extract.json;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.icij.event.Notifiable;
import org.icij.extract.extractor.ExtractionStatus;
import org.icij.extract.report.Report;
import org.icij.extract.report.ReportMap;

import java.io.IOException;
import java.nio.file.Path;
import java.util.Iterator;
import java.util.Map;

/**
 * Serializes a {@link ReportMap} to JSON.
 *
 *
 */
public class ReportSerializer extends JsonSerializer {

	private final Notifiable monitor;
	private final ExtractionStatus match;

	public ReportSerializer(final Notifiable monitor, final ExtractionStatus match) {
		this.monitor = monitor;
		this.match = match;
	}

	@Override
	public void serialize(final ReportMap reportMap, final JsonGenerator jsonGenerator, final SerializerProvider provider)
		throws IOException {
		final Iterator> iterator = reportMap.entrySet().iterator();

		jsonGenerator.writeStartObject();
		while (iterator.hasNext()) {
			final Map.Entry entry = iterator.next();
			final ExtractionStatus result = entry.getValue().getStatus();

			if (null == match || result.equals(match)) {
				jsonGenerator.writeObjectField(entry.getKey().toString(), result.getCode());
			}

			if (null != monitor) {
				monitor.notifyListeners();
			}
		}

		jsonGenerator.writeEndObject();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy