/// Created by Haoyi on 2022/7/20
part of "converts.dart";

class ConfigStringIntMapStringConvert implements JsonConverter<Map<String, int>, String> {
  const ConfigStringIntMapStringConvert();

  @override
  Map<String, int> fromJson(String json) {
    final entries = json.split(";");
    final Map<String, int> result = {};
    for (String itemStr in entries) {
      try {
        final item = itemStr.split(":");
        if (item.length == 2) {
          result[item[0]] = int.parse(item[1]);
        }
      } catch (error, stacktrace) {
        Log.w("parse ConfigMap error", error: error);
      }
    }
    return result;
  }

  @override
  String toJson(Map<String, int> object) {
    String result = "";
    for (var entry in object.entries) {
      result += "${entry.key}:${entry.value};";
    }
    return result;
  }
}
