我正在尝试形成这种类型的 JSON:
"answer_id":123,
"question_id":4567,
"value":"null"
但是,我无法将 NULL 值放在值键上,似乎 Gson 只是忽略了该字段,因此在序列化后我得到
"answer_id":123,
"question_id":4567
所以我的服务器抛出了错误的请求。我在 Gson 生成器中使用serializeNulls()
,它应该允许可为空的值,但它对我不起作用。有谁知道问题出在哪里?
@Parcelize
data class AnswerHelperObject(@SerializedName("question_id") val questionId: Int,
@SerializedName("value") val value: String? = null,
@SerializedName("answer_id") val answerId: Int?)
: Parcelable {}
由于您以字符串形式接收值 Gson 会将其解析为字符串"null",如果您希望字段值为 null,您可以编写自定义反序列化程序。
答案帮助程序对象
data class AnswerHelperObject(
var answer_id: Int,
var question_id: Int,
var value: String?
)
AnswerHelperObjectDeserializer
class AnswerHelperObjectDeserializer: JsonDeserializer<AnswerHelperObject>{
override fun deserialize(json: JsonElement, typeOfT: Type?, context: JsonDeserializationContext?): AnswerHelperObject {
val answerHelperObject = Gson().fromJson(json.asJsonObject, AnswerHelperObject::class.java)
if (answerHelperObject.value == "null") {
answerHelperObject.value = null
}
return answerHelperObject
}
}
可以看到测试方案。
class AnswerHelperObjectTest {
@Test
fun answer_object_parse_as_null_string() {
val json = "{"answer_id":123,n" +
""question_id":4567,n" +
""value":"null"}"
val answerHelperObject = Gson().fromJson(json, AnswerHelperObject::class.java)
// value is parsed as string of value null
assertEquals(answerHelperObject.value, "null")
}
@Test
fun answer_object_parse_null() {
val json = "{"answer_id":123,n" +
""question_id":4567,n" +
""value":"null"}"
val gson = GsonBuilder()
.registerTypeAdapter(AnswerHelperObject::class.java, AnswerHelperObjectDeserializer())
.create()
val answerHelperObject = gson.fromJson(json, AnswerHelperObject::class.java)
// value is deserialized as null
assertEquals(answerHelperObject.value, null)
}
}