SalomonBrys / Kotson

Kotlin bindings for JSON manipulation via Gson
MIT License
709 stars 37 forks source link

Kotlin - Java field Modifier #35

Open coderbuzz opened 6 years ago

coderbuzz commented 6 years ago

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

rostopira commented 6 years ago

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)