extraPropertiesAreFatal
This commit is contained in:
parent
a27cba5e0f
commit
2805717ded
@ -44,6 +44,11 @@ class BuilderAdapter<T : Any> private constructor(
|
|||||||
* Ключи, которые необходимо игнорировать при чтении JSON
|
* Ключи, которые необходимо игнорировать при чтении JSON
|
||||||
*/
|
*/
|
||||||
val ignoreKeys: ImmutableSet<String>,
|
val ignoreKeys: ImmutableSet<String>,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see Builder.extraPropertiesAreFatal
|
||||||
|
*/
|
||||||
|
val extraPropertiesAreFatal: Boolean,
|
||||||
) : TypeAdapter<T>() {
|
) : TypeAdapter<T>() {
|
||||||
private val loggedMisses = ObjectOpenHashSet<String>()
|
private val loggedMisses = ObjectOpenHashSet<String>()
|
||||||
|
|
||||||
@ -87,6 +92,10 @@ class BuilderAdapter<T : Any> private constructor(
|
|||||||
throw JsonSyntaxException("Reading property ${property.name} of ${instance::class.qualifiedName} near ${reader.path}", err)
|
throw JsonSyntaxException("Reading property ${property.name} of ${instance::class.qualifiedName} near ${reader.path}", err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
if (extraPropertiesAreFatal) {
|
||||||
|
throw JsonSyntaxException("$name is not a valid property of ${instance::class.qualifiedName}")
|
||||||
|
}
|
||||||
|
|
||||||
if (!loggedMisses.contains(name)) {
|
if (!loggedMisses.contains(name)) {
|
||||||
LOGGER.warn("{} has no property for storing {}", instance::class.qualifiedName, name)
|
LOGGER.warn("{} has no property for storing {}", instance::class.qualifiedName, name)
|
||||||
loggedMisses.add(name)
|
loggedMisses.add(name)
|
||||||
@ -160,6 +169,18 @@ class BuilderAdapter<T : Any> private constructor(
|
|||||||
class Builder<T : Any>(val factory: () -> T, vararg fields: KMutableProperty1<T, *>) {
|
class Builder<T : Any>(val factory: () -> T, vararg fields: KMutableProperty1<T, *>) {
|
||||||
private val properties = ArrayList<WrappedProperty<T, *>>()
|
private val properties = ArrayList<WrappedProperty<T, *>>()
|
||||||
private val ignoreKeys = ObjectArraySet<String>()
|
private val ignoreKeys = ObjectArraySet<String>()
|
||||||
|
var extraPropertiesAreFatal = false
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Являются ли "лишние" ключи в JSON структуре ошибкой.
|
||||||
|
*
|
||||||
|
* Если "лишние" ключи являются ошибкой и известны некоторые лишние ключи, которые не нужны,
|
||||||
|
* то [extraPropertiesAreFatal] можно скомбинировать с [ignoreKey].
|
||||||
|
*/
|
||||||
|
fun extraPropertiesAreFatal(flag: Boolean = true): Builder<T> {
|
||||||
|
extraPropertiesAreFatal = flag
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
init {
|
init {
|
||||||
for (field in fields)
|
for (field in fields)
|
||||||
@ -216,7 +237,12 @@ class BuilderAdapter<T : Any> private constructor(
|
|||||||
for (property in properties)
|
for (property in properties)
|
||||||
map.put(property.property.name, property)
|
map.put(property.property.name, property)
|
||||||
|
|
||||||
return BuilderAdapter(factory, map.build(), ImmutableSet.copyOf(ignoreKeys))
|
return BuilderAdapter(
|
||||||
|
factory = factory,
|
||||||
|
properties = map.build(),
|
||||||
|
ignoreKeys = ImmutableSet.copyOf(ignoreKeys),
|
||||||
|
extraPropertiesAreFatal = extraPropertiesAreFatal,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user