Skip to content
This repository has been archived by the owner on Dec 3, 2020. It is now read-only.

Kotlin - Java field Modifier #35

Open
coderbuzz opened this issue Feb 1, 2018 · 1 comment
Open

Kotlin - Java field Modifier #35

coderbuzz opened this issue Feb 1, 2018 · 1 comment

Comments

@coderbuzz
Copy link

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
Copy link

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)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants