gson.toJson返回一个数据类的空对象

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

gson.toJson returns an empty object for a data class

问题

以下是翻译好的内容:

我有一个 Kotlin 数据类 OfflineDataRequestInfo,我想使用 Gson 将其转换为 Json,但它总是返回一个空对象。

data class OfflineDataRequestInfo (
    @SerializedName("status") val status: String,
    @SerializedName("userId") val userId: String?,
    @SerializedName("fulOrderId") val fulOrderId: String,
    @SerializedName("timeStamp") val timeStamp: String,
    @SerializedName("fulOrder") val fulOrder: String,
    @SerializedName("checks") val checks: String?
)

其中一些值可能为空,所以我也尝试了下面的代码,但返回的是 {}

gson.toJson(OfflineDataRequestInfo("a","b","c","d", "e", "f"))

以下是稍微详细些的信息,以防有问题:

@Entity
data class OfflineData (
    @PrimaryKey(autoGenerate = true) val id: Int = 0,
    @ColumnInfo(name="request_code") val requestCode: String?,
    @Embedded
    val requestInfoJson: OfflineDataRequestInfo
)

这是我的实际函数:

fun postFulOurderData(offlineData: OfflineData) {
    if (offlineData != null) {
        val mainRepository = MainRepository(ApiHelper(RetrofitBuilder.apiService))
        val builder = GsonBuilder()
        builder.serializeNulls()
        val gson = builder.create()
        launch {

            val postFulOrder = mainRepository.postFulOrderOfflineData(gson.toJson(offlineData.requestInfoJson), tokenResult.access_token)
        }
    }
}

我还尝试过像上面展示的使用 GsonBuilder,以及默认的 Gson,但都没有成功。

还尝试过 gson.toJson(offlineData.requestInfoJson, OfflineDataRequestInfo::class.java)

请问有人可以建议一下我哪里出错了吗?非常感谢您的帮助。

谢谢,
R

英文:

I have a kotlin data class OfflineDataRequestInfo which I want to convert to Json using Gson, but it always returns an empty object

data class OfflineDataRequestInfo (
    @SerializedName("status") val status: String,
    @SerializedName("userId") val userId: String?,
    @SerializedName("fulOrderId") val fulOrderId: String,
    @SerializedName("timeStamp") val timeStamp: String,
    @SerializedName("fulOrder") val fulOrder: String,
    @SerializedName("checks") val checks: String?
)

some of the values could be null so I tried the bellow code too which returned {}

gson.toJson(OfflineDataRequestInfo("a","b","c","d", "e", "f"))

Here is a bit more info just in case that is an issue

@Entity
data class OfflineData (
    @PrimaryKey(autoGenerate = true) val id: Int = 0,
    @ColumnInfo(name="request_code") val requestCode: String?,
    @Embedded
    val requestInfoJson: OfflineDataRequestInfo
)

this is my actual function

fun postFulOurderData(offlineData: OfflineData) {
    if (offlineData != null) {
        val mainRepository = MainRepository(ApiHelper(RetrofitBuilder.apiService))
        val builder = GsonBuilder()
        builder.serializeNulls()
        val gson = builder.create()
        launch {

            val postFulOrder = mainRepository.postFulOrderOfflineData(gson.toJson(offlineData.requestInfoJson), tokenResult.access_token)
        }
    }
}

I also tried using GsonBuilder as shown above and also the default Gson, but no luck

also tried gson.toJson(offlineData.requestInfoJson, OfflineDataRequestInfo::class.java)

can any one suggest please where I am doing it wrong

your help will be very much appreciated

thanks
R

答案1

得分: 1

可能你的Gson配置中有 new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create() 设置。如果是这样,你只需要在每个想要在JSON中序列化的字段上添加 @Expose 注解:

data class OfflineDataRequestInfo (
    @Expose @SerializedName("status") val status: String,
    @Expose @SerializedName("userId") val userId: String?,
    @Expose @SerializedName("fulOrderId") val fulOrderId: String,
    @Expose @SerializedName("timeStamp") val timeStamp: String,
    @Expose @SerializedName("fulOrder") val fulOrder: String,
    @Expose @SerializedName("checks") val checks: String?
)
英文:

Probably you have new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create() setting for gson. If so you just need to add @Expose annotation to each field you want serialize in json:

data class OfflineDataRequestInfo (
    @Expose @SerializedName("status") val status: String,
    @Expose @SerializedName("userId") val userId: String?,
    @Expose @SerializedName("fulOrderId") val fulOrderId: String,
    @Expose @SerializedName("timeStamp") val timeStamp: String,
    @Expose @SerializedName("fulOrder") val fulOrder: String,
    @Expose @SerializedName("checks") val checks: String?
)

答案2

得分: 0

我对Kotlin的了解不是很多,但我知道你可以很好地混合使用Java和Kotlin,而且由于Gson是以Java使用为主要考虑而创建的,所以我认为你应该使用Java。你可以创建一个Java类,然后编写如下方法:

String toJson(OfflineDataRequestInfo o) {
    return new Gson().toJson(o, OfflineDataRequestInfo.class);
}

然后在Kotlin中调用该方法。

我并不是在说你应该将整个项目都使用Java,只是在这个方法中使用Java。

希望这能有所帮助,如果这不是你想要的,请告诉我 gson.toJson返回一个数据类的空对象

英文:

I don't know that much about Kotlin, but I do know that you can mix Java and Kotlin pretty well, and since Gson was made with Java use in mind I think you should use Java. You can create a Java class, and then a method like this:

String toJson(OfflineDataRequestInfo o) {
    return new Gson().toJson(o, OfflineDataRequestInfo.class);
}

and then call that method from Kotlin.

I'm not saying that you should do your entire project in Java, just use Java for this one method.

Hope this helps, and please tell me if this wasn't what you had in mind gson.toJson返回一个数据类的空对象

答案3

得分: 0

我刚刚使用gson:2.2.4测试了以下代码,它是可行的:

data class OfflineDataRequestInfo (
        @SerializedName("status") val status: String,
        @SerializedName("userId") val userId: String?,
        @SerializedName("fulOrderId") val fulOrderId: String,
        @SerializedName("timeStamp") val timeStamp: String,
        @SerializedName("fulOrder") val fulOrder: String,
        @SerializedName("checks") val checks: String?
)

fun main() {
    println(Gson().toJson(OfflineDataRequestInfo("a","b","c","d", "e", "f")))
}

输出:
{"status":"a","userId":"b","fulOrderId":"c","timeStamp":"d","fulOrder":"e","checks":"f"}

我没有在 Android 上尝试过,但它应该也可以工作。
我建议您尝试更新您的 gson 版本,并编写一个使用它的单元测试。

英文:

I just tested the following code using gson:2.2.4 and it works:

data class OfflineDataRequestInfo (
        @SerializedName("status") val status: String,
        @SerializedName("userId") val userId: String?,
        @SerializedName("fulOrderId") val fulOrderId: String,
        @SerializedName("timeStamp") val timeStamp: String,
        @SerializedName("fulOrder") val fulOrder: String,
        @SerializedName("checks") val checks: String?
)

fun main() {
    println(Gson().toJson(OfflineDataRequestInfo("a","b","c","d", "e", "f")))
}

Output:
{"status":"a","userId":"b","fulOrderId":"c","timeStamp":"d","fulOrder":"e","checks":"f"}

I didn't try it on android, but it should also work.
I suggest you try to update your gson version and also to write a unit test using it.

huangapple
  • 本文由 发表于 2020年10月15日 08:57:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/64363406.html
匿名

发表评论

匿名网友

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

确定