Last active
April 12, 2023 23:34
-
-
Save diefferson/39625c9791a7c904ee0453b63df6b978 to your computer and use it in GitHub Desktop.
Json utils
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:base/base.dart'; | |
class Client { | |
String name; | |
String socialName; | |
String email; | |
TaxIdentifier taxIdentifier; | |
Phone mobilePhone; | |
Address? mailAddress; | |
Client({ | |
String? name, | |
String? socialName, | |
String? email, | |
TaxIdentifier? taxIdentifier, | |
Phone? mobilePhone, | |
this.mailAddress, | |
}) : name = name ?? '', | |
socialName = socialName ?? '', | |
email = email ?? '', | |
taxIdentifier = taxIdentifier ?? TaxIdentifier(), | |
mobilePhone = mobilePhone ?? Phone(); | |
Client.fromJson(Map<String, dynamic> json) | |
: name = json.getString('name'), | |
socialName = json.getString('socialName'), | |
email = json.getString('email'), | |
taxIdentifier = json.getObject('taxIdentifier', TaxIdentifier.mapper) ?? | |
TaxIdentifier(), | |
mobilePhone = json.getObject('mobilePhone', Phone.mapper) ?? Phone(), | |
mailAddress = json.getObject('mailAddress', Address.mapper); | |
Map<String, dynamic> toJson() => { | |
"name": name, | |
"socialName": socialName, | |
"email": email, | |
"taxIdentifier": taxIdentifier.toJson(), | |
"mobilePhone": mobilePhone.toJson(), | |
"mailAddress": mailAddress?.toJson(), | |
}..removeWhere((e, dynamic v) => v == null); | |
/// Util method to clone classes | |
Client.clone(Client obj) : this.fromJson(obj.toJson()); | |
/// Util method pass fromJson mapper as a parameter | |
static Client mapper(Map<String, dynamic> map) => Client.fromJson(map); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:io'; | |
import 'package:base/base.dart'; | |
extension JsonUtils on Map<String, dynamic> { | |
T getSafeItem<T>(String key, {required T defaultValue}) { | |
if (containsKey(key)) { | |
return this[key]; | |
} | |
return defaultValue; | |
} | |
T getValue<T>(String key, T defaultValue) { | |
return jsonSafeValue(this, key, defaultValue); | |
} | |
T? getNestedValue<T>(List<String> keys) { | |
return jsonNestedValue(this, keys); | |
} | |
String getString(String key) { | |
return jsonSafeValue(this, key, ''); | |
} | |
String? getStringNull(String key) { | |
return jsonSafeValue<String?>(this, key, null); | |
} | |
String getStringFromKeys(List<String> keys) { | |
String? value; | |
for (final key in keys) { | |
if (containsKey(key)) { | |
value = getString(key); | |
break; | |
} | |
} | |
if (value != null) { | |
return value; | |
} | |
return getString(keys[0]); | |
} | |
File? getFile(String key) { | |
try { | |
return File(getString(key)); | |
} catch (_) { | |
return null; | |
} | |
} | |
File? getFileFromKeys(List<String> keys) { | |
File? value; | |
for (var element in keys) { | |
if (containsKey(element)) { | |
value = getFile(element); | |
continue; | |
} | |
} | |
return value!; | |
} | |
int getInt(String key) { | |
return jsonSafeValue(this, key, 0); | |
} | |
int? getIntNull(String key) { | |
return jsonSafeValue<int?>(this, key, null); | |
} | |
int getIntFromKeys(List<String> keys) { | |
int? value; | |
for (var element in keys) { | |
if (containsKey(element)) { | |
value = getInt(element); | |
continue; | |
} | |
} | |
if (value != null) { | |
return value; | |
} | |
return getInt(keys[0]); | |
} | |
double getDouble(String key) { | |
try { | |
return double.parse(this[key].toString()); | |
} catch (_) { | |
return 0.0; | |
} | |
} | |
double? getDoubleNull(String key) { | |
return jsonSafeValue<double?>(this, key, null); | |
} | |
double getDoubleFromKeys(List<String> keys) { | |
double? value; | |
for (var element in keys) { | |
if (containsKey(element)) { | |
value = getDouble(element); | |
continue; | |
} | |
} | |
if (value != null) { | |
return value; | |
} | |
return getDouble(keys[0]); | |
} | |
bool getBool(String key) { | |
return jsonSafeValue(this, key, false); | |
} | |
bool? getBoolNull(String key) { | |
return jsonSafeValue<bool?>(this, key, null); | |
} | |
bool getBoolFromKeys(List<String> keys) { | |
bool? value; | |
for (var element in keys) { | |
if (containsKey(element)) { | |
value = getBool(element); | |
continue; | |
} | |
} | |
if (value != null) { | |
return value; | |
} | |
return getBool(keys[0]); | |
} | |
T? getObject<T>(String key, T Function(Map<String, dynamic>) itemMap) { | |
return jsonSafeMap(this, key, itemMap); | |
} | |
T getEnum<T>(String key, T Function(String?) enumMap) { | |
return jsonSafeEnum(this, key, enumMap); | |
} | |
T getEnumFromKeys<T>(List<String> keys, T Function(String?) enumMap) { | |
T? value; | |
for (var element in keys) { | |
if (containsKey(element)) { | |
value = jsonSafeEnum(this, element, enumMap); | |
continue; | |
} | |
} | |
if (value != null) { | |
return value; | |
} | |
return jsonSafeEnum(this, keys[0], enumMap); | |
} | |
List<T> getList<T>(String key, [T Function(Map<String, dynamic>)? itemMap]) { | |
return jsonListValue(this, key, itemMap); | |
} | |
DateTime? getDate(String key) { | |
return jsonDateValue(this, key); | |
} | |
DateTime? getDateFromKeys(List<String> keys) { | |
DateTime? value; | |
for (var element in keys) { | |
if (containsKey(element)) { | |
value = getDate(element); | |
continue; | |
} | |
} | |
return value; | |
} | |
Map<String, dynamic> removeNulls() { | |
removeWhere((key, value) => value == null); | |
return this; | |
} | |
} | |
extension JsonListUtils on List<dynamic> { | |
List<T> asList<T>(T Function(Map<String, dynamic>) itemMap) { | |
return jsonAsList(this, itemMap); | |
} | |
} | |
DateTime? jsonDateValue(Map<String, dynamic> json, String key) { | |
if (json[key] is int) { | |
return DateTime.fromMillisecondsSinceEpoch(json[key], isUtc: true); | |
} | |
return (json[key] as String?)?.toDate(); | |
} | |
T jsonSafeValue<T>(Map<String, dynamic> json, String key, T defaultValue) { | |
try { | |
final value = json[key]; | |
if (value != null) { | |
return value; | |
} else { | |
return defaultValue; | |
} | |
} catch (e) { | |
return defaultValue; | |
} | |
} | |
T? jsonSafeMap<T>(Map<String, dynamic> json, String key, | |
T Function(Map<String, dynamic>) itemMap) { | |
if (json.containsKey(key)) { | |
try { | |
return itemMap(json[key]); | |
} catch (e) { | |
return null; | |
} | |
} | |
return null; | |
} | |
T jsonSafeEnum<T>( | |
Map<String, dynamic> json, String key, T Function(String?) enumMap) { | |
try { | |
return enumMap(json[key]); | |
} catch (e) { | |
return enumMap(null); | |
} | |
} | |
List<T> jsonListValue<T>(Map<String, dynamic> json, String key, | |
[T Function(Map<String, dynamic>)? itemMap]) { | |
try { | |
final list = (json[key] as List<dynamic>) | |
..removeWhere((element) => element == null); | |
if (itemMap != null) { | |
return list.map((dynamic e) => itemMap(e)).toList(); | |
} | |
return list.map((e) => e as T).toList(); | |
} catch (e) { | |
return []; | |
} | |
} | |
List<T> jsonAsList<T>( | |
List<dynamic> json, T Function(Map<String, dynamic>) itemMap) { | |
try { | |
return json.map((dynamic e) => itemMap(e)).toList(); | |
} catch (e) { | |
return []; | |
} | |
} | |
T? jsonNestedValue<T>(Map<String, dynamic> json, List<String> nestedKeys) { | |
try { | |
if (nestedKeys.length == 1) { | |
return json[nestedKeys[0]]; | |
} else if (nestedKeys.length == 2) { | |
return json[nestedKeys[0]][nestedKeys[1]]; | |
} else if (nestedKeys.length == 3) { | |
return json[nestedKeys[0]][nestedKeys[1]][nestedKeys[2]]; | |
} else if (nestedKeys.length == 4) { | |
return json[nestedKeys[0]][nestedKeys[1]][nestedKeys[2]][nestedKeys[3]]; | |
} else if (nestedKeys.length == 5) { | |
return json[nestedKeys[0]][nestedKeys[1]][nestedKeys[2]][nestedKeys[3]] | |
[nestedKeys[4]]; | |
} else { | |
safeLog('implements json getJsonValue from ${nestedKeys.length}'); | |
return null; | |
} | |
} catch (e) { | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment