aboutsummaryrefslogtreecommitdiffstats
path: root/metrics-proxy/src/main/java/ai/vespa/metricsproxy/metric/model/json/JacksonUtil.java
blob: d599e75b243c3855135edecd369788b0fef82157 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.metricsproxy.metric.model.json;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;

import java.io.IOException;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.Locale;

/**
 * @author smorgrav
 * @author gjoranv
 */
public class JacksonUtil {

    private static final ThreadLocal<DecimalFormat> withinLongRangeFormat = ThreadLocal.withInitial(JacksonUtil::createWithinLongRangeFormat);
    private static final ThreadLocal<DecimalFormat> outsideLongRangeFormat = ThreadLocal.withInitial(JacksonUtil::createOutsideLongRangeFormat);
    private static DecimalFormat createWithinLongRangeFormat() {
        DecimalFormat df = new DecimalFormat("#.####", new DecimalFormatSymbols(Locale.ENGLISH));
        df.setMaximumFractionDigits(13);
        return df;
    }
    private static DecimalFormat createOutsideLongRangeFormat() {
        DecimalFormat df = new DecimalFormat("#.0###", new DecimalFormatSymbols(Locale.ENGLISH));
        df.setMaximumFractionDigits(13);
        return df;
    }
    /**
     * Returns an object mapper with a custom floating point serializer to avoid scientific notation
     */
    public static ObjectMapper createObjectMapper() {
        ObjectMapper mapper = new ObjectMapper();
        SimpleModule module = new SimpleModule("DoubleSerializer",
                                               new Version(1, 0, 0, "", null, null));
        module.addSerializer(Double.class, new DoubleSerializer());
        mapper.registerModule(module);
        return mapper;
    }
    /**
     * Returns an object mapper with a custom floating point serializer to avoid scientific notation
     */
    public static void writeDouble(JsonGenerator jgen, Double value) throws IOException {
        jgen.writeNumber(format(value));
    }

    public static String format(Double value) {
        return format(value, withinLongRangeFormat.get(), outsideLongRangeFormat.get());
    }

    private static String format(Double value, NumberFormat withinLongRange, NumberFormat outsideLongRange) {
        if ((value <= Long.MAX_VALUE) && (value >= Long.MIN_VALUE)) {
            return withinLongRange.format(value);
        } else {
            return outsideLongRange.format(value);
        }
    }

    public static class DoubleSerializer extends JsonSerializer<Double> {
        private DecimalFormat withinLongRangeFormat = createWithinLongRangeFormat();
        private DecimalFormat outsideLongRangeFormat = createOutsideLongRangeFormat();
        @Override
        public void serialize(Double value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
            jgen.writeNumber(format(value, withinLongRangeFormat, outsideLongRangeFormat));
        }
    }

}