|
@@ -0,0 +1,73 @@
|
|
|
|
|
+
|
|
|
|
|
+import 'dart:convert';
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+class JsonUtils {
|
|
|
|
|
+
|
|
|
|
|
+ static String encodeJson(Object? value, {
|
|
|
|
|
+ Object? Function(dynamic object)? toEncodable,
|
|
|
|
|
+ }) {
|
|
|
|
|
+ return json.encode(value, toEncodable: toEncodable);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ static dynamic decodeJson(String source, {
|
|
|
|
|
+ Object? Function(Object? key, Object? value)? reviver,
|
|
|
|
|
+ }) {
|
|
|
|
|
+ return json.decode(source, reviver: reviver);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ static String getString(Map? map, String key, {String defaultValue = ''}) {
|
|
|
|
|
+ if (map == null || !map.containsKey(key)) return defaultValue;
|
|
|
|
|
+ final value = map[key];
|
|
|
|
|
+ if (value == null) return defaultValue;
|
|
|
|
|
+ return value.toString();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ static String? tryGetString(Map? map, String key) {
|
|
|
|
|
+ if (map == null || !map.containsKey(key)) return null;
|
|
|
|
|
+ final value = map[key];
|
|
|
|
|
+ if (value == null) return null;
|
|
|
|
|
+ return value.toString();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ static int getInt(Map? map, String key, {int defaultValue = 0}) {
|
|
|
|
|
+ if (map == null || !map.containsKey(key)) return defaultValue;
|
|
|
|
|
+ final value = map[key];
|
|
|
|
|
+ if (value == null) return defaultValue;
|
|
|
|
|
+ if (value is int) return value;
|
|
|
|
|
+ return int.tryParse(value.toString()) ?? defaultValue;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ static int? tryGetInt(Map? map, String key) {
|
|
|
|
|
+ if (map == null || !map.containsKey(key)) return null;
|
|
|
|
|
+ final value = map[key];
|
|
|
|
|
+ if (value == null) return null;
|
|
|
|
|
+ if (value is int) return value;
|
|
|
|
|
+ return int.tryParse(value.toString());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ static DateTime getDateTime(Map? map, String key) {
|
|
|
|
|
+ DateTime defaultValue = DateTime.now();
|
|
|
|
|
+ if (map == null || !map.containsKey(key)) return defaultValue;
|
|
|
|
|
+ final value = map[key];
|
|
|
|
|
+ if (value == null) return defaultValue;
|
|
|
|
|
+ if (value is int) return DateTime.fromMillisecondsSinceEpoch(value);
|
|
|
|
|
+ final dateTime = DateTime.tryParse(value.toString());
|
|
|
|
|
+ return dateTime ?? defaultValue;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ static DateTime? tryGetDateTime(Map? map, String key) {
|
|
|
|
|
+ DateTime? defaultValue;
|
|
|
|
|
+ if (map == null || !map.containsKey(key)) return defaultValue;
|
|
|
|
|
+ final value = map[key];
|
|
|
|
|
+ if (value == null) return defaultValue;
|
|
|
|
|
+ if (value is int) return DateTime.fromMillisecondsSinceEpoch(value);
|
|
|
|
|
+ final dateTime = DateTime.tryParse(value.toString());
|
|
|
|
|
+ return dateTime ?? defaultValue;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+}
|