使用 Jackson 忽略 JSON 对象上的新字段

发布于 2024-10-27 14:47:12 字数 191 浏览 3 评论 0 原文

我正在使用 Jackson JSON 库将一些 JSON 对象转换为 Android 应用程序上的 POJO 类。问题是,在发布应用程序时,JSON 对象可能会发生变化并添加新字段,但目前,即使添加一个简单的 String 字段(可以安全地忽略),它也会中断。

有没有办法告诉杰克逊忽略新添加的字段? (例如 POJO 对象上不存在)?全球忽视会很棒。

I'm using Jackson JSON library to convert some JSON objects to POJO classes on an android application. The problem is, the JSON objects might change and have new fields added while the application is published, but currently it will break even when a simple String field is added, which can safely be ignored.

Is there any way to tell Jackson to ignore newly added fields? (e.g. non-existing on the POJO objects)? A global ignore would be great.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

戈亓 2024-11-03 14:47:12

Jackson 提供了可在类级别使用的注释 (JsonIgnoreProperties)。

将以下内容添加到类的顶部(而不添加到各个方法中):

@JsonIgnoreProperties(ignoreUnknown = true)
public class Foo {
    ...
}

根据您使用的 jackson 版本,您必须在当前版本中使用不同的导入:

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

在旧版本中,它已经:

import org.codehaus.jackson.annotate.JsonIgnoreProperties;

Jackson provides an annotation that can be used on class level (JsonIgnoreProperties).

Add the following to the top of your class (not to individual methods):

@JsonIgnoreProperties(ignoreUnknown = true)
public class Foo {
    ...
}

Depending on the jackson version you are using you would have to use a different import in the current version it is:

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

in older versions it has been:

import org.codehaus.jackson.annotate.JsonIgnoreProperties;
心的位置 2024-11-03 14:47:12

除了已经提到的两种机制之外,还有一个全局功能可用于抑制由未知(未映射)属性引起的所有失败:

// jackson 1.9 and before
objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// or jackson 2.0
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

这是在没有注释的情况下默认使用的,并且可以方便地回退。

In addition to 2 mechanisms already mentioned, there is also global feature that can be used to suppress all failures caused by unknown (unmapped) properties:

// jackson 1.9 and before
objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// or jackson 2.0
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

This is the default used in absence of annotations, and can be convenient fallback.

悍妇囚夫 2024-11-03 14:47:12

最新完整答案


Jackson 2使用注释的

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public class MyMappingClass {

}

请参阅 JsonIgnoreProperties

使用配置

比注释侵入性更小。

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

ObjectReader objectReader = objectMapper.reader(MyMappingClass.class);
MyMappingClass myMappingClass = objectReader.readValue(json);

请参阅 FAIL_ON_UNKNOWN_PROPERTIES杰克逊在线文档。

Up to date and complete answer with Jackson 2


Using Annotation

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public class MyMappingClass {

}

See JsonIgnoreProperties on Jackson online documentation.

Using Configuration

Less intrusive than annotation.

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

ObjectReader objectReader = objectMapper.reader(MyMappingClass.class);
MyMappingClass myMappingClass = objectReader.readValue(json);

See FAIL_ON_UNKNOWN_PROPERTIES on Jackson online documentation.

西瑶 2024-11-03 14:47:12

它可以通过两种方式实现:

  1. 标记 POJO 以忽略未知属性

    @JsonIgnoreProperties(ignoreUnknown = true)
    
  2. 配置用于序列化/反序列化 POJO/json 的 ObjectMapper,如下所示:

    ObjectMapper 映射器 =new ObjectMapper();            
    // 对于 Jackson 版本 1.X        
    mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    // 对于 Jackson 版本 2.X
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,假) 
    

it can be achieved 2 ways:

  1. Mark the POJO to ignore unknown properties

    @JsonIgnoreProperties(ignoreUnknown = true)
    
  2. Configure ObjectMapper that serializes/De-serializes the POJO/json as below:

    ObjectMapper mapper =new ObjectMapper();            
    // for Jackson version 1.X        
    mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    // for Jackson version 2.X
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) 
    
°如果伤别离去 2024-11-03 14:47:12

@JsonIgnoreProperties(ignoreUnknown = true)
对我来说效果很好。我有一个java应用程序,它在带有jdk 1.7的tomcat上运行。

@JsonIgnoreProperties(ignoreUnknown = true)
worked well for me. I have a java application which runs on tomcat with jdk 1.7.

岁月苍老的讽刺 2024-11-03 14:47:12

如果使用基于 JSON 响应的 pojo 类。如果有可能 json 更改经常在 pojo 类级别声明:

@JsonIgnoreProperties(ignoreUnknown = true)

并且在 objectMapper 中添加此内容(如果要转换):

objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);

这样代码就不会中断。

If using a pojo class based on JSON response. If chances are there that json changes frequently declare at pojo class level:

@JsonIgnoreProperties(ignoreUnknown = true)

and at the objectMapper add this if you are converting:

objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);

So that code will not break.

猫卆 2024-11-03 14:47:12

从 Jackson 2.4 及更高版本开始,发生了一些变化。
现在您可以这样做:

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

................................................ ................................

 ObjectMapper mapper = new ObjectMapper();
    // to prevent exception when encountering unknown property:
 mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

注意:基于 @annotation 的解决方案保持不变,因此如果您想使用该解决方案,请参阅其他答案。

有关更多信息,请参阅 10 分钟配置教程: https://github.com /FasterXML/jackson-databind

Starting with Jackson version 2.4 and above there have been some changes.
Here is how you do it now:

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

..........................................................................

 ObjectMapper mapper = new ObjectMapper();
    // to prevent exception when encountering unknown property:
 mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

Note: The @annotation based solution remains the same so if you like to use that see the other answers.

For more information see the 10 minutes Configuration tutorial at: https://github.com/FasterXML/jackson-databind

放飞的风筝 2024-11-03 14:47:12

确保将 @JsonIgnoreProperties(ignoreUnknown = true) 注释放置到要作为解析 JSON 响应的结果填充的父 POJO 类,而不是放置从 JSON 到 Java 的转换的类对象正在发生。

Make sure that you place the @JsonIgnoreProperties(ignoreUnknown = true) annotation to the parent POJO class which you want to populate as a result of parsing the JSON response and not the class where the conversion from JSON to Java Object is taking place.

甜点 2024-11-03 14:47:12

如上所述,注释仅在父 POJO 类中指定时才起作用,而不是在发生从 JSON 到 Java 对象的转换的类中指定。

另一种不触及父类并造成中断的替代方案是仅为您所需的映射器方法实现您自己的映射器配置。

反序列化功能的包也已移动。
DeserializationConfig.FAIL_ON_UNKNOWN_PROPERTIES 为
DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES

import org.codehaus.jackson.map.DeserializationConfig;
...
objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);

As stated above the annotations only works if this is specified in the parent POJO class and not the class where the conversion from JSON to Java Object is taking place.

The other alternative without touching the parent class and causing disruptions is to implement your own mapper config only for the mapper methods you need for this.

Also the package of the Deserialization feature has been moved.
DeserializationConfig.FAIL_ON_UNKNOWN_PROPERTIES to
DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES

import org.codehaus.jackson.map.DeserializationConfig;
...
objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
贪了杯 2024-11-03 14:47:12

对于使用 Spring Boot 的用户,您可以使用 Jackson2ObjectMapperBuilder 配置 Jackson 的默认行为。

例如:

@Bean
public Jackson2ObjectMapperBuilder configureObjectMapper() {
    Jackson2ObjectMapperBuilder oMapper = new Jackson2ObjectMapperBuilder();
    oMapper.failOnUnknownProperties(false);
    return oMapper;
}

然后你可以在任何需要的地方自动装配ObjectMapper(默认情况下,这个对象映射器也将用于http内容转换)。

For whom are using Spring Boot, you can configure the default behaviour of Jackson by using the Jackson2ObjectMapperBuilder.

For example :

@Bean
public Jackson2ObjectMapperBuilder configureObjectMapper() {
    Jackson2ObjectMapperBuilder oMapper = new Jackson2ObjectMapperBuilder();
    oMapper.failOnUnknownProperties(false);
    return oMapper;
}

Then you can autowire the ObjectMapper everywhere you need it (by default, this object mapper will also be used for http content conversion).

执着的年纪 2024-11-03 14:47:12

我正在使用 jackson-xxx 2.8.5.Maven 依赖项,例如:

<dependencies>
    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.8.5</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.8.5</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.8.5</version>
    </dependency>

</dependencies>

首先,如果您想全局忽略未知属性。您可以配置 ObjectMapper
如下所示:

ObjectMapper objectMapper = new ObjectMapper();

objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

如果你想忽略某个类,你可以在你的类上添加注释 @JsonIgnoreProperties(ignoreUnknown = true) ,如下所示:

@JsonIgnoreProperties(ignoreUnknown = true)
public class E1 {

    private String t1;

    public String getT1() {
        return t1;
    }

    public void setT1(String t1) {
        this.t1 = t1;
    }
}

I'm using jackson-xxx 2.8.5.Maven Dependency like:

<dependencies>
    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.8.5</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.8.5</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.8.5</version>
    </dependency>

</dependencies>

First,If you want ignore unknown properties globally.you can config ObjectMapper.
Like below:

ObjectMapper objectMapper = new ObjectMapper();

objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

If you want ignore some class,you can add annotation @JsonIgnoreProperties(ignoreUnknown = true) on your class like:

@JsonIgnoreProperties(ignoreUnknown = true)
public class E1 {

    private String t1;

    public String getT1() {
        return t1;
    }

    public void setT1(String t1) {
        this.t1 = t1;
    }
}
蝶…霜飞 2024-11-03 14:47:12

您可以使用 @JsonIgnore 注释 POJO 中的特定属性。

You can annotate the specific property in your POJO with @JsonIgnore.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文