使用ObjectMapper更改字段大小写

时间:2021-07-12 18:03:02

I think I need to create a specialist ObjectMapper and cannot find any sample code to start the process.

我想我需要创建一个专业的ObjectMapper,并且无法找到任何示例代码来启动该过程。

The creator of the JSON is using .Net and public properties and therefore uses field names with an uppercase initial. I am parsing the JSON into POJOs so I would like to use a lowercase initial.

JSON的创建者使用.Net和公共属性,因此使用具有大写首字母的字段名称。我正在将JSON解析为POJO,所以我想使用小写的首字母。

At their end:

在他们结束时:

    public class Facet
    {
        public string Name { get; set; }
        public string  Value { get; set; }
    }

At my end I must therefore have:

因此,我必须:

    public class Facet {
        public String Name;
        public String Value;
    }

I would much prefer:

我更喜欢:

    public class Facet {
        public String name;
        public String value;
    }

Am I right that this could be done with an ObjectMapper?

我是对的,这可以用ObjectMapper完成吗?

3 个解决方案

#1


24  

Your first issue can be addressed very simply with the @JsonProperty annotation:

使用@JsonProperty注释可以非常简单地解决您的第一个问题:

// java-side class
public class Facet
{
    @JsonProperty("Name")
    public String name;

    @JsonProperty("Value")
    public String value;
}

Now the ObjectMapper will match up the differently-cased field names. If you don't want to add annotations into your classes, you can create a Mix-in class to stand in for your Facet:

现在,ObjectMapper将匹配不同的字段名称。如果您不想在类中添加注释,可以创建一个Mix-in类来代替您的Facet:

public class FacetMixIn
{
    @JsonProperty("Name")
    public String name;

    @JsonProperty("Value")
    public String value;
}

objectMapper.getDeserializationConfig().addMixInAnnotations(Facet.class, FacetMixIn.class);

This will achieve the same thing, without requiring additional annotations in your Facet class.

这将实现相同的功能,而不需要在Facet类中添加其他注释。

#2


14  

Instead of annotating each field, the Jackson ObjectMapper can be configured to use a built-in or custom PropertyNamingStrategy, to apply a consistent translation between Java property/field names and JSON element names.

可以将Jackson ObjectMapper配置为使用内置或自定义PropertyNamingStrategy,而不是注释每个字段,以在Java属性/字段名称和JSON元素名称之间应用一致的转换。

For example:

例如:

myObjectMapper.setPropertyNamingStrategy(PascalCaseStrategy);

#3


6  

This problem could be solved from Jackson 2.5.0 like this:

这个问题可以从Jackson 2.5.0这样解决:

ObjectMapper mapper = new ObjectMapper();
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);

From the javadoc:

来自javadoc:

com.fasterxml.jackson.databind.MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES

com.fasterxml.jackson.databind.MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES

Feature that will allow for more forgiving deserialization of incoming JSON. If enabled, the bean properties will be matched using their lower-case equivalents, meaning that any case-combination (incoming and matching names are canonicalized by lower-casing) should work.

该功能将允许对传入的JSON进行更宽容的反序列化。如果启用,将使用它们的小写等效项匹配bean属性,这意味着任何大小写组合(传入和匹配的名称由低位大小规范化)都应该有效。

Note that there is additional performance overhead since incoming property names need to be lower-cased before comparison, for cases where there are upper-case letters. Overhead for names that are already lower-case should be negligible however.

请注意,存在额外的性能开销,因为在存在大写字母的情况下,在比较之前需要对传入的属性名称进行较低的设置。对于已经小写的名称的开销应该可以忽略不计。

Feature is disabled by default.

默认情况下禁用功能。

Since: 2.5

自:2.5

#1


24  

Your first issue can be addressed very simply with the @JsonProperty annotation:

使用@JsonProperty注释可以非常简单地解决您的第一个问题:

// java-side class
public class Facet
{
    @JsonProperty("Name")
    public String name;

    @JsonProperty("Value")
    public String value;
}

Now the ObjectMapper will match up the differently-cased field names. If you don't want to add annotations into your classes, you can create a Mix-in class to stand in for your Facet:

现在,ObjectMapper将匹配不同的字段名称。如果您不想在类中添加注释,可以创建一个Mix-in类来代替您的Facet:

public class FacetMixIn
{
    @JsonProperty("Name")
    public String name;

    @JsonProperty("Value")
    public String value;
}

objectMapper.getDeserializationConfig().addMixInAnnotations(Facet.class, FacetMixIn.class);

This will achieve the same thing, without requiring additional annotations in your Facet class.

这将实现相同的功能,而不需要在Facet类中添加其他注释。

#2


14  

Instead of annotating each field, the Jackson ObjectMapper can be configured to use a built-in or custom PropertyNamingStrategy, to apply a consistent translation between Java property/field names and JSON element names.

可以将Jackson ObjectMapper配置为使用内置或自定义PropertyNamingStrategy,而不是注释每个字段,以在Java属性/字段名称和JSON元素名称之间应用一致的转换。

For example:

例如:

myObjectMapper.setPropertyNamingStrategy(PascalCaseStrategy);

#3


6  

This problem could be solved from Jackson 2.5.0 like this:

这个问题可以从Jackson 2.5.0这样解决:

ObjectMapper mapper = new ObjectMapper();
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);

From the javadoc:

来自javadoc:

com.fasterxml.jackson.databind.MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES

com.fasterxml.jackson.databind.MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES

Feature that will allow for more forgiving deserialization of incoming JSON. If enabled, the bean properties will be matched using their lower-case equivalents, meaning that any case-combination (incoming and matching names are canonicalized by lower-casing) should work.

该功能将允许对传入的JSON进行更宽容的反序列化。如果启用,将使用它们的小写等效项匹配bean属性,这意味着任何大小写组合(传入和匹配的名称由低位大小规范化)都应该有效。

Note that there is additional performance overhead since incoming property names need to be lower-cased before comparison, for cases where there are upper-case letters. Overhead for names that are already lower-case should be negligible however.

请注意,存在额外的性能开销,因为在存在大写字母的情况下,在比较之前需要对传入的属性名称进行较低的设置。对于已经小写的名称的开销应该可以忽略不计。

Feature is disabled by default.

默认情况下禁用功能。

Since: 2.5

自:2.5