Java将一个序列化对象转换为另一个对象

huangapple go评论66阅读模式
英文:

Java convert a serialized object to another object

问题

我有两个Java数据对象,ObjectFloat用于反序列化传入的请求,ObjectInt用于传出的请求。我从HTTP调用中接收ObjectFloat,其中包含Float属性,对其进行反序列化,然后调用upgradeMembers()方法。因此,在将对象发送出去之前,我想要使用upgradeMembers()处理每个m_member1m_member2属性,然后将其作为类型ObjectInt发送出去。

ObjectFloat:

public class ObjectFloat {
    @SerializedName("member_number_1")
    protected Float m_member1;

    @SerializedName("member_number_2")
    protected Float m_member2;

    public void upgradeMembers() {
        m_member1 = m_member1 * 100;
        m_member2 = m_member2 * 100;
    }
}

ObjectInt:

public class ObjectInt {
    @SerializedName("member_number_1")
    protected Integer m_member1;

    @SerializedName("member_number_2")
    protected Integer m_member2;
}

在将对象发送出去之前,您可以执行以下步骤将ObjectFloat转换为ObjectInt(不保留小数部分):

  1. 使用ObjectFloat对象调用upgradeMembers()方法,以确保m_member1m_member2已被处理。
  2. 使用Gson库将ObjectFloat对象转换为JSON字符串。
  3. 使用Gson库将JSON字符串转换回ObjectInt对象。

以下是示例代码:

// 步骤1: 调用upgradeMembers()方法
ObjectFloat objectFloat = new ObjectFloat();
objectFloat.upgradeMembers();

// 步骤2: 将ObjectFloat转换为JSON字符串
Gson gson = new Gson();
String json = gson.toJson(objectFloat);

// 步骤3: 将JSON字符串转换为ObjectInt对象
ObjectInt objectInt = gson.fromJson(json, ObjectInt.class);

这样,您可以在不过度复杂化的情况下将ObjectFloat转换为ObjectInt并将其发送出去。

英文:

I have two java data objects ObjectFloat is for deseriealize incoming requests, and ObjectInt is for outgoing requests. I receive ObjectFloat from an HTTP call with Float attributes, deserialize it, and call upgradeMembers(). So before I send the object out I want to process each m_member1 and m_member1 attribute with upgradeMembers() and then send it out as a type ObjectInt.

Object float:

public class ObjectFloat {
    @SerializedName("member_number_1")
    protected Float m_member1;

    @SerializedName("member_number_2")
    protected Float m_member2;

    public void upgradeMembers() {
        m_member1 = m_member1 * 100
        m_member1 = m_member1 * 100
    }
}

Object integer:

public class ObjectInt {
    @SerializedName("member_number_1")
    protected Integer m_member1;

    @SerializedName("member_number_2")
    protected Integer m_member2;

}

How would I be able to covert ObjectFloat to ObjectInt before I send the object out? (I don't mind losing the decimals). I was thinking maybe I could call m_gson.toJson(objectfloat) and the serialize it back to with fromJson ObjectInt but when I send it out I would have to serialize it again with toJson() would that be overkill?

答案1

得分: 1

你可以添加一个以ObjectFloat为参数的构造函数。这样,你就可以基于ObjectFloat的实例创建ObjectInt的实例:

public class ObjectInt {
    protected Integer m_member1;
    protected Integer m_member2;

    public ObjectInt() {
        // 默认构造函数允许创建一个不依赖ObjectFloat的实例
    }

    public ObjectInt(ObjectFloat objectFloat) {
        this.m_member1 = objectFloat.m_member1.intValue();
        this.m_member2 = objectFloat.m_member2.intValue();
    }
}

当你添加了这个构造函数后,你可以使用以下方式将ObjectFloat“转换”为ObjectInt

ObjectInt objectInt = new ObjectInt(objectFloat);
英文:

You could add a constructor that takes ObjectFloat as a parameter. This way you can create an instance of ObjectInt based on an instance of ObjectFloat:

public class ObjectInt {
    protected Integer m_member1;
    protected Integer m_member2;

    public ObjectInt() {
        // default constructor allows creating an instance without ObjectFloat
    }

    public ObjectInt(ObjectFloat objectFloat) {
        this.m_member1 = objectFloat.m_member1.intValue();
        this.m_member2 = objectFloat.m_member2.intValue();
    }
}

When you've added this constructor, you can "convert" an ObjectFloat into an ObjectInt with

ObjectInt objectInt = new ObjectInt(objectFloat)

</details>



huangapple
  • 本文由 发表于 2020年8月8日 08:36:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63310714.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定