Java - 处理对象的本地化字段

标签 java object

我有几个 A 类对象,它们是使用 Json 导入的。每个都有 2 个字段。

Several Objects (of Class A)
  - field-1
  - field-2

在程序中 - 字段的值已更改。然后,这些对象将与更新的字段一起返回。

在程序中 - 我们需要为每个对象“创建并跟踪”几个“临时”字段 - 示例

Several Objects (of Class A)
  - field-1
  - field-2
  - Temp-field-3
  - Temp-field-4

运行后。对象与原始字段(即 field-1 和 field-2)一起发送回。

A 类 - 正在被多个程序使用,因此我们无法将“Temp”字段添加到类定义本身中。

现在 - 我将 A 类对象(2 个字段)映射到 B 类对象(具有 4 个字段 - 包括临时字段;程序本地)。运行结束时 - A 类对象将使用 B 类相应对象的字段值进行更新。

有更好的方法吗?

最佳答案

您可以使用 transient 或 JsonIgnore 来避免从 A 到 B(以及 B 到 A)的映射,如下所示。

下面的工作示例对此进行了解释。它考虑单个对象,但您可以轻松地将其扩展为对象列表。

这样您仍然可以控制扩展类,但性能方面它不会产生任何开销。并且您的序列化代码不受 A 类中的任何更改 - 例如添加了新字段。

假设您的 A 类有以下字段。

class A {
    String name;
    String id;
    public String getName() {return name;}
    public void setName(String name) {this.name = name;}
    public String getId() {return id;}
    public void setId(String id) {this.id = id;}
}

定义B类如下

class B extends A {
    @JsonIgnore
    transient String temp_1;
    @JsonIgnore
    transient String temp_2;
    //Getter and setters
    public String getTemp_1() {return temp_1;}
    public void setTemp_1(String temp_1) {this.temp_1 = temp_1;}
    public String getTemp_2() {return temp_2;}
    public void setTemp_2(String temp_2) {this.temp_2 = temp_2;}
}

示例代码 - 请注意,它直接反序列化为对象 B 而不是 A。

import java.io.IOException;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Sample {

    public B fromJson(String jsonA) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.readValue(jsonA, B.class);
    }

    public void doProcess(B b){
        b.setTemp_1("newTemp1");
        b.setName("newName");
    }
    public String toJson(B b) throws JsonProcessingException{
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(b);
    }

    public static void main(String[] args) 
       throws JsonParseException, JsonMappingException, IOException {

        String incomingJson = "{ \"id\": \"123\",  \"name\" : \"JM\" }";
        System.out.println("Input->  " + incomingJson);
        Sample sample = new Sample();
        B b = sample.fromJson(incomingJson);
        sample.doProcess(b);
        String modifiedJson = sample.toJson(b);
        System.out.println("Output-> "+modifiedJson);
    }
}

输出

Input->  { "id": "123",  "name" : "JM" }
Output-> {"name":"newName","id":"123"}

关于Java - 处理对象的本地化字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32827451/

相关文章:

javascript - 从不同级别的对象中按属性名称获取值

java - 类转换异常 : cannot assign instance of

Java:显式设置 HashMap 的键并保留对其的引用

java - 在文本文件中间插入一个字符串而不替换

java - 为什么当我使用 hibernate 时我的表会被清除?

javascript - 如何创建类似于 string.split() 的自定义函数来反转字符串

java - 两个相同的类可以序列化和反序列化吗?

java - Spring返回List时预期 ':'而不是 't'错误?

java - Java 是否有办法让非库开发人员使用扩展方法?

javascript - 如何在javascript中按不同的键进行分组?