如何使用 Jackson 使用本地化小数点分隔符反序列化浮点值 [英] How to deserialize a float value with a localized decimal separator with Jackson

查看:32
本文介绍了如何使用 Jackson 使用本地化小数点分隔符反序列化浮点值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Jackson 解析的输入流包含纬度和经度值,例如此处:

The input stream I am parsing with Jackson contains latitude and longitude values such as here:

{
    "name": "product 23",
    "latitude": "52,48264",
    "longitude": "13,31822"
}

出于某种原因,服务器使用 逗号 作为小数点分隔符,这会产生 InvalidFormatException.由于我无法更改服务器输出格式,我想教 Jackson 的 ObjectMapper 处理这些情况.相关代码如下:

For some reason the server uses commas as the decimal separator which produces an InvalidFormatException. Since I cannot change the server output format I would like to teach Jackson's ObjectMapper to handle those cases. Here is the relevant code:

public static Object getProducts(final String inputStream) {
    ObjectMapper objectMapper = new ObjectMapper();
    try {
        return objectMapper.readValue(inputStream,
                new TypeReference<Product>() {}
        );
    } catch (UnrecognizedPropertyException e) {
        e.printStackTrace();
    } catch (InvalidFormatException e) {
        e.printStackTrace();
    } catch (JsonMappingException e) {
        e.printStackTrace();
    } catch (JsonParseException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

这里是 POJO:

import com.fasterxml.jackson.annotation.JsonProperty;

public class Product {

    @JsonProperty("name")
    public String name;
    @JsonProperty("latitude")
    public float latitude;
    @JsonProperty("longitude")
    public float longitude;

}

我如何告诉 Jackson 这些坐标值带有德国语言环境?

How can I tell Jackson that those coordinate values come with a German locale?

我想 此处讨论的特定字段的自定义反序列化器 将是要走的路.我起草了这个:

I suppose a custom deserializer for the specific fields as discussed here would be the way to go. I drafted this:

public class GermanFloatDeserializer extends JsonDeserializer<Float> {

    @Override
    public Float deserialize(JsonParser parser, DeserializationContext context)
            throws IOException {
        // TODO Do some comma magic
        return floatValue;
    }

}

然后 POJO 将如下所示:

Then the POJO would look like this:

import com.fasterxml.jackson.annotation.JsonProperty;

public class Product {

    @JsonProperty("name")
    public String name;
    @JsonDeserialize(using = GermanFloatDeserializer.class, as = Float.class)
    @JsonProperty("latitude")
    public float latitude;
    @JsonDeserialize(using = GermanFloatDeserializer.class, as = Float.class)
    @JsonProperty("longitude")
    public float longitude;

}

推荐答案

我想出了以下解决方案:

I came up with the following solution:

public class FlexibleFloatDeserializer extends JsonDeserializer<Float> {

    @Override
    public Float deserialize(JsonParser parser, DeserializationContext context)
            throws IOException {
        String floatString = parser.getText();
        if (floatString.contains(",")) {
            floatString = floatString.replace(",", ".");
        }
        return Float.valueOf(floatString);
    }

}

...

public class Product {

    @JsonProperty("name")
    public String name;
    @JsonDeserialize(using = FlexibleFloatDeserializer.class)
    @JsonProperty("latitude")
    public float latitude;
    @JsonDeserialize(using = FlexibleFloatDeserializer.class)
    @JsonProperty("longitude")
    public float longitude;

}

我仍然想知道为什么当我将返回值类指定为 as = Float.class 时它不会不起作用,可以在 JsonDeserialize 文档一>.它读起来好像我应该使用一个或另一个而不是两个.无论如何,文档还声称 as = 在定义 using = 时将被忽略:

Still I wonder why I it does not work when I specify the return value class as as = Float.class as can be found in the documentation of JsonDeserialize. It reads as if I am supposed to use one or the other but not both. Whatsoever, the docs also claim that as = will be ignored when using = is defined:

如果 using() 也被使用,它具有优先级(因为它直接指定了反序列化器,而这只会用于定位反序列化器)并且这个注解属性的值被忽略.

if using() is also used it has precedence (since it directly specified deserializer, whereas this would only be used to locate the deserializer) and value of this annotation property is ignored.

这篇关于如何使用 Jackson 使用本地化小数点分隔符反序列化浮点值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆