mirror of
https://github.com/Melledy/Grasscutter.git
synced 2024-11-23 13:37:42 +00:00
Add JSON-related methods to Utils.java
This commit is contained in:
parent
f16c3fb8bc
commit
3425e0f1fc
@ -6,10 +6,7 @@ import java.nio.file.Files;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.time.*;
|
||||
import java.time.temporal.TemporalAdjusters;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import java.util.Locale;
|
||||
import java.util.*;
|
||||
|
||||
import emu.grasscutter.Grasscutter;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
@ -308,10 +305,42 @@ public final class Utils {
|
||||
}
|
||||
|
||||
/**
|
||||
* get language code from Locale
|
||||
* Gets the language code from a given locale.
|
||||
* @param locale A locale.
|
||||
* @return A string in the format of 'XX-XX'.
|
||||
*/
|
||||
public static String getLanguageCode(Locale locale) {
|
||||
return String.format("%s-%s", locale.getLanguage(), locale.getCountry());
|
||||
}
|
||||
|
||||
/**
|
||||
* Base64 encodes a given byte array.
|
||||
* @param toEncode An array of bytes.
|
||||
* @return A base64 encoded string.
|
||||
*/
|
||||
public static String base64Encode(byte[] toEncode) {
|
||||
return Base64.getEncoder().encodeToString(toEncode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Base64 decodes a given string.
|
||||
* @param toDecode A base64 encoded string.
|
||||
* @return An array of bytes.
|
||||
*/
|
||||
public static byte[] base64Decode(String toDecode) {
|
||||
return Base64.getDecoder().decode(toDecode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Safely JSON decodes a given string.
|
||||
* @param jsonData The JSON-encoded data.
|
||||
* @return JSON decoded data, or null if an exception occurred.
|
||||
*/
|
||||
public static <T> T jsonDecode(String jsonData, Class<T> classType) {
|
||||
try {
|
||||
return Grasscutter.getGsonFactory().fromJson(jsonData, classType);
|
||||
} catch (Exception ignored) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user