|
| 1 | +import 'package:json_son/json_son.dart'; |
| 2 | + |
| 3 | +void main() { |
| 4 | + // Example 1: Parsing an integer |
| 5 | + // API might send "123", 123, or 123.0 |
| 6 | + print('--- Integer Parsing ---'); |
| 7 | + dynamic intJsonValue = '123'; |
| 8 | + int? parsedInt = flexibleIntFromJson(intJsonValue); |
| 9 | + print('Parsed int from String "$intJsonValue": $parsedInt'); |
| 10 | + |
| 11 | + intJsonValue = 123; |
| 12 | + parsedInt = flexibleIntFromJson(intJsonValue); |
| 13 | + print('Parsed int from int $intJsonValue: $parsedInt'); |
| 14 | + |
| 15 | + intJsonValue = 123.45; |
| 16 | + parsedInt = flexibleIntFromJson(intJsonValue); |
| 17 | + print('Parsed int from double $intJsonValue: $parsedInt (truncated)'); |
| 18 | + |
| 19 | + intJsonValue = 'abc'; // Unparseable |
| 20 | + parsedInt = flexibleIntFromJson(intJsonValue); |
| 21 | + print('Parsed int from String "$intJsonValue": $parsedInt'); |
| 22 | + |
| 23 | + intJsonValue = null; |
| 24 | + parsedInt = flexibleIntFromJson(intJsonValue); |
| 25 | + print('Parsed int from null: $parsedInt'); |
| 26 | + |
| 27 | + // Example 2: Parsing a boolean |
| 28 | + // API might send "true", "false", "1", "0", true, false, 1, 0 |
| 29 | + print('\n--- Boolean Parsing ---'); |
| 30 | + dynamic boolJsonValue = 'true'; |
| 31 | + bool? parsedBool = flexibleBoolFromJson(boolJsonValue); |
| 32 | + print('Parsed bool from String "$boolJsonValue": $parsedBool'); |
| 33 | + |
| 34 | + boolJsonValue = '0'; |
| 35 | + parsedBool = flexibleBoolFromJson(boolJsonValue); |
| 36 | + print('Parsed bool from String "$boolJsonValue": $parsedBool'); |
| 37 | + |
| 38 | + boolJsonValue = 1; |
| 39 | + parsedBool = flexibleBoolFromJson(boolJsonValue); |
| 40 | + print('Parsed bool from int $boolJsonValue: $parsedBool'); |
| 41 | + |
| 42 | + boolJsonValue = false; |
| 43 | + parsedBool = flexibleBoolFromJson(boolJsonValue); |
| 44 | + print('Parsed bool from bool $boolJsonValue: $parsedBool'); |
| 45 | + |
| 46 | + boolJsonValue = 'yes'; // Unhandled |
| 47 | + parsedBool = flexibleBoolFromJson(boolJsonValue); |
| 48 | + print('Parsed bool from String "$boolJsonValue": $parsedBool'); |
| 49 | + |
| 50 | + boolJsonValue = null; |
| 51 | + parsedBool = flexibleBoolFromJson(boolJsonValue); |
| 52 | + print('Parsed bool from null: $parsedBool'); |
| 53 | + |
| 54 | + // Example 3: Parsing a List of nullable integers |
| 55 | + // API might send a list, a single item, or null |
| 56 | + print('\n--- List<int?> Parsing ---'); |
| 57 | + dynamic listJsonValue = ['1', 2, '3.0', 'four', null, 5]; |
| 58 | + List<int?>? parsedIntList = flexibleListFromJson<int>(listJsonValue, flexibleIntFromJson); |
| 59 | + print('Parsed List<int?> from List $listJsonValue: $parsedIntList'); |
| 60 | + |
| 61 | + listJsonValue = '42'; // Single item |
| 62 | + parsedIntList = flexibleListFromJson<int>(listJsonValue, flexibleIntFromJson); |
| 63 | + print('Parsed List<int?> from single String "$listJsonValue": $parsedIntList'); |
| 64 | + |
| 65 | + listJsonValue = 'invalid'; // Single invalid item |
| 66 | + parsedIntList = flexibleListFromJson<int>(listJsonValue, flexibleIntFromJson); |
| 67 | + print('Parsed List<int?> from single invalid String "$listJsonValue": $parsedIntList'); |
| 68 | + |
| 69 | + listJsonValue = null; |
| 70 | + parsedIntList = flexibleListFromJson<int>(listJsonValue, flexibleIntFromJson); |
| 71 | + print('Parsed List<int?> from null: $parsedIntList'); |
| 72 | + |
| 73 | + // Example 4: Parsing a List of non-null strings |
| 74 | + // API might send a list, a single item, or null. Guarantees a non-null list. |
| 75 | + print('\n--- List<String> (non-null) Parsing ---'); |
| 76 | + dynamic nonNullListJsonValue = ['apple', 123, 'banana', null, 'cherry', true]; |
| 77 | + List<String> parsedNonNullStringList = flexibleListNotNullFromJson<String>(nonNullListJsonValue, flexibleStringFromJson); |
| 78 | + print('Parsed List<String> from List $nonNullListJsonValue: $parsedNonNullStringList'); |
| 79 | + |
| 80 | + nonNullListJsonValue = 'single_item'; |
| 81 | + parsedNonNullStringList = flexibleListNotNullFromJson<String>(nonNullListJsonValue, flexibleStringFromJson); |
| 82 | + print('Parsed List<String> from single item "$nonNullListJsonValue": $parsedNonNullStringList'); |
| 83 | + |
| 84 | + nonNullListJsonValue = null; |
| 85 | + parsedNonNullStringList = flexibleListNotNullFromJson<String>(nonNullListJsonValue, flexibleStringFromJson); |
| 86 | + print('Parsed List<String> from null: $parsedNonNullStringList'); |
| 87 | + |
| 88 | + |
| 89 | + // Example 5: Parsing a DateTime |
| 90 | + // API might send ISO string, timestamp as int, or timestamp as string |
| 91 | + print('\n--- DateTime Parsing ---'); |
| 92 | + dynamic dateTimeJsonValue = '2023-10-26T10:30:00Z'; |
| 93 | + DateTime? parsedDateTime = flexibleDateTimeFromJson(dateTimeJsonValue); |
| 94 | + print('Parsed DateTime from ISO String "$dateTimeJsonValue": $parsedDateTime'); |
| 95 | + |
| 96 | + dateTimeJsonValue = 1698316200000; // Milliseconds since epoch |
| 97 | + parsedDateTime = flexibleDateTimeFromJson(dateTimeJsonValue); |
| 98 | + print('Parsed DateTime from int timestamp $dateTimeJsonValue: $parsedDateTime'); |
| 99 | + |
| 100 | + dateTimeJsonValue = '1698316200000'; // Milliseconds since epoch as string |
| 101 | + parsedDateTime = flexibleDateTimeFromJson(dateTimeJsonValue); |
| 102 | + print('Parsed DateTime from string timestamp "$dateTimeJsonValue": $parsedDateTime'); |
| 103 | + |
| 104 | + dateTimeJsonValue = 'Oct 26, 2023'; // Unparseable by default |
| 105 | + parsedDateTime = flexibleDateTimeFromJson(dateTimeJsonValue); |
| 106 | + print('Parsed DateTime from String "$dateTimeJsonValue": $parsedDateTime'); |
| 107 | + |
| 108 | + dateTimeJsonValue = null; |
| 109 | + parsedDateTime = flexibleDateTimeFromJson(dateTimeJsonValue); |
| 110 | + print('Parsed DateTime from null: $parsedDateTime'); |
| 111 | + |
| 112 | + // Example 6: Parsing a comma-separated list |
| 113 | + // API might send "tag1,tag2, tag3 ", null, or an actual list |
| 114 | + print('\n--- Comma-Separated List Parsing ---'); |
| 115 | + dynamic csvJsonValue = 'apple, banana, cherry ,, orange '; |
| 116 | + List<String>? parsedCsvList = flexibleCommaSeparatedListFromJson(csvJsonValue); |
| 117 | + print('Parsed CSV List from String "$csvJsonValue": $parsedCsvList'); |
| 118 | + |
| 119 | + csvJsonValue = ['one', 'two', null, ' three ']; // Already a list |
| 120 | + parsedCsvList = flexibleCommaSeparatedListFromJson(csvJsonValue); |
| 121 | + print('Parsed CSV List from List $csvJsonValue: $parsedCsvList'); |
| 122 | + |
| 123 | + csvJsonValue = null; |
| 124 | + parsedCsvList = flexibleCommaSeparatedListFromJson(csvJsonValue); |
| 125 | + print('Parsed CSV List from null: $parsedCsvList'); |
| 126 | + |
| 127 | + csvJsonValue = ' '; // Empty string |
| 128 | + parsedCsvList = flexibleCommaSeparatedListFromJson(csvJsonValue); |
| 129 | + print('Parsed CSV List from empty String "$csvJsonValue": $parsedCsvList'); |
| 130 | + |
| 131 | + // Example 7: String Normalization |
| 132 | + print('\n--- String Normalization ---'); |
| 133 | + dynamic stringNormValue = ' Hello World! '; |
| 134 | + print('Original String: "$stringNormValue"'); |
| 135 | + print('Trimmed: "${flexibleTrimmedStringFromJson(stringNormValue)}"'); |
| 136 | + print('Lowercase: "${flexibleLowerStringFromJson(stringNormValue)}"'); |
| 137 | + print('Uppercase: "${flexibleUpperStringFromJson(stringNormValue)}"'); |
| 138 | + |
| 139 | + stringNormValue = ' '; |
| 140 | + print('Original String (whitespace only): "$stringNormValue"'); |
| 141 | + print('Trimmed: "${flexibleTrimmedStringFromJson(stringNormValue)}"'); |
| 142 | + |
| 143 | + stringNormValue = null; |
| 144 | + print('Original String (null): "$stringNormValue"'); |
| 145 | + print('Trimmed: "${flexibleTrimmedStringFromJson(stringNormValue)}"'); |
| 146 | + |
| 147 | + // You can also use these in a Map, simulating a JSON object |
| 148 | + print('\n--- Simulating JSON Object Deserialization ---'); |
| 149 | + Map<String, dynamic> jsonData = { |
| 150 | + 'id': ' 789 ', |
| 151 | + 'isActive': 'TRUE', |
| 152 | + 'price': '19.99', |
| 153 | + 'created_at': '2024-01-01T12:00:00.000Z', |
| 154 | + 'tags_string': ' new, featured, popular item ', |
| 155 | + 'values_list': ['10', 20, '30.5', null, 'forty'], |
| 156 | + 'maybeANumber': null, |
| 157 | + 'singleValueAsList': 'itemA' |
| 158 | + }; |
| 159 | + |
| 160 | + print('Raw ID: ${jsonData['id']} -> Parsed int: ${flexibleIntFromJson(jsonData['id'])}'); |
| 161 | + print('Raw isActive: ${jsonData['isActive']} -> Parsed bool: ${flexibleBoolFromJson(jsonData['isActive'])}'); |
| 162 | + print('Raw price: ${jsonData['price']} -> Parsed double: ${flexibleDoubleFromJson(jsonData['price'])}'); |
| 163 | + print('Raw created_at: ${jsonData['created_at']} -> Parsed DateTime: ${flexibleDateTimeFromJson(jsonData['created_at'])}'); |
| 164 | + print('Raw tags_string: ${jsonData['tags_string']} -> Parsed List<String>: ${flexibleCommaSeparatedListFromJson(jsonData['tags_string'])}'); |
| 165 | + print('Raw values_list: ${jsonData['values_list']} -> Parsed List<num?>: ${flexibleListFromJson<num>(jsonData['values_list'], flexibleNumFromJson)}'); |
| 166 | + print('Raw maybeANumber: ${jsonData['maybeANumber']} -> Parsed int: ${flexibleIntFromJson(jsonData['maybeANumber'])}'); |
| 167 | + print('Raw singleValueAsList: ${jsonData['singleValueAsList']} -> Parsed List<String>: ${flexibleListNotNullFromJson<String>(jsonData['singleValueAsList'], flexibleStringFromJson)}'); |
| 168 | + |
| 169 | +} |
0 commit comments