Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set json field accessible, fixes #163 #346

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion klaxon/src/main/kotlin/com/beust/klaxon/DefaultConverter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import java.lang.reflect.ParameterizedType
import java.lang.reflect.TypeVariable
import java.math.BigDecimal
import java.math.BigInteger
import kotlin.reflect.jvm.isAccessible
import kotlin.reflect.jvm.jvmErasure

/**
Expand Down Expand Up @@ -77,6 +78,7 @@ class DefaultConverter(private val klaxon: Klaxon, private val allPaths: HashMap
val valueList = arrayListOf<String>()
val properties = Annotations.findNonIgnoredProperties(value::class, klaxon.propertyStrategies)
properties.forEach { prop ->
prop.isAccessible = true
val getValue = prop.getter.call(value)
if (Annotations.findJsonAnnotation(value::class, prop.name)?.serializeNull != false || getValue != null) {
val jsonValue = klaxon.toJsonString(getValue, prop)
Expand Down Expand Up @@ -235,4 +237,4 @@ class DefaultConverter(private val klaxon: Klaxon, private val allPaths: HashMap
return result
}

}
}
2 changes: 2 additions & 0 deletions klaxon/src/main/kotlin/com/beust/klaxon/JsonValue.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import java.math.BigDecimal
import java.math.BigInteger
import kotlin.reflect.KProperty
import kotlin.reflect.full.declaredMemberProperties
import kotlin.reflect.jvm.isAccessible

/**
* Variant class that encapsulates one JSON value. Only exactly one of the property fields defined in the
Expand Down Expand Up @@ -191,6 +192,7 @@ class JsonValue constructor(value: Any?,
// .filter { it.visibility != KVisibility.PRIVATE && it.isAccessible }
// obj.javaClass.declaredFields
.forEach { property ->
property.isAccessible = true
val p = property.call(obj)
result[property] = p
}
Expand Down