blob: c4f2b7198a4406c077ffaa3c75493c60faae79ed (
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
|
// 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.util.Locale;
/**
* @author smorgrav
* @author gjoranv
*/
public class JacksonUtil {
private static final DecimalFormat decimalFormat = createDecimalFormat();
private static DecimalFormat createDecimalFormat() {
DecimalFormat df = new DecimalFormat("#.####", 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(decimalFormat.format(value));
}
public static class DoubleSerializer extends JsonSerializer<Double> {
@Override
public void serialize(Double value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
writeDouble(jgen, value);
}
}
}
|