英文:
Java convert a serialized object to another object
问题
我有两个Java数据对象,ObjectFloat
用于反序列化传入的请求,ObjectInt
用于传出的请求。我从HTTP调用中接收ObjectFloat
,其中包含Float
属性,对其进行反序列化,然后调用upgradeMembers()
方法。因此,在将对象发送出去之前,我想要使用upgradeMembers()
处理每个m_member1
和m_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
(不保留小数部分):
- 使用
ObjectFloat
对象调用upgradeMembers()
方法,以确保m_member1
和m_member2
已被处理。 - 使用Gson库将
ObjectFloat
对象转换为JSON字符串。 - 使用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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论