moshi自定义限定符注释仅在一个属性上序列化null

问题描述 投票:2回答:2

我想在我的JSON主体中仅在一个PUT上的一个属性中序列化null。我不想为对象中的任何其他类型序列化null。模型类是这样的

@Parcel
class User @ParcelConstructor constructor(var college: College?,
                                          var firstname: String?,
                                          var lastname: String?,
                                          var email: String?,
                                          var active: Boolean = true,
                                          var updatedAt: String?,
                                          var gender: String?,
                                          var picture: String?,
                                          var id: String?,
                                          @field: [CollegeField] var collegeInput: String?,
                                          @field: [CollegeField] var otherCollege: String?,)

如果其中任何一个为空,我只想序列化collegeInput和其他字段。例如

val user = User(firstname = "foo", lastname=null, collegeInput="abcd", otherCollege = null)

Json看起来像这样:

{"user":{
  "firstname": "foo",
  "collegeInput": "abcd",
  "otherCollege": null
}}

如果otherCollege为null,则从对象中省略las​​tname,因为默认情况下moshi不会序列化我想要的空值,但是qualifer字段应该使用空值序列化

我试过用

class UserAdapter {
@FromJson
@CollegeField
@Throws(Exception::class)
fun fromJson(reader: JsonReader): String? {
    return when (reader.peek()) {
        JsonReader.Token.NULL ->
            reader.nextNull()
        JsonReader.Token.STRING -> reader.nextString()
        else -> {
            reader.skipValue() // or throw
            null
        }
    }
}

@ToJson
@Throws(IOException::class)
fun toJson(@CollegeField b: String?): String? {
    return b
}


@Retention(AnnotationRetention.RUNTIME)
@JsonQualifier
annotation class CollegeField

我将适配器添加到moshi但它永远不会被调用

@Provides
@Singleton
fun provideMoshi(): Moshi {
    return Moshi.Builder()
            .add(UserAdapter())
            .build()
}

@Provides
@Singleton
fun provideRetrofit(client: OkHttpClient, moshi: Moshi, apiConfig: ApiConfig): Retrofit {
    return Retrofit.Builder()
            .baseUrl(apiConfig.baseUrl)
            .client(client)
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(ScalarsConverterFactory.create())
            .addConverterFactory(MoshiConverterFactory.create(moshi))
            .build()
}
kotlin gson retrofit moshi
2个回答
1
投票

当限定字符串值为null时,您的toJson适配器方法将返回null,并且JsonWriter将不会写入null值。

这是一个可以安装的限定符和适配器工厂。

@Retention(RUNTIME)
@JsonQualifier
public @interface SerializeNulls {
  JsonAdapter.Factory JSON_ADAPTER_FACTORY = new JsonAdapter.Factory() {
    @Nullable @Override
    public JsonAdapter<?> create(Type type, Set<? extends Annotation> annotations, Moshi moshi) {
      Set<? extends Annotation> nextAnnotations =
          Types.nextAnnotations(annotations, SerializeNulls.class);
      if (nextAnnotations == null) {
        return null;
      }
      return moshi.nextAdapter(this, type, nextAnnotations).serializeNulls();
    }
  };
}

现在,以下内容将通过。

class User(
  var firstname: String?,
  var lastname: String?,
  @SerializeNulls var collegeInput: String?,
  @SerializeNulls var otherCollege: String?
)

@Test fun serializeNullsQualifier() {
  val moshi = Moshi.Builder()
      .add(SerializeNulls.JSON_ADAPTER_FACTORY)
      .add(KotlinJsonAdapterFactory())
      .build()
  val userAdapter = moshi.adapter(User::class.java)
  val user = User(
      firstname = "foo",
      lastname = null,
      collegeInput = "abcd",
      otherCollege = null
  )
  assertThat(
      userAdapter.toJson(user)
  ).isEqualTo(
      """{"firstname":"foo","collegeInput":"abcd","otherCollege":null}"""
  )
}

请注意,您应该使用Kotlin support in Moshi来避免@field:奇怪。


0
投票

尝试从我的要点:

https://gist.github.com/OleksandrKucherenko/ffb2126d37778b88fca3774f1666ce66

在我的例子中,我将NULL从JSON转换为默认的双/整数值。您可以轻松修改该方法并使其适用于您的特定情况。

附:它的JAVA,首先将其转换为Kotlin。

© www.soinside.com 2019 - 2024. All rights reserved.