12

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!

1 Answer 1

22

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.