LenientConverters Enumeration Reference
public enum 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 nil for invalid or non-representable values rather than crashing or
producing corrupted data.
Edge cases handled:
- Integer overflow: NaN values return
nil; values outsideInt.min...Int.max(including infinities) are clamped toInt.min/Int.max - Special float values:
Double.nanis treated as invalid;Double.infinityand-Double.infinityare clamped in integer conversions - String parsing: Invalid formats return
nilrather than crashing - Type mismatches: Missing or incompatible types return
nil
Example usage:
let payload: DataObject = ...
let timeout = payload.getConvertible(key: "timeout", converter: LenientConverters.double)
let retryCount = payload.getConvertible(key: "retryCount", converter: LenientConverters.int)
let isEnabled = payload.getConvertible(key: "isEnabled", converter: LenientConverters.bool)
-
A lenient converter to
Doublevalues.This converter attempts the following conversions in order:
- Direct extraction as
Double - Extraction as
Stringand parsing viaDataItemFormatter(uses en_US_POSIX locale)
Declaration
Swift
public static let double: any DataItemConverter<Double> - Direct extraction as
-
A lenient converter to
Intvalues.This converter attempts the following conversions in order:
- Direct extraction as
Int(uses NSNumber’s intValue which clamps to Int.min/Int.max and truncates decimals) - Extraction as
Stringand parsing viaDataItemFormatter(truncates decimal part)
Returns
nilfor values that:- Cannot be parsed as numbers
Note
Out-of-bounds Double values are clamped to Int.min/Int.max (NSNumber behavior)Declaration
Swift
public static let int: any DataItemConverter<Int> - Direct extraction as
-
A lenient converter to
Boolvalues.This converter attempts the following conversions in order:
- Direct extraction as
Bool - Extraction as
Stringand parsing common boolean representations:- “true”, “yes”, “1” →
true - “false”, “no”, “0” →
false(case-insensitive)
- “true”, “yes”, “1” →
- Extraction as whole number where 0 is
falseand 1 istrueNote
Decimal numbers (e.g., 1.5) and integers other than 0 or 1 will returnnil
Returns
nilfor values that:- Any other strings non-conforming to the rule above
- Any numbers other than 0 or 1
Declaration
Swift
public static let bool: any DataItemConverter<Bool> - Direct extraction as
-
A lenient converter to
Stringvalues.This converter attempts the following conversions in order:
- Direct extraction as
String - Conversion of
Boolto “true” or “false” - Conversion of numeric types (
Double,Int) toStringusingDataItemFormatter- Special values:
Double.nan→"NaN",Double.infinity→"Infinity" - Whole numbers: 1.0 →
"1"(removes unnecessary “.0” suffix) - Disables scientific notation for better readability
- Special values:
Declaration
Swift
public static let string: any DataItemConverter<String> - Direct extraction as