ThemeParkAudio/src/main/java/nl/sbdeveloper/mctpaudio/socket/JSONUtil.java

52 lines
1.7 KiB
Java

package nl.sbdeveloper.mctpaudio.socket;
import lombok.experimental.UtilityClass;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.util.UUID;
@UtilityClass
public class JSONUtil {
public static JSONObject parse(String string) {
try {
JSONParser parser = new JSONParser();
return (JSONObject) parser.parse(string);
} catch (ParseException ignored) {
}
return null;
}
public static String getValue(JSONObject object, String string) {
if (object != null && !object.isEmpty()) {
Object obj = object.get(string);
String str = null;
if (obj != null) str = String.valueOf(obj);
return str;
}
return null;
}
public static UUID formatFromInput(String uuid) throws IllegalArgumentException {
if (uuid == null) throw new IllegalArgumentException();
uuid = uuid.trim();
return uuid.length() == 32 ? fromTrimmed(uuid.replaceAll("-", "")) : UUID.fromString(uuid);
}
public static UUID fromTrimmed(String trimmedUUID) throws IllegalArgumentException {
if (trimmedUUID == null) throw new IllegalArgumentException();
StringBuilder builder = new StringBuilder(trimmedUUID.trim());
/* Backwards adding to avoid index adjustments */
try {
builder.insert(20, "-");
builder.insert(16, "-");
builder.insert(12, "-");
builder.insert(8, "-");
} catch (StringIndexOutOfBoundsException e) {
throw new IllegalArgumentException();
}
return UUID.fromString(builder.toString());
}
}