mirror of
https://github.com/SimpleMobileTools/Simple-SMS-Messenger.git
synced 2025-01-31 17:44:46 +01:00
Merge remote-tracking branch 'origin/feat/export-sms' into feat/export-sms
# Conflicts: # app/src/main/kotlin/com/simplemobiletools/smsmessenger/dialogs/ImportMessagesDialog.kt # app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/MessagesExporter.kt # app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/MessagesImporter.kt # app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/MessagesReader.kt # app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/MessagesWriter.kt # app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/ExportedMessage.kt # app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/SmsBackup.kt
This commit is contained in:
commit
31ad2d1421
@ -0,0 +1,9 @@
|
||||
package com.simplemobiletools.smsmessenger.extensions.gson
|
||||
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.GsonBuilder
|
||||
import com.google.gson.reflect.TypeToken
|
||||
|
||||
private val gsonBuilder = GsonBuilder().registerTypeAdapter(object: TypeToken<Map<String, Any>>(){}.type, MapDeserializerDoubleAsIntFix())
|
||||
val gson : Gson = gsonBuilder.create()
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.simplemobiletools.smsmessenger.extensions
|
||||
package com.simplemobiletools.smsmessenger.extensions.gson
|
||||
|
||||
import com.google.gson.*
|
||||
import java.math.BigDecimal
|
||||
@ -58,42 +58,3 @@ private fun <T> JsonElement.safeConversion(converter: () -> T?): T? {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
fun JsonObject.optGet(key: String): JsonElement? = get(key)
|
||||
|
||||
fun JsonObject.optGetJsonArray(key: String): JsonArray? = getAsJsonArray(key)
|
||||
|
||||
fun JsonObject.optGetJsonObject(key: String): JsonObject? = getAsJsonObject(key)
|
||||
|
||||
fun JsonObject.optGetJsonPrimitive(key: String): JsonPrimitive? = getAsJsonPrimitive(key)
|
||||
|
||||
fun JsonObject.optString(key: String) = optGet(key)?.asString
|
||||
|
||||
fun JsonObject.optLong(key: String) = optGet(key)?.asLong
|
||||
|
||||
fun JsonObject.optBoolean(key: String) = optGet(key)?.asBoolean
|
||||
|
||||
fun JsonObject.optFloat(key: String) = optGet(key)?.asFloat
|
||||
|
||||
fun JsonObject.optDouble(key: String) = optGet(key)?.asDouble
|
||||
|
||||
fun JsonObject.optJsonObject(key: String) = optGet(key)?.asJsonObject
|
||||
|
||||
fun JsonObject.optJsonArray(key: String) = optGet(key)?.asJsonArray
|
||||
|
||||
fun JsonObject.optJsonPrimitive(key: String) = optGet(key)?.asJsonPrimitive
|
||||
|
||||
fun JsonObject.optInt(key: String) = optGet(key)?.asInt
|
||||
|
||||
fun JsonObject.optBigDecimal(key: String) = optGet(key)?.asBigDecimal
|
||||
|
||||
fun JsonObject.optBigInteger(key: String) = optGet(key)?.asBigInteger
|
||||
|
||||
fun JsonObject.optByte(key: String) = optGet(key)?.asByte
|
||||
|
||||
fun JsonObject.optShort(key: String) = optGet(key)?.asShort
|
||||
|
||||
fun JsonObject.optJsonNull(key: String) = optGet(key)?.asJsonNull
|
||||
|
||||
fun JsonObject.optCharacter(key: String) = optGet(key)?.asCharacter
|
||||
|
@ -0,0 +1,45 @@
|
||||
package com.simplemobiletools.smsmessenger.extensions.gson
|
||||
|
||||
import com.google.gson.JsonArray
|
||||
import com.google.gson.JsonElement
|
||||
import com.google.gson.JsonObject
|
||||
import com.google.gson.JsonPrimitive
|
||||
|
||||
fun JsonObject.optGet(key: String): JsonElement? = get(key)
|
||||
|
||||
fun JsonObject.optGetJsonArray(key: String): JsonArray? = getAsJsonArray(key)
|
||||
|
||||
fun JsonObject.optGetJsonObject(key: String): JsonObject? = getAsJsonObject(key)
|
||||
|
||||
fun JsonObject.optGetJsonPrimitive(key: String): JsonPrimitive? = getAsJsonPrimitive(key)
|
||||
|
||||
fun JsonObject.optString(key: String) = optGet(key)?.asString
|
||||
|
||||
fun JsonObject.optLong(key: String) = optGet(key)?.asLong
|
||||
|
||||
fun JsonObject.optBoolean(key: String) = optGet(key)?.asBoolean
|
||||
|
||||
fun JsonObject.optFloat(key: String) = optGet(key)?.asFloat
|
||||
|
||||
fun JsonObject.optDouble(key: String) = optGet(key)?.asDouble
|
||||
|
||||
fun JsonObject.optJsonObject(key: String) = optGet(key)?.asJsonObject
|
||||
|
||||
fun JsonObject.optJsonArray(key: String) = optGet(key)?.asJsonArray
|
||||
|
||||
fun JsonObject.optJsonPrimitive(key: String) = optGet(key)?.asJsonPrimitive
|
||||
|
||||
fun JsonObject.optInt(key: String) = optGet(key)?.asInt
|
||||
|
||||
fun JsonObject.optBigDecimal(key: String) = optGet(key)?.asBigDecimal
|
||||
|
||||
fun JsonObject.optBigInteger(key: String) = optGet(key)?.asBigInteger
|
||||
|
||||
fun JsonObject.optByte(key: String) = optGet(key)?.asByte
|
||||
|
||||
fun JsonObject.optShort(key: String) = optGet(key)?.asShort
|
||||
|
||||
fun JsonObject.optJsonNull(key: String) = optGet(key)?.asJsonNull
|
||||
|
||||
fun JsonObject.optCharacter(key: String) = optGet(key)?.asCharacter
|
||||
|
@ -0,0 +1,58 @@
|
||||
package com.simplemobiletools.smsmessenger.extensions.gson
|
||||
|
||||
import com.google.gson.JsonDeserializationContext
|
||||
import com.google.gson.JsonDeserializer
|
||||
import com.google.gson.JsonElement
|
||||
import com.google.gson.JsonParseException
|
||||
import com.google.gson.internal.LinkedTreeMap
|
||||
import java.lang.reflect.Type
|
||||
import kotlin.math.ceil
|
||||
|
||||
// https://stackoverflow.com/a/36529534/10552591
|
||||
class MapDeserializerDoubleAsIntFix : JsonDeserializer<Map<String, Any>?> {
|
||||
@Throws(JsonParseException::class)
|
||||
override fun deserialize(json: JsonElement, typeOfT: Type, context: JsonDeserializationContext): Map<String, Any>? {
|
||||
return read(json) as Map<String, Any>?
|
||||
}
|
||||
|
||||
fun read(element: JsonElement): Any? {
|
||||
when {
|
||||
element.isJsonArray -> {
|
||||
val list: MutableList<Any?> = ArrayList()
|
||||
val arr = element.asJsonArray
|
||||
for (anArr in arr) {
|
||||
list.add(read(anArr))
|
||||
}
|
||||
return list
|
||||
}
|
||||
element.isJsonObject -> {
|
||||
val map: MutableMap<String, Any?> = LinkedTreeMap()
|
||||
val obj = element.asJsonObject
|
||||
val entitySet = obj.entrySet()
|
||||
for ((key, value) in entitySet) {
|
||||
map[key] = read(value)
|
||||
}
|
||||
return map
|
||||
}
|
||||
element.isJsonPrimitive -> {
|
||||
val prim = element.asJsonPrimitive
|
||||
when {
|
||||
prim.isBoolean -> {
|
||||
return prim.asBoolean
|
||||
}
|
||||
prim.isString -> {
|
||||
return prim.asString
|
||||
}
|
||||
prim.isNumber -> {
|
||||
val num = prim.asNumber
|
||||
// here you can handle double int/long values
|
||||
// and return any type you want
|
||||
// this solution will transform 3.0 float to long values
|
||||
return if (ceil(num.toDouble()) == num.toLong().toDouble()) num.toLong() else num.toDouble()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user