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

com.izettle.metrics.influxdb.utils.InfluxDbWriteObjectSerializer Maven / Gradle / Ivy

There is a newer version: 1.3.4
Show newest version
package com.izettle.metrics.influxdb.utils;

import java.io.IOException;
import java.util.Map;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.izettle.metrics.influxdb.data.InfluxDbWriteObject;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;

public class InfluxDbWriteObjectSerializer {

    protected static class MapSerializer extends JsonSerializer> {
        @Override
        public void serialize(final Map influxDbMap, final JsonGenerator jsonGenerator, final SerializerProvider provider)
                throws IOException {
            if (influxDbMap != null) {
                jsonGenerator.writeStartObject();
                for (Map.Entry entry : influxDbMap.entrySet()) {
                    jsonGenerator.writeFieldName(entry.getKey().toString());
                    jsonGenerator.writeObject(entry.getValue());
                }
                jsonGenerator.writeEndObject();
            }
        }
    }

    private final ObjectMapper objectMapper;

    public InfluxDbWriteObjectSerializer() {
        objectMapper = new ObjectMapper();
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);

        final SimpleModule module = new SimpleModule("SimpleModule", new Version(1, 0, 0, null, null, null));
        module.addSerializer(Map.class, new MapSerializer());
        objectMapper.registerModule(module);
    }

    public String getJsonString(InfluxDbWriteObject influxDbWriteObject) throws Exception {
        return objectMapper.writeValueAsString(influxDbWriteObject);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy