Skip to content

Commit 35b3f0d

Browse files
2dustAnGgIt886
authored andcommitted
Add JsonUtil
1 parent 3335ef4 commit 35b3f0d

File tree

11 files changed

+83
-61
lines changed

11 files changed

+83
-61
lines changed

app/src/main/kotlin/com/neko/v2ray/service/V2RayTestService.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ package com.neko.v2ray.service
33
import android.app.Service
44
import android.content.Intent
55
import android.os.IBinder
6-
import com.google.gson.Gson
76
import com.neko.v2ray.AppConfig.MSG_MEASURE_CONFIG
87
import com.neko.v2ray.AppConfig.MSG_MEASURE_CONFIG_CANCEL
98
import com.neko.v2ray.AppConfig.MSG_MEASURE_CONFIG_SUCCESS
109
import com.neko.v2ray.dto.ConfigResult
1110
import com.neko.v2ray.extension.serializable
11+
import com.neko.v2ray.util.JsonUtil
1212
import com.neko.v2ray.util.MessageUtil
1313
import com.neko.v2ray.util.SpeedtestUtil
1414
import com.neko.v2ray.util.Utils
@@ -33,8 +33,8 @@ class V2RayTestService : Service() {
3333
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
3434
when (intent?.getIntExtra("key", 0)) {
3535
MSG_MEASURE_CONFIG -> {
36-
val content = intent.serializable<String>("content")
37-
val config = Gson().fromJson(content, ConfigResult::class.java)
36+
val content = intent.serializable<String>("content") ?: ""
37+
val config = JsonUtil.fromJson(content, ConfigResult::class.java)
3838
realTestScope.launch {
3939
val result = SpeedtestUtil.realPing(config.content)
4040
MessageUtil.sendMsg2UI(this@V2RayTestService, MSG_MEASURE_CONFIG_SUCCESS, Pair(config.guid, result))

app/src/main/kotlin/com/neko/v2ray/ui/RoutingSettingActivity.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ import androidx.recyclerview.widget.ItemTouchHelper
1212
import androidx.recyclerview.widget.LinearLayoutManager
1313
import com.google.android.material.appbar.MaterialToolbar
1414
import com.google.android.material.appbar.CollapsingToolbarLayout
15-
import com.google.gson.Gson
1615
import com.neko.v2ray.AppConfig
1716
import com.neko.v2ray.R
1817
import com.neko.v2ray.databinding.ActivityRoutingSettingBinding
1918
import com.neko.v2ray.dto.RulesetItem
2019
import com.neko.v2ray.extension.toast
2120
import com.neko.v2ray.helper.SimpleItemTouchHelperCallback
21+
import com.neko.v2ray.util.JsonUtil
2222
import com.neko.v2ray.util.MmkvManager
2323
import com.neko.v2ray.util.MmkvManager.settingsStorage
2424
import com.neko.v2ray.util.SettingsManager
@@ -145,7 +145,7 @@ class RoutingSettingActivity : BaseActivity() {
145145
if (rulesetList.isNullOrEmpty()) {
146146
toast(R.string.toast_failure)
147147
} else {
148-
Utils.setClipboard(this, Gson().toJson(rulesetList))
148+
Utils.setClipboard(this, JsonUtil.toJson(rulesetList))
149149
toast(R.string.toast_success)
150150
}
151151
true

app/src/main/kotlin/com/neko/v2ray/ui/ServerCustomConfigActivity.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ import com.google.android.material.appbar.MaterialToolbar
1212
import com.blacksquircle.ui.editorkit.utils.EditorTheme
1313
import com.blacksquircle.ui.language.json.JsonLanguage
1414
import com.google.android.material.appbar.CollapsingToolbarLayout
15-
import com.google.gson.Gson
15+
1616
import com.neko.v2ray.R
1717
import com.neko.v2ray.databinding.ActivityServerCustomConfigBinding
1818
import com.neko.v2ray.dto.EConfigType
1919
import com.neko.v2ray.dto.ServerConfig
2020
import com.neko.v2ray.dto.V2rayConfig
2121
import com.neko.v2ray.extension.toast
22+
import com.neko.v2ray.util.JsonUtil
2223
import com.neko.v2ray.util.MmkvManager
2324
import com.neko.v2ray.util.SoftInputAssist
2425
import com.neko.v2ray.util.Utils
@@ -135,7 +136,7 @@ class ServerCustomConfigActivity : BaseActivity() {
135136
}
136137

137138
val v2rayConfig = try {
138-
Gson().fromJson(binding.editor.text.toString(), V2rayConfig::class.java)
139+
JsonUtil.fromJson(binding.editor.text.toString(), V2rayConfig::class.java)
139140
} catch (e: Exception) {
140141
e.printStackTrace()
141142
ToastCompat.makeText(this, "${getString(R.string.toast_malformed_josn)} ${e.cause?.message}", Toast.LENGTH_LONG).show()

app/src/main/kotlin/com/neko/v2ray/util/AngConfigManager.kt

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import android.content.Context
44
import android.graphics.Bitmap
55
import android.text.TextUtils
66
import android.util.Log
7-
import com.google.gson.Gson
7+
88
import com.google.gson.GsonBuilder
99
import com.google.gson.JsonPrimitive
1010
import com.google.gson.JsonSerializationContext
@@ -277,34 +277,21 @@ object AngConfigManager {
277277
&& server.contains("routing")
278278
) {
279279
try {
280-
//val gson = GsonBuilder().setPrettyPrinting().create()
281-
val gson = GsonBuilder()
282-
.setPrettyPrinting()
283-
.disableHtmlEscaping()
284-
.registerTypeAdapter( // custom serialiser is needed here since JSON by default parse number as Double, core will fail to start
285-
object : TypeToken<Double>() {}.type,
286-
JsonSerializer { src: Double?, _: Type?, _: JsonSerializationContext? ->
287-
JsonPrimitive(
288-
src?.toInt()
289-
)
290-
}
291-
)
292-
.create()
293280
val serverList: Array<Any> =
294-
Gson().fromJson(server, Array<Any>::class.java)
281+
JsonUtil.fromJson(server, Array<Any>::class.java)
295282

296283
if (serverList.isNotEmpty()) {
297284
var count = 0
298285
for (srv in serverList.reversed()) {
299286
val config = ServerConfig.create(EConfigType.CUSTOM)
300287
config.fullConfig =
301-
Gson().fromJson(Gson().toJson(srv), V2rayConfig::class.java)
288+
JsonUtil.fromJson(JsonUtil.toJson(srv), V2rayConfig::class.java)
302289
config.remarks = config.fullConfig?.remarks
303290
?: ("%04d-".format(count + 1) + System.currentTimeMillis()
304291
.toString())
305292
config.subscriptionId = subid
306293
val key = MmkvManager.encodeServerConfig("", config)
307-
MmkvManager.encodeServerRaw(key, gson.toJson(srv))
294+
MmkvManager.encodeServerRaw(key, JsonUtil.toJsonPretty(srv))
308295
count += 1
309296
}
310297
return count
@@ -317,7 +304,7 @@ object AngConfigManager {
317304
// For compatibility
318305
val config = ServerConfig.create(EConfigType.CUSTOM)
319306
config.subscriptionId = subid
320-
config.fullConfig = Gson().fromJson(server, V2rayConfig::class.java)
307+
config.fullConfig = JsonUtil.fromJson(server, V2rayConfig::class.java)
321308
config.remarks = config.fullConfig?.remarks ?: System.currentTimeMillis().toString()
322309
val key = MmkvManager.encodeServerConfig("", config)
323310
MmkvManager.encodeServerRaw(key, server)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.neko.v2ray.util
2+
3+
import com.google.gson.Gson
4+
import com.google.gson.GsonBuilder
5+
import com.google.gson.JsonPrimitive
6+
import com.google.gson.JsonSerializationContext
7+
import com.google.gson.JsonSerializer
8+
import com.google.gson.reflect.TypeToken
9+
import java.lang.reflect.Type
10+
11+
object JsonUtil {
12+
private var gson = Gson()
13+
14+
fun toJson(src: Any?): String {
15+
return gson.toJson(src)
16+
}
17+
18+
fun <T> fromJson(json: String, cls: Class<T>): T {
19+
return gson.fromJson(json, cls)
20+
}
21+
22+
fun toJsonPretty(src: Any?): String {
23+
val gsonPre = GsonBuilder()
24+
.setPrettyPrinting()
25+
.disableHtmlEscaping()
26+
.registerTypeAdapter( // custom serialiser is needed here since JSON by default parse number as Double, core will fail to start
27+
object : TypeToken<Double>() {}.type,
28+
JsonSerializer { src: Double?, _: Type?, _: JsonSerializationContext? ->
29+
JsonPrimitive(
30+
src?.toInt()
31+
)
32+
}
33+
)
34+
.create()
35+
return gsonPre.toJson(src)
36+
}
37+
}

app/src/main/kotlin/com/neko/v2ray/util/MmkvManager.kt

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.neko.v2ray.util
22

3-
import com.google.gson.Gson
3+
44
import com.tencent.mmkv.MMKV
55
import com.neko.v2ray.AppConfig.PREF_IS_BOOTED
66
import com.neko.v2ray.AppConfig.PREF_ROUTING_RULESET
@@ -49,15 +49,15 @@ object MmkvManager {
4949
}
5050

5151
fun encodeServerList(serverList: MutableList<String>) {
52-
mainStorage.encode(KEY_ANG_CONFIGS, Gson().toJson(serverList))
52+
mainStorage.encode(KEY_ANG_CONFIGS, JsonUtil.toJson(serverList))
5353
}
5454

5555
fun decodeServerList(): MutableList<String> {
5656
val json = mainStorage.decodeString(KEY_ANG_CONFIGS)
5757
return if (json.isNullOrBlank()) {
5858
mutableListOf()
5959
} else {
60-
Gson().fromJson(json, Array<String>::class.java).toMutableList()
60+
JsonUtil.fromJson(json, Array<String>::class.java).toMutableList()
6161
}
6262
}
6363

@@ -69,7 +69,7 @@ object MmkvManager {
6969
if (json.isNullOrBlank()) {
7070
return null
7171
}
72-
return Gson().fromJson(json, ServerConfig::class.java)
72+
return JsonUtil.fromJson(json, ServerConfig::class.java)
7373
}
7474

7575
fun decodeProfileConfig(guid: String): ProfileItem? {
@@ -80,12 +80,12 @@ object MmkvManager {
8080
if (json.isNullOrBlank()) {
8181
return null
8282
}
83-
return Gson().fromJson(json, ProfileItem::class.java)
83+
return JsonUtil.fromJson(json, ProfileItem::class.java)
8484
}
8585

8686
fun encodeServerConfig(guid: String, config: ServerConfig): String {
8787
val key = guid.ifBlank { Utils.getUuid() }
88-
serverStorage.encode(key, Gson().toJson(config))
88+
serverStorage.encode(key, JsonUtil.toJson(config))
8989
val serverList = decodeServerList()
9090
if (!serverList.contains(key)) {
9191
serverList.add(0, key)
@@ -101,7 +101,7 @@ object MmkvManager {
101101
server = config.getProxyOutbound()?.getServerAddress(),
102102
serverPort = config.getProxyOutbound()?.getServerPort(),
103103
)
104-
profileStorage.encode(key, Gson().toJson(profile))
104+
profileStorage.encode(key, JsonUtil.toJson(profile))
105105
return key
106106
}
107107

@@ -141,7 +141,7 @@ object MmkvManager {
141141
if (json.isNullOrBlank()) {
142142
return null
143143
}
144-
return Gson().fromJson(json, ServerAffiliationInfo::class.java)
144+
return JsonUtil.fromJson(json, ServerAffiliationInfo::class.java)
145145
}
146146

147147
fun encodeServerTestDelayMillis(guid: String, testResult: Long) {
@@ -150,14 +150,14 @@ object MmkvManager {
150150
}
151151
val aff = decodeServerAffiliationInfo(guid) ?: ServerAffiliationInfo()
152152
aff.testDelayMillis = testResult
153-
serverAffStorage.encode(guid, Gson().toJson(aff))
153+
serverAffStorage.encode(guid, JsonUtil.toJson(aff))
154154
}
155155

156156
fun clearAllTestDelayResults(keys: List<String>?) {
157157
keys?.forEach { key ->
158158
decodeServerAffiliationInfo(key)?.let { aff ->
159159
aff.testDelayMillis = 0
160-
serverAffStorage.encode(key, Gson().toJson(aff))
160+
serverAffStorage.encode(key, JsonUtil.toJson(aff))
161161
}
162162
}
163163
}
@@ -217,7 +217,7 @@ object MmkvManager {
217217
decodeSubsList().forEach { key ->
218218
val json = subStorage.decodeString(key)
219219
if (!json.isNullOrBlank()) {
220-
subscriptions.add(Pair(key, Gson().fromJson(json, SubscriptionItem::class.java)))
220+
subscriptions.add(Pair(key, JsonUtil.fromJson(json, SubscriptionItem::class.java)))
221221
}
222222
}
223223
return subscriptions
@@ -234,7 +234,7 @@ object MmkvManager {
234234

235235
fun encodeSubscription(guid: String, subItem: SubscriptionItem) {
236236
val key = guid.ifBlank { Utils.getUuid() }
237-
subStorage.encode(key, Gson().toJson(subItem))
237+
subStorage.encode(key, JsonUtil.toJson(subItem))
238238

239239
val subsList = decodeSubsList()
240240
if (!subsList.contains(key)) {
@@ -245,19 +245,19 @@ object MmkvManager {
245245

246246
fun decodeSubscription(subscriptionId: String): SubscriptionItem? {
247247
val json = subStorage.decodeString(subscriptionId) ?: return null
248-
return Gson().fromJson(json, SubscriptionItem::class.java)
248+
return JsonUtil.fromJson(json, SubscriptionItem::class.java)
249249
}
250250

251251
fun encodeSubsList(subsList: MutableList<String>) {
252-
mainStorage.encode(KEY_SUB_IDS, Gson().toJson(subsList))
252+
mainStorage.encode(KEY_SUB_IDS, JsonUtil.toJson(subsList))
253253
}
254254

255255
fun decodeSubsList(): MutableList<String> {
256256
val json = mainStorage.decodeString(KEY_SUB_IDS)
257257
return if (json.isNullOrBlank()) {
258258
mutableListOf()
259259
} else {
260-
Gson().fromJson(json, Array<String>::class.java).toMutableList()
260+
JsonUtil.fromJson(json, Array<String>::class.java).toMutableList()
261261
}
262262
}
263263

@@ -270,7 +270,7 @@ object MmkvManager {
270270
assetStorage.allKeys()?.forEach { key ->
271271
val json = assetStorage.decodeString(key)
272272
if (!json.isNullOrBlank()) {
273-
assetUrlItems.add(Pair(key, Gson().fromJson(json, AssetUrlItem::class.java)))
273+
assetUrlItems.add(Pair(key, JsonUtil.fromJson(json, AssetUrlItem::class.java)))
274274
}
275275
}
276276
return assetUrlItems.sortedBy { (_, value) -> value.addedTime }
@@ -282,12 +282,12 @@ object MmkvManager {
282282

283283
fun encodeAsset(assetid: String, assetItem: AssetUrlItem) {
284284
val key = assetid.ifBlank { Utils.getUuid() }
285-
assetStorage.encode(key, Gson().toJson(assetItem))
285+
assetStorage.encode(key, JsonUtil.toJson(assetItem))
286286
}
287287

288288
fun decodeAsset(assetid: String): AssetUrlItem? {
289289
val json = assetStorage.decodeString(assetid) ?: return null
290-
return Gson().fromJson(json, AssetUrlItem::class.java)
290+
return JsonUtil.fromJson(json, AssetUrlItem::class.java)
291291
}
292292

293293
//endregion
@@ -297,14 +297,14 @@ object MmkvManager {
297297
fun decodeRoutingRulesets(): MutableList<RulesetItem>? {
298298
val ruleset = settingsStorage.decodeString(PREF_ROUTING_RULESET)
299299
if (ruleset.isNullOrEmpty()) return null
300-
return Gson().fromJson(ruleset, Array<RulesetItem>::class.java).toMutableList()
300+
return JsonUtil.fromJson(ruleset, Array<RulesetItem>::class.java).toMutableList()
301301
}
302302

303303
fun encodeRoutingRulesets(rulesetList: MutableList<RulesetItem>?) {
304304
if (rulesetList.isNullOrEmpty())
305305
settingsStorage.encode(PREF_ROUTING_RULESET, "")
306306
else
307-
settingsStorage.encode(PREF_ROUTING_RULESET, Gson().toJson(rulesetList))
307+
settingsStorage.encode(PREF_ROUTING_RULESET, JsonUtil.toJson(rulesetList))
308308
}
309309

310310
//endregion

app/src/main/kotlin/com/neko/v2ray/util/PluginUtil.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package com.neko.v2ray.util
33
import android.content.Context
44
import android.os.SystemClock
55
import android.util.Log
6-
import com.google.gson.Gson
76
import com.neko.v2ray.AppConfig.ANG_PACKAGE
87
import com.neko.v2ray.dto.EConfigType
98
import com.neko.v2ray.dto.ServerConfig
@@ -38,8 +37,8 @@ object PluginUtil {
3837
Log.d(packageName, "runPlugin ${configFile.absolutePath}")
3938

4039
configFile.parentFile?.mkdirs()
41-
configFile.writeText(Gson().toJson(hy2Config))
42-
Log.d(packageName, Gson().toJson(hy2Config))
40+
configFile.writeText(JsonUtil.toJson(hy2Config))
41+
Log.d(packageName, JsonUtil.toJson(hy2Config))
4342

4443
runHy2(context, configFile)
4544
}

app/src/main/kotlin/com/neko/v2ray/util/SettingsManager.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package com.neko.v2ray.util
22

33
import android.content.Context
44
import android.text.TextUtils
5-
import com.google.gson.Gsonl
65
import com.neko.v2ray.AppConfig
76
import com.neko.v2ray.dto.RulesetItem
87
import com.neko.v2ray.dto.ServerConfig
@@ -35,7 +34,7 @@ object SettingsManager {
3534
return null
3635
}
3736

38-
return Gson().fromJson(assets, Array<RulesetItem>::class.java).toMutableList()
37+
return JsonUtil.fromJson(assets, Array<RulesetItem>::class.java).toMutableList()
3938
}
4039

4140
fun resetRoutingRulesets(context: Context, index: Int) {
@@ -49,7 +48,7 @@ object SettingsManager {
4948
}
5049

5150
try {
52-
val rulesetList = Gson().fromJson(content, Array<RulesetItem>::class.java).toMutableList()
51+
val rulesetList = JsonUtil.fromJson(content, Array<RulesetItem>::class.java).toMutableList()
5352
if (rulesetList.isNullOrEmpty()) {
5453
return false
5554
}

0 commit comments

Comments
 (0)