Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moshi/Kotlin - How to serialize NULL JSON strings into empty strings instead?

I'm trying to write a null-safe String adapter that will serialize this JSON {"nullString": null} into this: Model(nullString = "") so that any JSON with a 'null' value that I expect to be a String will be replaced with "" (assuming there exists a data class like this: data class Model(val nullString: String))

I wrote a custom adapter to try and handle this:

class NullStringAdapter: JsonAdapter<String>() {
    @FromJson
    override fun fromJson(reader: JsonReader?): String {
        if (reader == null) {
            return ""
        }

        return if (reader.peek() == NULL) "" else reader.nextString()
    }

    @ToJson
    override fun toJson(writer: JsonWriter?, value: String?) {
        writer?.value(value)
    }
}

...in an attempt to solve this parsing error:

com.squareup.moshi.JsonDataException: Expected a name but was NULL at path $.nullString

Moshi parsing code:

val json = "{\"nullString\": null}"

val moshi = Moshi.Builder()
    .add(KotlinJsonAdapterFactory())
    .add(NullStringAdapter())
    .build()

val result = moshi.adapter(Model::class.java).fromJson(configStr)

What am I missing here? Still new to moshi so any help is appreciated!

like image 256
Mitch Ware Avatar asked Oct 06 '17 14:10

Mitch Ware


1 Answers

The immediate problem is the missing reader.nextNull() call to consume the null value.

There are a couple other cleanup things you can do here, too. With @FromJson, implementing JsonAdapter is unnecessary. Also, the JsonReader and JsonWriter are not nullable.

object NULL_TO_EMPTY_STRING_ADAPTER {
  @FromJson fun fromJson(reader: JsonReader): String {
    if (reader.peek() != JsonReader.Token.NULL) {
      return reader.nextString()
    }
    reader.nextNull<Unit>()
    return ""
  }
}

and use add the adapter:

val moshi = Moshi.Builder()
    .add(NULL_TO_EMPTY_STRING_ADAPTER)
    .add(KotlinJsonAdapterFactory())
    .build()
like image 132
Eric Cochran Avatar answered Oct 15 '22 21:10

Eric Cochran