extraPropertiesAreFatal
This commit is contained in:
parent
a27cba5e0f
commit
2805717ded
@ -44,6 +44,11 @@ class BuilderAdapter<T : Any> private constructor(
|
||||
* Ключи, которые необходимо игнорировать при чтении JSON
|
||||
*/
|
||||
val ignoreKeys: ImmutableSet<String>,
|
||||
|
||||
/**
|
||||
* @see Builder.extraPropertiesAreFatal
|
||||
*/
|
||||
val extraPropertiesAreFatal: Boolean,
|
||||
) : TypeAdapter<T>() {
|
||||
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)
|
||||
}
|
||||
} else {
|
||||
if (extraPropertiesAreFatal) {
|
||||
throw JsonSyntaxException("$name is not a valid property of ${instance::class.qualifiedName}")
|
||||
}
|
||||
|
||||
if (!loggedMisses.contains(name)) {
|
||||
LOGGER.warn("{} has no property for storing {}", instance::class.qualifiedName, 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, *>) {
|
||||
private val properties = ArrayList<WrappedProperty<T, *>>()
|
||||
private val ignoreKeys = ObjectArraySet<String>()
|
||||
var extraPropertiesAreFatal = false
|
||||
|
||||
/**
|
||||
* Являются ли "лишние" ключи в JSON структуре ошибкой.
|
||||
*
|
||||
* Если "лишние" ключи являются ошибкой и известны некоторые лишние ключи, которые не нужны,
|
||||
* то [extraPropertiesAreFatal] можно скомбинировать с [ignoreKey].
|
||||
*/
|
||||
fun extraPropertiesAreFatal(flag: Boolean = true): Builder<T> {
|
||||
extraPropertiesAreFatal = flag
|
||||
return this
|
||||
}
|
||||
|
||||
init {
|
||||
for (field in fields)
|
||||
@ -216,7 +237,12 @@ class BuilderAdapter<T : Any> private constructor(
|
||||
for (property in properties)
|
||||
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