Improve Utils

This commit is contained in:
2dust
2024-11-19 10:09:03 +08:00
parent 5d47777307
commit f67af69dda
3 changed files with 16 additions and 5 deletions

View File

@@ -280,7 +280,7 @@ object AngConfigManager {
val config = CustomFmt.parse(JsonUtil.toJson(srv)) ?: continue
config.subscriptionId = subid
val key = MmkvManager.encodeServerConfig("", config)
MmkvManager.encodeServerRaw(key, JsonUtil.toJsonPretty(srv))
MmkvManager.encodeServerRaw(key, JsonUtil.toJsonPretty(srv)?:"")
count += 1
}
return count

View File

@@ -2,6 +2,8 @@ package com.v2ray.ang.util
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import com.google.gson.JsonPrimitive
import com.google.gson.JsonSerializationContext
import com.google.gson.JsonSerializer
@@ -15,11 +17,13 @@ object JsonUtil {
return gson.toJson(src)
}
fun <T> fromJson(json: String, cls: Class<T>): T {
return gson.fromJson(json, cls)
fun <T> fromJson(src: String, cls: Class<T>): T {
return gson.fromJson(src, cls)
}
fun toJsonPretty(src: Any?): String {
fun toJsonPretty(src: Any?): String? {
if (src == null)
return null
val gsonPre = GsonBuilder()
.setPrettyPrinting()
.disableHtmlEscaping()
@@ -34,4 +38,11 @@ object JsonUtil {
.create()
return gsonPre.toJson(src)
}
fun parseString(src: String?): JsonObject? {
if (src == null)
return null
return JsonParser.parseString(src).getAsJsonObject()
}
}

View File

@@ -295,7 +295,7 @@ object Utils {
fun urlEncode(url: String): String {
return try {
URLEncoder.encode(url, Charsets.UTF_8.toString())
URLEncoder.encode(url, Charsets.UTF_8.toString()).replace("+", "%20")
} catch (e: Exception) {
e.printStackTrace()
url