com.basistech.rosette.dm.jackson.DoubleSerializer Maven / Gradle / Ivy
/*
* Copyright 2017 Basis Technology Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.basistech.rosette.dm.jackson;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;
public class DoubleSerializer extends StdSerializer {
private final NumberFormat numberFormat;
public DoubleSerializer() {
super(Double.class);
NumberFormat f = NumberFormat.getInstance(Locale.US);
if (f instanceof DecimalFormat) {
f.setMaximumFractionDigits(8);
}
f.setGroupingUsed(false);
numberFormat = f;
}
/**
* Method to serialize Double values in {@link com.basistech.rosette.dm.AnnotatedText} globally.
*
* @param value Value to serialize; can not be null.
* @param gen Generator used to output resulting Json content
* @param serializers Provider that can be used to get serializers for
*/
@Override
public void serialize(Double value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
// truncate to 8 digits below decimal for all Double fields.
gen.writeNumber(Double.parseDouble(numberFormat.format(value)));
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy