Kotson icon indicating copy to clipboard operation
Kotson copied to clipboard

Kotlin - Java field Modifier

Open coderbuzz opened this issue 8 years ago • 1 comments

Hi,

What Kotlin's field modifier that compatible with Java Modifier.PRIVATE, Modifier.PROTECTED, Modifier.PUBLIC?

I tried with the following

class Config(activity: Activity) {
    var remoteUrl: String = "ABC"
    var localUrl: String = "DEF"
    protected val preferences: SharedPreferences = activity.getPreferences(Context.MODE_PRIVATE)

    fun save() {
        val gson = GsonBuilder().excludeFieldsWithModifiers(Modifier.PROTECTED).create()
        Log.e(">> SAVE", gson.toJson(this))
    }
}

But preferences field still included on the json output

Thanks

coderbuzz avatar Feb 01 '18 09:02 coderbuzz

This is unrelated to library Gson ignores field that are marked with transient java keyword. In Kotlin you can annotate generated property like this:

@get:Transient
val preferences: SharedPreferences = activity.getPreferences(Context.MODE_PRIVATE)

rostopira avatar May 05 '18 17:05 rostopira