LenientConverters
A collection of lenient converters that can handle type mismatches in JSON data.
These converters attempt multiple conversion strategies when the actual type in JSON differs from the expected type. They follow a fail-safe pattern: returning null for invalid or non-representable values rather than crashing or producing corrupted data.
Edge cases handled:
Int/Long overflow: NaN values return
null; values outside Int.MIN_VALUE..Int.MAX_VALUE or Long.MIN_VALUE..Long.MAX_VALUE (including infinities) are clamped to their respective max and min valuesSpecial float values: Double.NaN is treated as invalid for Int/Long converters; Double.POSITIVE_INFINITY and Double.NEGATIVE_INFINITY are clamped in integer max and min values
String parsing: Invalid formats return
nullrather than crashingType mismatches: Missing or incompatible types return
null
Example usage:
val payload: DataObject = ...
val timeout = payload.get("timeout", LenientConverters.DOUBLE)
val retryCount = payload.get("retryCount", LenientConverters.INT)
val isEnabled = payload.get("isEnabled", LenientConverters.BOOLEAN)Properties
A lenient converter to Boolean values.
A lenient converter to Double values.
A lenient converter to Int values.
A lenient converter to Long values.
A lenient converter to String values.