James 3 mēneši atpakaļ
vecāks
revīzija
1fbc433806
2 mainītis faili ar 74 papildinājumiem un 0 dzēšanām
  1. 1 0
      lib/appfx.dart
  2. 73 0
      lib/utils/jsonUtils.dart

+ 1 - 0
lib/appfx.dart

@@ -5,6 +5,7 @@ export 'utils/fileUtils.dart';
 export 'utils/logger.dart';
 export 'utils/riverpodUtils.dart';
 export 'utils/biometricAuthUtils.dart';
+export 'utils/jsonUtils.dart';
 
 export 'store/objbxDbMgrBase.dart';
 export 'store/semDbMgrBase.dart';

+ 73 - 0
lib/utils/jsonUtils.dart

@@ -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;
+  }
+
+}