check for valid JSON on import

This commit is contained in:
Milos Kozak 2022-03-14 14:33:31 +01:00
parent b511cc147f
commit b31e328022

View file

@ -35,6 +35,13 @@ class EncryptedPrefsFormat @Inject constructor(
return if (file.absolutePath.endsWith(".json")) { return if (file.absolutePath.endsWith(".json")) {
val contents = preloadedContents ?: storage.getFileContents(file) val contents = preloadedContents ?: storage.getFileContents(file)
FORMAT_TEST_REGEX.containsMatchIn(contents) FORMAT_TEST_REGEX.containsMatchIn(contents)
try {
// test valid JSON object
JSONObject(contents)
true
} catch (e: Exception) {
false
}
} else { } else {
false false
} }
@ -219,8 +226,11 @@ class EncryptedPrefsFormat @Inject constructor(
override fun loadMetadata(contents: String?): PrefMetadataMap { override fun loadMetadata(contents: String?): PrefMetadataMap {
contents?.let { contents?.let {
val container = JSONObject(contents) return try {
return loadMetadata(container) loadMetadata(JSONObject(contents))
} catch (e: Exception) {
mutableMapOf()
}
} }
return mutableMapOf() return mutableMapOf()
} }