Kotson
Kotson copied to clipboard
Kotlin - Java field Modifier
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
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)