ansman/kotshi

invalid code in xxxJsonAdapter generated with @JsonSerializable

gikahei opened this issue · 1 comments

When the data class's name is very long, the generated JsonAdapter may contains invalid line break, which causes compile error.

Test Code:

package com.example.test

import se.ansman.kotshi.JsonSerializable

@JsonSerializable
data class AAAAAAAAAAAAAA(
    val b: BBBBBBBBBBBBBBB
) {
    @JsonSerializable
    data class BBBBBBBBBBBBBBB(
        val c: CCCCCCCCCCCCCCCCC
    ) {
        @JsonSerializable
        data class CCCCCCCCCCCCCCCCC(
            val d: DDDDDDDDDDDDDDDDDD
        ) {
            @JsonSerializable
            data class DDDDDDDDDDDDDDDDDD(
                val e: EEEEEEEEEEEEEEEEEEEEEE
            ) {
                @JsonSerializable
                data class EEEEEEEEEEEEEEEEEEEEEE(
                    val test: String?
                )
            }
        }
    }
}

And the invalid code :

// Code generated by Kotshi. Do not edit.
package com.example.test

import com.squareup.moshi.JsonReader
import com.squareup.moshi.JsonWriter
import java.io.IOException
import kotlin.String
import kotlin.jvm.JvmStatic
import kotlin.jvm.Throws
import se.ansman.kotshi.NamedJsonAdapter

internal class
        KotshiAAAAAAAAAAAAAA_BBBBBBBBBBBBBBB_CCCCCCCCCCCCCCCCC_DDDDDDDDDDDDDDDDDD_EEEEEEEEEEEEEEEEEEEEEEJsonAdapter()
        :
        NamedJsonAdapter<AAAAAAAAAAAAAA.BBBBBBBBBBBBBBB.CCCCCCCCCCCCCCCCC.DDDDDDDDDDDDDDDDDD.EEEEEEEEEEEEEEEEEEEEEE>("KotshiJsonAdapter(AAAAAAAAAAAAAA.BBBBBBBBBBBBBBB.CCCCCCCCCCCCCCCCC.DDDDDDDDDDDDDDDDDD.EEEEEEEEEEEEEEEEEEEEEE)")
        {
    @Throws(IOException::class)
    override fun toJson(writer: JsonWriter,
            value: AAAAAAAAAAAAAA.BBBBBBBBBBBBBBB.CCCCCCCCCCCCCCCCC.DDDDDDDDDDDDDDDDDD.EEEEEEEEEEEEEEEEEEEEEE?) {
        if (value == null) {
            writer.nullValue()
            return
        }
        writer.beginObject()

        writer.name("test")
        writer.value(value.test)

        writer.endObject()
    }

    @Throws(IOException::class)
    override fun fromJson(reader: JsonReader):
            AAAAAAAAAAAAAA.BBBBBBBBBBBBBBB.CCCCCCCCCCCCCCCCC.DDDDDDDDDDDDDDDDDD.EEEEEEEEEEEEEEEEEEEEEE? {
        if (reader.peek() == JsonReader.Token.NULL) return reader.nextNull()

        var test: String? = null

        reader.beginObject()
        while (reader.hasNext()) {
            when (reader.selectName(options)) {
                0 -> {
                    if (reader.peek() == JsonReader.Token.NULL) {
                        reader.skipValue()
                    } else {
                        test = reader.nextString()
                    }
                }
                -1 -> {
                    reader.skipName()
                    reader.skipValue()
                }
            }
        }
        reader.endObject()

        return
                AAAAAAAAAAAAAA.BBBBBBBBBBBBBBB.CCCCCCCCCCCCCCCCC.DDDDDDDDDDDDDDDDDD.EEEEEEEEEEEEEEEEEEEEEE(
                test = test)
    }

    private companion object {
        @JvmStatic
        private val options: JsonReader.Options = JsonReader.Options.of(
                "test")
    }
}

image

I've tested the boundary value of the class name's length. When the length of return xxxx reaches 101, the invalid line break will be added after return. And my line wrap setting in Android Studio is 100, I don't know if there is any relationship between these.

Nice catch, and great with a test case too. Fixed and released as 2.0.3