Commit 8ce99d43 authored by Mygod's avatar Mygod

Refine json parsing

parent 43f5738c
...@@ -27,8 +27,7 @@ import com.github.shadowsocks.preference.DataStore ...@@ -27,8 +27,7 @@ import com.github.shadowsocks.preference.DataStore
import com.github.shadowsocks.utils.DirectBoot import com.github.shadowsocks.utils.DirectBoot
import com.github.shadowsocks.utils.forEachTry import com.github.shadowsocks.utils.forEachTry
import com.github.shadowsocks.utils.printLog import com.github.shadowsocks.utils.printLog
import com.google.gson.JsonParser import com.google.gson.JsonStreamParser
import com.google.gson.stream.JsonReader
import org.json.JSONArray import org.json.JSONArray
import java.io.IOException import java.io.IOException
import java.io.InputStream import java.io.InputStream
...@@ -55,7 +54,6 @@ object ProfileManager { ...@@ -55,7 +54,6 @@ object ProfileManager {
return profile return profile
} }
private val jsonParser = JsonParser()
fun createProfilesFromJson(jsons: Sequence<InputStream>, replace: Boolean = false) { fun createProfilesFromJson(jsons: Sequence<InputStream>, replace: Boolean = false) {
val profiles = if (replace) getAllProfiles()?.associateBy { it.formattedAddress } else null val profiles = if (replace) getAllProfiles()?.associateBy { it.formattedAddress } else null
val feature = if (replace) { val feature = if (replace) {
...@@ -63,9 +61,7 @@ object ProfileManager { ...@@ -63,9 +61,7 @@ object ProfileManager {
} else Core.currentProfile?.first } else Core.currentProfile?.first
val lazyClear = lazy { clear() } val lazyClear = lazy { clear() }
jsons.asIterable().forEachTry { json -> jsons.asIterable().forEachTry { json ->
Profile.parseJson(jsonParser.parse(JsonReader(json.bufferedReader()).apply { Profile.parseJson(JsonStreamParser(json.bufferedReader()).asSequence().single(), feature) {
isLenient = true
}), feature) {
if (replace) { if (replace) {
lazyClear.value lazyClear.value
// if two profiles has the same address, treat them as the same profile and copy stats over // if two profiles has the same address, treat them as the same profile and copy stats over
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment