Skip to content

Commit d86738d

Browse files
author
travis
committed
feat: add flexible map transformation functions and comprehensive tests
1 parent 245b7c3 commit d86738d

File tree

5 files changed

+484
-22
lines changed

5 files changed

+484
-22
lines changed

CHANGELOG.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,48 @@
1+
## 0.1.22
2+
3+
- Added `flexibleMapFromJson` and `flexibleMapNotNullFromJson` functions for flexible map transformations
4+
5+
## 0.1.21
6+
7+
- Added comprehensive tests for `flexibleListFromJson` and `flexibleListNotNullFromJson`
8+
9+
## 0.1.20
10+
11+
- Added comprehensive tests for `flexibleLowerStringFromJson` and `flexibleUpperStringFromJson`
12+
13+
## 0.1.19
14+
15+
- Added comprehensive tests for `flexibleTrimmedStringFromJson`
16+
17+
## 0.1.18
18+
19+
- Added comprehensive tests for `flexibleUriFromJson`
20+
- Fixed test expectations for `flexibleUriFromJson` to match actual behavior
21+
22+
## 0.1.17
23+
24+
- Added comprehensive tests for `flexibleDateTimeFromJson`
25+
26+
## 0.1.16
27+
28+
- Added comprehensive tests for `flexibleNumFromJson`
29+
30+
## 0.1.15
31+
32+
- Added comprehensive tests for `flexibleStringFromJson`
33+
34+
## 0.1.14
35+
36+
- Added comprehensive tests for `flexibleBoolFromJson`
37+
38+
## 0.1.13
39+
40+
- Added comprehensive tests for `flexibleDoubleFromJson`
41+
42+
## 0.1.12
43+
44+
- Added comprehensive tests for `flexibleIntFromJson`
45+
146
## 0.1.11
247

348
- Ran dart format on the codebase

lib/json_son.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ library;
99

1010
export 'src/json_son_base.dart';
1111

12+
export 'src/json_son_base.dart'
13+
show flexibleMapFromJson, flexibleMapNotNullFromJson;
14+
1215
/// Parses a [dynamic] value into an [int]?.
1316
/// Handles `null`, `int`, `double` (truncates), and `String` representations.
1417
/// An empty string or a string that fails to parse will result in `null`.

lib/src/json_son_base.dart

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,60 @@ String? flexibleLowerStringFromJson(dynamic value) {
244244
/// String? productCode; // Input " abc-123 " -> "ABC-123"
245245
/// ```
246246
String? flexibleUpperStringFromJson(dynamic value) {
247-
final str = flexibleTrimmedStringFromJson(value);
248-
return str?.toUpperCase();
247+
return flexibleTrimmedStringFromJson(value)?.toUpperCase();
248+
}
249+
250+
/// Transforms a map by applying a transformation function to each key-value pair.
251+
/// Handles `null` input by returning `null`.
252+
/// The [mapper] function can return `null` to skip entries.
253+
///
254+
/// Example:
255+
/// ```dart
256+
/// @JsonKey(fromJson: (v) => flexibleMapFromJson(v, (k, v) =>
257+
/// k != null ? MapEntry(k, int.tryParse(v)) : null))
258+
/// Map<String, int>? counts; // Input {"a": "1", "b": "2"} -> {"a": 1, "b": 2}
259+
/// ```
260+
Map<K, V>? flexibleMapFromJson<K, V>(
261+
dynamic value,
262+
MapEntry<K, V>? Function(dynamic key, dynamic value) mapper,
263+
) {
264+
if (value == null) return null;
265+
266+
final result = <K, V>{};
267+
if (value is Map) {
268+
value.forEach((key, value) {
269+
final entry = mapper(key, value);
270+
if (entry != null) {
271+
result[entry.key] = entry.value;
272+
}
273+
});
274+
}
275+
return result;
276+
}
277+
278+
/// Similar to [flexibleMapFromJson] but returns an empty map instead of `null` for `null` input.
279+
/// The [mapper] function can return `null` to skip entries.
280+
///
281+
/// Example:
282+
/// ```dart
283+
/// @JsonKey(fromJson: (v) => flexibleMapNotNullFromJson(v, (k, v) =>
284+
/// k != null ? MapEntry(k, int.tryParse(v)) : null))
285+
/// Map<String, int> counts; // Input {"a": "1", "b": "x"} -> {"a": 1}
286+
/// ```
287+
Map<K, V> flexibleMapNotNullFromJson<K, V>(
288+
dynamic value,
289+
MapEntry<K, V>? Function(dynamic key, dynamic value) mapper,
290+
) {
291+
if (value == null) return <K, V>{};
292+
293+
final result = <K, V>{};
294+
if (value is Map) {
295+
value.forEach((key, value) {
296+
final entry = mapper(key, value);
297+
if (entry != null) {
298+
result[entry.key] = entry.value;
299+
}
300+
});
301+
}
302+
return result;
249303
}

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: json_son
22
description: >-
33
A Dart utility package providing helper functions to flexibly parse JSON values
44
that might have inconsistent data types (e.g., strings to numbers, strings/numbers to booleans).
5-
version: 0.1.11
5+
version: 0.1.22
66
homepage: https://github.com/scalecode-solutions/json_son/blob/main/README.md
77
repository: https://github.com/scalecode-solutions/json_son
88

0 commit comments

Comments
 (0)