2

My app send my location every time. when I disable geolocation. i want send {"latitude":null,"longitude":null} but sent {}

Model

@Serializable
data class PointBody(
    @Json(name = "latitude") val latitude: Double?,
    @Json(name = "longitude") val longitude: Double?
)

Request

 @POST(Path.LOCATION)
    suspend fun sendPoint(
        @Body point: PointBody
    )

Retorift

private fun provideRetrofit(moshi: Moshi, client: OkHttpClient) = Retrofit.Builder()
    .client(client)
    .addConverterFactory(MoshiConverterFactory.create(moshi))
    .baseUrl("Base")
    .build()

Moshi

private fun provideMoshi(): Moshi {
    return Moshi
        .Builder()
        .add(KotlinJsonAdapterFactory())
        .build()
}
Svetl9chok
  • 153
  • 5

2 Answers2

2

You can solve just adding withNullSerialization() function to MoshiConverterFactory.

    @Singleton
    @Provides
    fun provideMoshi(): Moshi =
        Moshi.Builder()
            .add(KotlinJsonAdapterFactory())
            .build()
    
    @Singleton
    @Provides
    fun provideMoshiConverterFactory(moshi: Moshi): MoshiConverterFactory =
        MoshiConverterFactory.create(moshi).withNullSerialization()

Code From MoshiConverterFactory.java

...
  /** Return a new factory which includes null values into the serialized JSON. */
  public MoshiConverterFactory withNullSerialization() {
    return new MoshiConverterFactory(moshi, lenient, failOnUnknown, true);
  }
...

Please check MoshiConverterFactory.java for further information.

iamonat
  • 66
  • 6
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 09 '21 at 07:42
0

My solution

Factory

@kotlin.annotation.Retention(AnnotationRetention.RUNTIME)
@JsonQualifier
annotation class SerializeNulls

class SerializeNullsFactory : JsonAdapter.Factory {
    override fun create(type: Type, annotations: Set<Annotation?>, moshi: Moshi): JsonAdapter<*>? {
        val nextAnnotations = Types.nextAnnotations(
            annotations,
            SerializeNulls::class.java
        ) ?: return null
        return moshi.nextAdapter<Any>(this, type, nextAnnotations).serializeNulls()
    }
}

Model

@Serializable
data class PointBody(
    @SerializeNulls val latitude: Double?,
    @SerializeNulls val longitude: Double?
)

Moshi

private fun provideMoshi(): Moshi {
    return Moshi
        .Builder()
        .add(SerializeNullsFactory())
        .add(KotlinJsonAdapterFactory())
        .build()
}
Svetl9chok
  • 153
  • 5
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 29 '21 at 07:46