0

I'm trying to write a Moshi adapter in Kotlin that will create enums based on an integer value, and will also default to a specific enum value if it encounters an unsupported value.

My adapter is never getting called, which causes the default enum adapter to fail with the included stacktrace.

Tracing the code show that the @ToJson annotation somehow isn't present, and so the adapter isn't added when it should be.

Declaring the adapter

enum class Status {
    IDLE,
    IN_PROGRESS,
    FAILED,
    SUCCESS,
    UNKNOWN;

    companion object {
        private val map = Status.values().associateBy(Status::ordinal);
        fun fromInt(type: Int?): Status {
            var ret = map[type]
            if (ret == null) {
                ret = UNKNOWN
            }
            return ret
        }

    }

    class Adapter {
        @ToJson
        fun toJson(status: Status): Int {
            return status.ordinal
        }

        @FromJson
        fun fromJson(value: Int): Status {
            return fromInt(value)
        }
    }
}

Building Moshi

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

Dependencies

implementation 'com.squareup.moshi:moshi:1.6.0'
implementation 'com.squareup.moshi:moshi-kotlin:1.6.0'

Stacktrace

05-29 18:57:56.603 24560-24560/com.example.app E/MyActivity: com.squareup.moshi.JsonDataException: Expected one of [IDLE, IN_PROGRESS, FAILED, SUCCESS, UNKNOWN] but was 2 at path $[0].status
    at com.squareup.moshi.StandardJsonAdapters$EnumJsonAdapter.fromJson(StandardJsonAdapters.java:297)
    at com.squareup.moshi.StandardJsonAdapters$EnumJsonAdapter.fromJson(StandardJsonAdapters.java:266)
    at com.squareup.moshi.JsonAdapter$2.fromJson(JsonAdapter.java:137)
    at com.squareup.moshi.ClassJsonAdapter$FieldBinding.read(ClassJsonAdapter.java:196)
    at com.squareup.moshi.ClassJsonAdapter.fromJson(ClassJsonAdapter.java:158)
    at com.squareup.moshi.JsonAdapter$2.fromJson(JsonAdapter.java:137)
    at com.squareup.moshi.CollectionJsonAdapter.fromJson(CollectionJsonAdapter.java:76)
    at com.squareup.moshi.CollectionJsonAdapter$2.fromJson(CollectionJsonAdapter.java:53)
    at com.squareup.moshi.JsonAdapter$2.fromJson(JsonAdapter.java:137)
    at retrofit2.converter.moshi.MoshiResponseBodyConverter.convert(MoshiResponseBodyConverter.java:45)
    at retrofit2.converter.moshi.MoshiResponseBodyConverter.convert(MoshiResponseBodyConverter.java:27)
    ...
Sky Kelsey
  • 19,192
  • 5
  • 36
  • 77
  • I couldn’t reproduce this. https://gist.github.com/swankjesse/e556b6e6687d31536f8a58eb0039eda2 – Jesse Wilson May 30 '18 at 04:13
  • Did you tell your Retrofit instance about your Moshi instance? https://github.com/square/retrofit/tree/master/retrofit-converters/moshi – Jesse Wilson May 30 '18 at 04:14
  • 1
    Thanks so much Jesse. Your hunch was correct. I failed to pass my `Moshi` instance into my `MoshiConverterFactory`. Thanks for the help! – Sky Kelsey May 30 '18 at 17:22

0 Answers0