Merge pull request #93 from EoD/fix-string-to-map

fix string to map generation with base64 encoding
This commit is contained in:
mlus 2025-07-12 13:14:19 +08:00 committed by GitHub
commit 2ec7fec89e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2,31 +2,44 @@ package vip.fubuki.playersync.util;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
public class LocalJsonUtil {
public static Map<String,String> StringToMap(String param) {
Map<String,String> map = new HashMap<>();
String s1 = param.substring(1,param.length()-1);
String s2 = s1.trim();
String[] split = s2.split(",");
for (int i = split.length - 1; i >= 0; i--) {
String trim = split[i].trim();
String[] split1 = trim.split("=");
map.put(split1[0],split1[1]);
private static <K> Map<K, String> stringToGenericMap(String param, Function<String, K> keyParser) {
Map<K, String> map = new HashMap<>();
// check if string is at least minimal json
if (param == null || param.length() < 2 || param.equals("{}")) {
return map;
}
// extract string within outermost json brackets {}
String s1 = param.substring(param.indexOf('{')+1, param.lastIndexOf('}')).trim();
if (s1.isEmpty()) {
return map;
}
// split all json elements
for (String split : s1.split(",")) {
String trim = split.trim();
// only check for the first "=" as the values also contain additional "="
int equalIndex = trim.indexOf('=');
if (equalIndex < 0)
continue;
String key = trim.substring(0, equalIndex);
String value = trim.substring(equalIndex + 1);
map.put(keyParser.apply(key), value);
}
return map;
}
public static Map<Integer,String> StringToEntryMap(String param) {
Map<Integer,String> map = new HashMap<>();
String s1 = param.substring(1,param.length()-1);
String s2 = s1.trim();
String[] split = s2.split(",");
for (int i = split.length - 1; i >= 0; i--) {
String trim = split[i].trim();
String[] split1 = trim.split("=");
map.put(Integer.parseInt(split1[0]),split1[1]);
}
return map;
public static Map<String, String> StringToMap(String param) {
return stringToGenericMap(param, Function.identity());
}
public static Map<Integer, String> StringToEntryMap(String param) {
return stringToGenericMap(param, Integer::parseInt);
}
}