1- // TODO: Put public facing types in this file.
2-
3- /// Parses a [dynamic] value into an [int] ?.
1+ /// Parses a [dynamic] value into an [int] ?.
42/// Handles `null` , `int` , `double` (truncates), and `String` representations.
53/// An empty string or a string that fails to parse will result in `null` .
64int ? flexibleIntFromJson (dynamic value) {
@@ -49,7 +47,7 @@ bool? flexibleBoolFromJson(dynamic value) {
4947}
5048
5149/// Parses a [dynamic] value into a [String] ?.
52- /// Handles `null` and `String` . Converts other types (like `int` , `double` , `bool` )
50+ /// Handles `null` and `String` . Converts other types (like `int` , `double` , `bool` )
5351/// to their string representation using `.toString()` .
5452String ? flexibleStringFromJson (dynamic value) {
5553 if (value == null ) return null ;
@@ -77,7 +75,7 @@ num? flexibleNumFromJson(dynamic value) {
7775/// If the value is an `int` , assumes it's milliseconds since epoch (UTC).
7876/// If the value is a `String` :
7977/// - Tries direct parsing via `DateTime.tryParse` (for ISO 8601 and similar).
80- /// - If direct parsing fails and the string is purely numeric,
78+ /// - If direct parsing fails and the string is purely numeric,
8179/// treats it as milliseconds since epoch.
8280/// An empty string or unparseable string format will result in `null` .
8381DateTime ? flexibleDateTimeFromJson (dynamic value) {
@@ -91,7 +89,7 @@ DateTime? flexibleDateTimeFromJson(dynamic value) {
9189 // Try standard ISO 8601 parsing first
9290 DateTime ? dt = DateTime .tryParse (value);
9391 if (dt != null ) return dt;
94-
92+
9593 // If it's a purely numeric string, try parsing as milliseconds since epoch
9694 final intValue = int .tryParse (value);
9795 if (intValue != null ) {
@@ -111,7 +109,7 @@ Uri? flexibleUriFromJson(dynamic value) {
111109 return Uri .tryParse (value);
112110 }
113111 // If the value is already a Uri, though less common from raw JSON
114- if (value is Uri ) return value;
112+ if (value is Uri ) return value;
115113 return null ;
116114}
117115
@@ -121,22 +119,25 @@ Uri? flexibleUriFromJson(dynamic value) {
121119
122120/// Ensures result is always a List, even if API returns single item or null.
123121/// Useful for APIs that inconsistently return arrays vs single items for a field.
124- /// Returns `null` if the input `value` is null or if the `itemParser`
122+ /// Returns `null` if the input `value` is null or if the `itemParser`
125123/// results in `null` for a single item input.
126124/// Filters out `null` items from the list if the input is a list.
127- ///
125+ ///
128126/// Example:
129127/// ```dart
130128/// @JsonKey(fromJson: (v) => flexibleListFromJson(v, flexibleIntFromJson))
131129/// List<int?>? numbers;
132130/// ```
133131List <T ?>? flexibleListFromJson <T >(
134- dynamic value,
132+ dynamic value,
135133 T ? Function (dynamic ) itemParser,
136134) {
137135 if (value == null ) return null ;
138136 if (value is List ) {
139- return value.map ((item) => itemParser (item)).where ((parsedItem) => parsedItem != null ).toList ();
137+ return value
138+ .map ((item) => itemParser (item))
139+ .where ((parsedItem) => parsedItem != null )
140+ .toList ();
140141 }
141142 // Single item? Wrap it in a list if the parsed item is not null.
142143 final T ? parsedSingleItem = itemParser (value);
@@ -146,14 +147,14 @@ List<T?>? flexibleListFromJson<T>(
146147/// For when APIs return empty arrays as null, or you want to guarantee a non-null list.
147148/// Returns empty list instead of null if the input is null or if parsing results in nulls.
148149/// Filters out `null` items from the list if the input is a list.
149- ///
150+ ///
150151/// Example:
151152/// ```dart
152153/// @JsonKey(fromJson: (v) => flexibleListNotNullFromJson(v, flexibleStringFromJson))
153154/// List<String> tags; // Guarantees a List<String>, never null.
154155/// ```
155156List <T > flexibleListNotNullFromJson <T >(
156- dynamic value,
157+ dynamic value,
157158 T ? Function (dynamic ) itemParser,
158159) {
159160 if (value == null ) return < T > [];
@@ -176,7 +177,7 @@ List<T> flexibleListNotNullFromJson<T>(
176177/// Handles `null` input.
177178/// If input is already a list, its elements are converted to strings.
178179/// Trims whitespace from each part and filters out empty strings after splitting.
179- ///
180+ ///
180181/// Example:
181182/// ```dart
182183/// @JsonKey(fromJson: flexibleCommaSeparatedListFromJson)
@@ -185,7 +186,10 @@ List<T> flexibleListNotNullFromJson<T>(
185186List <String >? flexibleCommaSeparatedListFromJson (dynamic value) {
186187 if (value == null ) return null ;
187188 if (value is List ) {
188- return value.map ((e) => e.toString ().trim ()).where ((e) => e.isNotEmpty).toList ();
189+ return value
190+ .map ((e) => e.toString ().trim ())
191+ .where ((e) => e.isNotEmpty)
192+ .toList ();
189193 }
190194 if (value is String ) {
191195 if (value.trim ().isEmpty) return null ;
@@ -202,10 +206,10 @@ List<String>? flexibleCommaSeparatedListFromJson(dynamic value) {
202206// STRING NORMALIZATION
203207// ============================================================================
204208
205- /// Trims whitespace from the input string (if it's a string) and
209+ /// Trims whitespace from the input string (if it's a string) and
206210/// treats empty or whitespace-only strings as `null` .
207211/// If the input is not a string but not null, it's converted via `.toString()` first.
208- ///
212+ ///
209213/// Example:
210214/// ```dart
211215/// @JsonKey(fromJson: flexibleTrimmedStringFromJson)
@@ -220,7 +224,7 @@ String? flexibleTrimmedStringFromJson(dynamic value) {
220224
221225/// Normalizes a string to lowercase after trimming and handling null/empty strings.
222226/// Uses `flexibleTrimmedStringFromJson` internally.
223- ///
227+ ///
224228/// Example:
225229/// ```dart
226230/// @JsonKey(fromJson: flexibleLowerStringFromJson)
@@ -233,7 +237,7 @@ String? flexibleLowerStringFromJson(dynamic value) {
233237
234238/// Normalizes a string to uppercase after trimming and handling null/empty strings.
235239/// Uses `flexibleTrimmedStringFromJson` internally.
236- ///
240+ ///
237241/// Example:
238242/// ```dart
239243/// @JsonKey(fromJson: flexibleUpperStringFromJson)
0 commit comments