Skip to content

Commit

Permalink
Set json field accessible, fixes #163
Browse files Browse the repository at this point in the history
  • Loading branch information
Bixilon committed Dec 1, 2021
1 parent 41e7e66 commit e43e22f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
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

0 comments on commit e43e22f

Please sign in to comment.