diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/AbsAdapter.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/AbsDtoAdapter.kt similarity index 89% rename from app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/AbsAdapter.kt rename to app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/AbsDtoAdapter.kt index c55d72ed6..32cbd80ef 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/AbsAdapter.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/AbsDtoAdapter.kt @@ -5,17 +5,15 @@ import dev.ragnarok.fenrir.kJson import dev.ragnarok.fenrir.util.serializeble.json.JsonArray import dev.ragnarok.fenrir.util.serializeble.json.JsonDecoder import dev.ragnarok.fenrir.util.serializeble.json.JsonElement +import dev.ragnarok.fenrir.util.serializeble.json.JsonNull import dev.ragnarok.fenrir.util.serializeble.json.JsonObject import dev.ragnarok.fenrir.util.serializeble.json.JsonPrimitive import dev.ragnarok.fenrir.util.serializeble.json.booleanOrNull import dev.ragnarok.fenrir.util.serializeble.json.doubleOrNull import dev.ragnarok.fenrir.util.serializeble.json.floatOrNull -import dev.ragnarok.fenrir.util.serializeble.json.int import dev.ragnarok.fenrir.util.serializeble.json.intOrNull import dev.ragnarok.fenrir.util.serializeble.json.jsonArray -import dev.ragnarok.fenrir.util.serializeble.json.jsonObject import dev.ragnarok.fenrir.util.serializeble.json.jsonPrimitive -import dev.ragnarok.fenrir.util.serializeble.json.long import dev.ragnarok.fenrir.util.serializeble.json.longOrNull import dev.ragnarok.fenrir.util.serializeble.msgpack.internal.BasicMsgPackDecoder import kotlinx.serialization.KSerializer @@ -25,7 +23,7 @@ import kotlinx.serialization.encoding.Decoder import kotlinx.serialization.encoding.Encoder import kotlin.contracts.contract -abstract class AbsAdapter(name: String) : KSerializer { +abstract class AbsDtoAdapter(name: String) : KSerializer { override val descriptor: SerialDescriptor = buildClassSerialDescriptor(name) override fun deserialize(decoder: Decoder): T { @@ -49,7 +47,7 @@ abstract class AbsAdapter(name: String) : KSerializer { fun checkPrimitive(element: JsonElement?): Boolean { contract { - returns(true) implies (element is JsonPrimitive) + returns(true) implies (element is JsonPrimitive && element !is JsonNull) } return element is JsonPrimitive } @@ -68,7 +66,7 @@ abstract class AbsAdapter(name: String) : KSerializer { } obj ?: return false if (obj.containsKey(name)) { - return obj[name] is JsonPrimitive + return checkPrimitive(obj[name]) } return false } @@ -80,7 +78,7 @@ abstract class AbsAdapter(name: String) : KSerializer { } obj ?: return false if (obj.containsKey(name)) { - return obj[name] is JsonObject + return checkObject(obj[name]) } return false } @@ -92,8 +90,7 @@ abstract class AbsAdapter(name: String) : KSerializer { } obj ?: return false if (obj.containsKey(name)) { - val element = obj[name] - return element is JsonArray && element.jsonArray.size > 0 + return checkArray(obj[name]) } return false } @@ -107,7 +104,7 @@ abstract class AbsAdapter(name: String) : KSerializer { json ?: return fallback return try { val element = json[name] - if (element is JsonPrimitive) element.content else fallback + if (checkPrimitive(element)) element.content else fallback } catch (e: Exception) { if (Constants.IS_DEBUG) { e.printStackTrace() @@ -125,7 +122,7 @@ abstract class AbsAdapter(name: String) : KSerializer { for (i in names) { try { val element = json[i] - if (element is JsonPrimitive) return element.content else continue + if (checkPrimitive(element)) return element.content else continue } catch (e: Exception) { if (Constants.IS_DEBUG) { e.printStackTrace() @@ -164,7 +161,7 @@ abstract class AbsAdapter(name: String) : KSerializer { json ?: return fallback return try { val element = json[name] - (element as? JsonPrimitive)?.intOrNull ?: fallback + if (checkPrimitive(element)) element.intOrNull ?: fallback else fallback } catch (e: Exception) { if (Constants.IS_DEBUG) { e.printStackTrace() @@ -181,7 +178,7 @@ abstract class AbsAdapter(name: String) : KSerializer { json ?: return fallback return try { val element = json[name] - (element as? JsonPrimitive)?.floatOrNull ?: fallback + if (checkPrimitive(element)) element.floatOrNull ?: fallback else fallback } catch (e: Exception) { if (Constants.IS_DEBUG) { e.printStackTrace() @@ -198,7 +195,7 @@ abstract class AbsAdapter(name: String) : KSerializer { json ?: return fallback return try { val element = json[name] - (element as? JsonPrimitive)?.doubleOrNull ?: fallback + if (checkPrimitive(element)) element.doubleOrNull ?: fallback else fallback } catch (e: Exception) { if (Constants.IS_DEBUG) { e.printStackTrace() @@ -215,7 +212,7 @@ abstract class AbsAdapter(name: String) : KSerializer { json ?: return fallback return try { val element = json[name] - (element as? JsonPrimitive)?.longOrNull ?: fallback + if (checkPrimitive(element)) element.longOrNull ?: fallback else fallback } catch (e: Exception) { if (Constants.IS_DEBUG) { e.printStackTrace() @@ -232,7 +229,7 @@ abstract class AbsAdapter(name: String) : KSerializer { return try { for (name in names) { val element = json[name] - if (element is JsonPrimitive) { + if (checkPrimitive(element)) { return element.intOrNull ?: fallback } } @@ -253,7 +250,7 @@ abstract class AbsAdapter(name: String) : KSerializer { return try { for (name in names) { val element = json[name] - if (element is JsonPrimitive) { + if (checkPrimitive(element)) { return element.longOrNull ?: fallback } } @@ -274,7 +271,7 @@ abstract class AbsAdapter(name: String) : KSerializer { array ?: return fallback return try { val opt = opt(array, index) - (opt as? JsonPrimitive)?.longOrNull ?: fallback + if (checkPrimitive(opt)) opt.longOrNull ?: fallback else fallback } catch (e: Exception) { if (Constants.IS_DEBUG) { e.printStackTrace() @@ -292,7 +289,7 @@ abstract class AbsAdapter(name: String) : KSerializer { array ?: return fallback return try { val opt = opt(array, index) - (opt as? JsonPrimitive)?.intOrNull ?: fallback + if (checkPrimitive(opt)) opt.intOrNull ?: fallback else fallback } catch (e: Exception) { if (Constants.IS_DEBUG) { e.printStackTrace() @@ -321,7 +318,7 @@ abstract class AbsAdapter(name: String) : KSerializer { array ?: return fallback return try { val opt = opt(array, index) - if (opt is JsonPrimitive) opt.content else fallback + if (checkPrimitive(opt)) opt.content else fallback } catch (e: Exception) { if (Constants.IS_DEBUG) { e.printStackTrace() @@ -483,7 +480,7 @@ abstract class AbsAdapter(name: String) : KSerializer { array ?: return LongArray(0) val list = LongArray(array.size) for (i in 0 until array.size) { - list[i] = array[i].jsonPrimitive.long + list[i] = array[i].asPrimitiveSafe?.longOrNull ?: 0 } return list } @@ -495,7 +492,7 @@ abstract class AbsAdapter(name: String) : KSerializer { array ?: return IntArray(0) val list = IntArray(array.size) for (i in 0 until array.size) { - list[i] = array[i].jsonPrimitive.int + list[i] = array[i].asPrimitiveSafe?.intOrNull ?: 0 } return list } @@ -508,28 +505,14 @@ abstract class AbsAdapter(name: String) : KSerializer { return Array(array.size) { optString(array, it) ?: "null" } } - fun JsonObject?.getAsJsonArray(name: String): JsonArray? { - return this?.get(name)?.jsonArray - } - - fun JsonObject?.getAsJsonObject(name: String): JsonObject? { - return this?.get(name)?.jsonObject - } - - val JsonElement.asJsonObject: JsonObject - get() = this as? JsonObject ?: error("JsonObject") - val JsonElement.asJsonObjectSafe: JsonObject? - get() = this as? JsonObject + get() = if (checkObject(this)) this else null val JsonElement.asPrimitiveSafe: JsonPrimitive? - get() = this as? JsonPrimitive + get() = if (checkPrimitive(this)) this else null val JsonElement.asJsonArraySafe: JsonArray? - get() = this as? JsonArray - - val JsonElement.asJsonArray: JsonArray - get() = this as? JsonArray ?: error("JsonArray") + get() = if (checkArray(this)) this else null fun JsonObject?.has(name: String): Boolean { return this?.containsKey(name) ?: false diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/ArticleDtoAdapter.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/ArticleDtoAdapter.kt index 99646f86f..71f340544 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/ArticleDtoAdapter.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/ArticleDtoAdapter.kt @@ -7,7 +7,7 @@ import dev.ragnarok.fenrir.util.serializeble.json.JsonElement import dev.ragnarok.fenrir.util.serializeble.json.jsonObject -class ArticleDtoAdapter : AbsAdapter("VKApiArticle") { +class ArticleDtoAdapter : AbsDtoAdapter("VKApiArticle") { @Throws(Exception::class) override fun deserialize( json: JsonElement diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/AttachmentsDtoAdapter.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/AttachmentsDtoAdapter.kt index 201d07471..3425c21d2 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/AttachmentsDtoAdapter.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/AttachmentsDtoAdapter.kt @@ -10,7 +10,7 @@ import dev.ragnarok.fenrir.util.serializeble.json.JsonObject import dev.ragnarok.fenrir.util.serializeble.json.jsonArray import dev.ragnarok.fenrir.util.serializeble.json.jsonObject -class AttachmentsDtoAdapter : AbsAdapter("VKApiAttachments") { +class AttachmentsDtoAdapter : AbsDtoAdapter("VKApiAttachments") { @Throws(Exception::class) override fun deserialize( json: JsonElement diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/AttachmentsEntryDtoAdapter.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/AttachmentsEntryDtoAdapter.kt index c033cce96..1d29edecd 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/AttachmentsEntryDtoAdapter.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/AttachmentsEntryDtoAdapter.kt @@ -7,7 +7,7 @@ import dev.ragnarok.fenrir.api.model.interfaces.VKApiAttachment import dev.ragnarok.fenrir.util.serializeble.json.JsonElement import dev.ragnarok.fenrir.util.serializeble.json.jsonObject -class AttachmentsEntryDtoAdapter : AbsAdapter("VKApiAttachments.Entry") { +class AttachmentsEntryDtoAdapter : AbsDtoAdapter("VKApiAttachments.Entry") { @Throws(Exception::class) override fun deserialize( json: JsonElement diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/AudioDtoAdapter.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/AudioDtoAdapter.kt index 167397043..948ca841d 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/AudioDtoAdapter.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/AudioDtoAdapter.kt @@ -4,9 +4,10 @@ import dev.ragnarok.fenrir.api.model.VKApiAudio import dev.ragnarok.fenrir.nonNullNoEmpty import dev.ragnarok.fenrir.orZero import dev.ragnarok.fenrir.util.serializeble.json.JsonElement +import dev.ragnarok.fenrir.util.serializeble.json.jsonArray import dev.ragnarok.fenrir.util.serializeble.json.jsonObject -class AudioDtoAdapter : AbsAdapter("VKApiAudio") { +class AudioDtoAdapter : AbsDtoAdapter("VKApiAudio") { @Throws(Exception::class) override fun deserialize( json: JsonElement @@ -28,7 +29,7 @@ class AudioDtoAdapter : AbsAdapter("VKApiAudio") { dto.access_key = optString(root, "access_key") dto.isHq = optBoolean(root, "is_hq") if (hasArray(root, "main_artists")) { - val arr = root.getAsJsonArray("main_artists") + val arr = root["main_artists"]?.jsonArray val main_artists: HashMap = HashMap(arr?.size.orZero()) for (i in arr.orEmpty()) { if (!checkObject(i)) { @@ -44,13 +45,13 @@ class AudioDtoAdapter : AbsAdapter("VKApiAudio") { dto.main_artists = main_artists } if (hasObject(root, "album")) { - var thmb = root.getAsJsonObject("album") + var thmb = root["album"]?.jsonObject dto.album_id = optInt(thmb, "id") dto.album_owner_id = optLong(thmb, "owner_id") dto.album_access_key = optString(thmb, "access_key") dto.album_title = optString(thmb, "title") if (hasObject(thmb, "thumb")) { - thmb = thmb.getAsJsonObject("thumb") + thmb = thmb["thumb"]?.jsonObject when { thmb.has("photo_135") -> dto.thumb_image_little = optString( thmb, diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/AudioPlaylistDtoAdapter.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/AudioPlaylistDtoAdapter.kt index 1db335fe7..43a2cafe0 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/AudioPlaylistDtoAdapter.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/AudioPlaylistDtoAdapter.kt @@ -2,9 +2,10 @@ package dev.ragnarok.fenrir.api.adapters import dev.ragnarok.fenrir.api.model.VKApiAudioPlaylist import dev.ragnarok.fenrir.util.serializeble.json.JsonElement +import dev.ragnarok.fenrir.util.serializeble.json.jsonArray import dev.ragnarok.fenrir.util.serializeble.json.jsonObject -class AudioPlaylistDtoAdapter : AbsAdapter("VKApiAudioPlaylist") { +class AudioPlaylistDtoAdapter : AbsDtoAdapter("VKApiAudioPlaylist") { @Throws(Exception::class) override fun deserialize( json: JsonElement @@ -24,43 +25,43 @@ class AudioPlaylistDtoAdapter : AbsAdapter("VKApiAudioPlayli album.Year = optInt(root, "year") if (hasArray(root, "genres")) { val build = StringBuilder() - val gnr = root.getAsJsonArray("genres") + val gnr = root["genres"]?.jsonArray var isFirst = true for (i in gnr.orEmpty()) { if (!checkObject(i)) { continue } if (isFirst) isFirst = false else build.append(", ") - val value = optString(i.asJsonObject, "name") + val value = optString(i.jsonObject, "name") if (value != null) build.append(value) } album.genre = build.toString() } if (hasObject(root, "original")) { - val orig = root.getAsJsonObject("original") + val orig = root["original"]?.jsonObject album.original_id = optInt(orig, "playlist_id") album.original_owner_id = optLong(orig, "owner_id") album.original_access_key = optString(orig, "access_key") } if (hasArray(root, "main_artists")) { - val artist = root.getAsJsonArray("main_artists")?.get(0) + val artist = root["main_artists"]?.jsonArray?.get(0) if (checkObject(artist)) { - album.artist_name = optString(artist.asJsonObject, "name") + album.artist_name = optString(artist.jsonObject, "name") } } if (hasObject(root, "photo")) { - val thmb = root.getAsJsonObject("photo") + val thmb = root["photo"]?.jsonObject if (thmb.has("photo_600")) album.thumb_image = optString(thmb, "photo_600") else if (thmb.has("photo_300")) album.thumb_image = optString(thmb, "photo_300") } else if (hasArray(root, "thumbs")) { - val thmbc = root.getAsJsonArray("thumbs")?.get(0) + val thmbc = root["thumbs"]?.jsonArray?.get(0) if (checkObject(thmbc)) { - if (thmbc.asJsonObject.has("photo_600")) album.thumb_image = optString( - thmbc.asJsonObject, + if (thmbc.jsonObject.has("photo_600")) album.thumb_image = optString( + thmbc.jsonObject, "photo_600" - ) else if (thmbc.asJsonObject.has("photo_300")) album.thumb_image = - optString(thmbc.asJsonObject, "photo_300") + ) else if (thmbc.jsonObject.has("photo_300")) album.thumb_image = + optString(thmbc.jsonObject, "photo_300") } } return album diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/ChatDtoAdapter.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/ChatDtoAdapter.kt index 1cd52f95e..a8b91d676 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/ChatDtoAdapter.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/ChatDtoAdapter.kt @@ -7,10 +7,11 @@ import dev.ragnarok.fenrir.api.model.VKApiUser import dev.ragnarok.fenrir.kJson import dev.ragnarok.fenrir.orZero import dev.ragnarok.fenrir.util.serializeble.json.JsonElement -import dev.ragnarok.fenrir.util.serializeble.json.JsonPrimitive +import dev.ragnarok.fenrir.util.serializeble.json.jsonArray +import dev.ragnarok.fenrir.util.serializeble.json.jsonObject import dev.ragnarok.fenrir.util.serializeble.json.long -class ChatDtoAdapter : AbsAdapter("VKApiChat") { +class ChatDtoAdapter : AbsDtoAdapter("VKApiChat") { @Throws(Exception::class) override fun deserialize( json: JsonElement @@ -19,7 +20,7 @@ class ChatDtoAdapter : AbsAdapter("VKApiChat") { throw Exception("$TAG error parse object") } val dto = VKApiChat() - val root = json.asJsonObject + val root = json.jsonObject dto.id = optLong(root, "id") dto.type = optString(root, "type") dto.title = optString(root, "title") @@ -28,11 +29,11 @@ class ChatDtoAdapter : AbsAdapter("VKApiChat") { dto.photo_200 = optString(root, "photo_200") dto.admin_id = optLong(root, "admin_id") if (hasArray(root, "users")) { - val users = root.getAsJsonArray("users") + val users = root["users"]?.jsonArray dto.users = ArrayList(users?.size.orZero()) for (i in 0 until users?.size.orZero()) { val userElement = users?.get(i) - if (userElement is JsonPrimitive) { + if (checkPrimitive(userElement)) { val user = VKApiUser() user.id = userElement.long val chatUserDto = ChatUserDto() @@ -42,7 +43,7 @@ class ChatDtoAdapter : AbsAdapter("VKApiChat") { if (!checkObject(userElement)) { continue } - val jsonObject = userElement.asJsonObject + val jsonObject = userElement.jsonObject val type = optString(jsonObject, "type") val chatUserDto = ChatUserDto() chatUserDto.type = type diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/ChatJsonResponseDtoAdapter.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/ChatJsonResponseDtoAdapter.kt index 2b76cda6f..01879d27c 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/ChatJsonResponseDtoAdapter.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/ChatJsonResponseDtoAdapter.kt @@ -4,8 +4,10 @@ import dev.ragnarok.fenrir.api.model.VKApiMessage import dev.ragnarok.fenrir.api.model.local_json.ChatJsonResponse import dev.ragnarok.fenrir.kJson import dev.ragnarok.fenrir.util.serializeble.json.JsonElement +import dev.ragnarok.fenrir.util.serializeble.json.jsonArray +import dev.ragnarok.fenrir.util.serializeble.json.jsonObject -class ChatJsonResponseDtoAdapter : AbsAdapter("ChatJsonResponse") { +class ChatJsonResponseDtoAdapter : AbsDtoAdapter("ChatJsonResponse") { @Throws(Exception::class) override fun deserialize( json: JsonElement @@ -14,7 +16,7 @@ class ChatJsonResponseDtoAdapter : AbsAdapter("ChatJsonRespons if (!checkObject(json)) { return story } - val root = json.asJsonObject + val root = json.jsonObject story.type = optString(root, "type") story.page_avatar = optString(root, "page_avatar") story.page_id = optLong(root, "page_id") @@ -26,7 +28,7 @@ class ChatJsonResponseDtoAdapter : AbsAdapter("ChatJsonRespons kJson.decodeFromJsonElement(ChatJsonResponse.Version.serializer(), it) } story.messages = parseArray( - story.type?.let { root.getAsJsonArray(it) }, + story.type?.let { root[it]?.jsonArray }, emptyList(), VKApiMessage.serializer() ) return story diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/ChatUserDtoAdapter.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/ChatUserDtoAdapter.kt index a55111ceb..ac4e54ec1 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/ChatUserDtoAdapter.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/ChatUserDtoAdapter.kt @@ -4,8 +4,9 @@ import dev.ragnarok.fenrir.api.model.ChatUserDto import dev.ragnarok.fenrir.api.model.VKApiUser import dev.ragnarok.fenrir.kJson import dev.ragnarok.fenrir.util.serializeble.json.JsonElement +import dev.ragnarok.fenrir.util.serializeble.json.jsonObject -class ChatUserDtoAdapter : AbsAdapter("ChatUserDto") { +class ChatUserDtoAdapter : AbsDtoAdapter("ChatUserDto") { @Throws(Exception::class) override fun deserialize( json: JsonElement @@ -14,7 +15,7 @@ class ChatUserDtoAdapter : AbsAdapter("ChatUserDto") { kJson.decodeFromJsonElement(VKApiUser.serializer(), json) val dto = ChatUserDto() if (checkObject(json)) { - val root = json.asJsonObject + val root = json.jsonObject dto.invited_by = optLong(root, "invited_by") dto.type = optString(root, "type") } diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/ChatsInfoAdapter.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/ChatsInfoDtoAdapter.kt similarity index 84% rename from app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/ChatsInfoAdapter.kt rename to app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/ChatsInfoDtoAdapter.kt index 41f4912ec..7d622b7da 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/ChatsInfoAdapter.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/ChatsInfoDtoAdapter.kt @@ -4,8 +4,9 @@ import dev.ragnarok.fenrir.api.model.VKApiChat import dev.ragnarok.fenrir.api.model.response.ChatsInfoResponse import dev.ragnarok.fenrir.kJson import dev.ragnarok.fenrir.util.serializeble.json.JsonElement +import dev.ragnarok.fenrir.util.serializeble.json.jsonArray -class ChatsInfoAdapter : AbsAdapter("ChatsInfoResponse") { +class ChatsInfoDtoAdapter : AbsDtoAdapter("ChatsInfoResponse") { @Throws(Exception::class) override fun deserialize( json: JsonElement @@ -21,7 +22,7 @@ class ChatsInfoAdapter : AbsAdapter("ChatsInfoResponse") { } checkArray(json) -> { - val array = json.asJsonArray + val array = json.jsonArray parseArray( array, emptyList(), diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/CommentDtoAdapter.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/CommentDtoAdapter.kt index f506e65ca..281ec2b4d 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/CommentDtoAdapter.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/CommentDtoAdapter.kt @@ -4,9 +4,10 @@ import dev.ragnarok.fenrir.api.model.VKApiAttachments import dev.ragnarok.fenrir.api.model.VKApiComment import dev.ragnarok.fenrir.kJson import dev.ragnarok.fenrir.util.serializeble.json.JsonElement +import dev.ragnarok.fenrir.util.serializeble.json.jsonObject import kotlinx.serialization.builtins.ListSerializer -class CommentDtoAdapter : AbsAdapter("VKApiComment") { +class CommentDtoAdapter : AbsDtoAdapter("VKApiComment") { @Throws(Exception::class) override fun deserialize( json: JsonElement @@ -15,7 +16,7 @@ class CommentDtoAdapter : AbsAdapter("VKApiComment") { throw Exception("$TAG error parse object") } val dto = VKApiComment() - val root = json.asJsonObject + val root = json.jsonObject dto.id = optInt(root, "id") dto.from_id = optLong(root, "from_id") if (dto.from_id == 0L) { @@ -31,7 +32,7 @@ class CommentDtoAdapter : AbsAdapter("VKApiComment") { } } if (hasObject(root, "thread")) { - val threadRoot = root.getAsJsonObject("thread") + val threadRoot = root["thread"]?.jsonObject dto.threads_count = optInt(threadRoot, "count") if (hasArray(threadRoot, "items")) { dto.threads = threadRoot["items"]?.let { @@ -41,7 +42,7 @@ class CommentDtoAdapter : AbsAdapter("VKApiComment") { } dto.pid = optInt(root, "pid") if (hasObject(root, "likes")) { - val likesRoot = root.getAsJsonObject("likes") + val likesRoot = root["likes"]?.jsonObject dto.likes = optInt(likesRoot, "count") dto.user_likes = optBoolean(likesRoot, "user_likes") dto.can_like = optBoolean(likesRoot, "can_like") diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/CommunityDtoAdapter.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/CommunityDtoAdapter.kt index f664ca373..55032f583 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/CommunityDtoAdapter.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/CommunityDtoAdapter.kt @@ -5,10 +5,12 @@ import dev.ragnarok.fenrir.api.model.* import dev.ragnarok.fenrir.api.util.VKStringUtils import dev.ragnarok.fenrir.kJson import dev.ragnarok.fenrir.util.serializeble.json.JsonElement +import dev.ragnarok.fenrir.util.serializeble.json.jsonArray +import dev.ragnarok.fenrir.util.serializeble.json.jsonObject import java.util.Locale import kotlin.math.abs -class CommunityDtoAdapter : AbsAdapter("VKApiCommunity") { +class CommunityDtoAdapter : AbsDtoAdapter("VKApiCommunity") { @Throws(Exception::class) override fun deserialize( json: JsonElement @@ -16,7 +18,7 @@ class CommunityDtoAdapter : AbsAdapter("VKApiCommunity") { if (!checkObject(json)) { throw Exception("$TAG error parse object") } - val root = json.asJsonObject + val root = json.jsonObject val dto = VKApiCommunity() dto.id = optLong(root, "id") dto.name = optString(root, Fields.GROUP_FIELDS.NAME) @@ -26,14 +28,14 @@ class CommunityDtoAdapter : AbsAdapter("VKApiCommunity") { String.format(Locale.getDefault(), "club%d", abs(dto.id)) ) if (hasObject(root, Fields.GROUP_FIELDS.MENU)) { - val pMenu = root.getAsJsonObject(Fields.GROUP_FIELDS.MENU) + val pMenu = root[Fields.GROUP_FIELDS.MENU]?.jsonObject if (hasArray(pMenu, "items")) { dto.menu = ArrayList() - for (i in pMenu.getAsJsonArray("items").orEmpty()) { + for (i in pMenu["items"]?.jsonArray.orEmpty()) { if (!checkObject(i)) { continue } - val p = i.asJsonObject + val p = i.jsonObject val o = VKApiCommunity.Menu() o.id = optInt(p, "id") o.title = optString(p, "title") @@ -42,11 +44,11 @@ class CommunityDtoAdapter : AbsAdapter("VKApiCommunity") { if (hasArray(p, "cover")) { var wd = 0 var hd = 0 - for (s in p.getAsJsonArray("cover").orEmpty()) { + for (s in p["cover"]?.jsonArray.orEmpty()) { if (!checkObject(s)) { continue } - val f = s.asJsonObject + val f = s.jsonObject if (optInt(f, "width") > wd || optInt(f, "height") > hd) { wd = optInt(f, "width") hd = optInt(f, "height") @@ -91,7 +93,7 @@ class CommunityDtoAdapter : AbsAdapter("VKApiCommunity") { } } if (hasObject(root, Fields.GROUP_FIELDS.BAN_INFO)) { - val banInfo = root.getAsJsonObject(Fields.GROUP_FIELDS.BAN_INFO) + val banInfo = root[Fields.GROUP_FIELDS.BAN_INFO]?.jsonObject dto.blacklisted = true dto.ban_end_date = optLong(banInfo, "end_date") dto.ban_comment = optString(banInfo, "comment") @@ -99,7 +101,7 @@ class CommunityDtoAdapter : AbsAdapter("VKApiCommunity") { dto.description = optString(root, Fields.GROUP_FIELDS.DESCRIPTION) dto.members_count = optInt(root, Fields.GROUP_FIELDS.MEMBERS_COUNT) if (hasObject(root, Fields.GROUP_FIELDS.COUNTERS)) { - val counters = root.getAsJsonObject(Fields.GROUP_FIELDS.COUNTERS) + val counters = root[Fields.GROUP_FIELDS.COUNTERS]?.jsonObject dto.counters = counters?.let { kJson.decodeFromJsonElement(VKApiCommunity.Counters.serializer(), it) } @@ -109,7 +111,7 @@ class CommunityDtoAdapter : AbsAdapter("VKApiCommunity") { dto.counters = VKApiCommunity.Counters() } dto.counters?.chats = optInt( - root.getAsJsonObject(Fields.GROUP_FIELDS.CHATS_STATUS), + root[Fields.GROUP_FIELDS.CHATS_STATUS]?.jsonObject, "count", VKApiCommunity.Counters.NO_COUNTER ) @@ -130,12 +132,12 @@ class CommunityDtoAdapter : AbsAdapter("VKApiCommunity") { } } dto.contacts = parseArray( - root.getAsJsonArray(Fields.GROUP_FIELDS.CONTACTS), + root[Fields.GROUP_FIELDS.CONTACTS]?.jsonArray, null, VKApiCommunity.Contact.serializer() ) dto.links = parseArray( - root.getAsJsonArray(Fields.GROUP_FIELDS.LINKS), + root[Fields.GROUP_FIELDS.LINKS]?.jsonArray, null, VKApiCommunity.Link.serializer() ) diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/CustomCommentsResponseAdapter.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/CustomCommentsResponseDtoAdapter.kt similarity index 68% rename from app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/CustomCommentsResponseAdapter.kt rename to app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/CustomCommentsResponseDtoAdapter.kt index 90fc19786..7aaf19bc0 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/CustomCommentsResponseAdapter.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/CustomCommentsResponseDtoAdapter.kt @@ -3,11 +3,12 @@ package dev.ragnarok.fenrir.api.adapters import dev.ragnarok.fenrir.api.model.response.CustomCommentsResponse import dev.ragnarok.fenrir.kJson import dev.ragnarok.fenrir.util.serializeble.json.JsonElement -import dev.ragnarok.fenrir.util.serializeble.json.JsonPrimitive import dev.ragnarok.fenrir.util.serializeble.json.intOrNull +import dev.ragnarok.fenrir.util.serializeble.json.jsonObject import dev.ragnarok.fenrir.util.serializeble.json.jsonPrimitive -class CustomCommentsResponseAdapter : AbsAdapter("CustomCommentsResponse") { +class CustomCommentsResponseDtoAdapter : + AbsDtoAdapter("CustomCommentsResponse") { @Throws(Exception::class) override fun deserialize( json: JsonElement @@ -16,7 +17,7 @@ class CustomCommentsResponseAdapter : AbsAdapter("Custom throw Exception("$TAG error parse object") } val response = CustomCommentsResponse() - val root = json.asJsonObject + val root = json.jsonObject val main = root["main"] if (checkObject(main)) { response.main = @@ -25,22 +26,22 @@ class CustomCommentsResponseAdapter : AbsAdapter("Custom if (root.has("first_id")) { val firstIdJson = root["first_id"] response.firstId = - if (firstIdJson is JsonPrimitive) firstIdJson.jsonPrimitive.intOrNull else null + if (checkPrimitive(firstIdJson)) firstIdJson.jsonPrimitive.intOrNull else null } if (root.has("last_id")) { val lastIdJson = root["last_id"] response.lastId = - if (lastIdJson is JsonPrimitive) lastIdJson.jsonPrimitive.intOrNull else null + if (checkPrimitive(lastIdJson)) lastIdJson.jsonPrimitive.intOrNull else null } if (root.has("admin_level")) { val adminLevelJson = root["admin_level"] response.admin_level = - if (adminLevelJson is JsonPrimitive) adminLevelJson.jsonPrimitive.intOrNull else null + if (checkPrimitive(adminLevelJson)) adminLevelJson.jsonPrimitive.intOrNull else null } return response } companion object { - private val TAG = CustomCommentsResponseAdapter::class.java.simpleName + private val TAG = CustomCommentsResponseDtoAdapter::class.java.simpleName } } \ No newline at end of file diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/DocsEntryDtoAdapter.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/DocsEntryDtoAdapter.kt index 0f22ede8a..ac5c064c7 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/DocsEntryDtoAdapter.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/DocsEntryDtoAdapter.kt @@ -3,13 +3,14 @@ package dev.ragnarok.fenrir.api.adapters import dev.ragnarok.fenrir.api.model.VKApiDoc import dev.ragnarok.fenrir.kJson import dev.ragnarok.fenrir.util.serializeble.json.JsonElement +import dev.ragnarok.fenrir.util.serializeble.json.jsonObject -class DocsEntryDtoAdapter : AbsAdapter("VKApiDoc.Entry") { +class DocsEntryDtoAdapter : AbsDtoAdapter("VKApiDoc.Entry") { @Throws(Exception::class) override fun deserialize( json: JsonElement ): VKApiDoc.Entry { - val o = json.asJsonObject + val o = json.jsonObject val type = optString(o, "type") ?: throw NullPointerException("VKApiDoc.Entry type") val entry: VKApiDoc.Entry val pp: VKApiDoc = diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/FaveLinkDtoAdapter.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/FaveLinkDtoAdapter.kt index ffaf48401..ec481a568 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/FaveLinkDtoAdapter.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/FaveLinkDtoAdapter.kt @@ -5,8 +5,9 @@ import dev.ragnarok.fenrir.api.model.VKApiPhoto import dev.ragnarok.fenrir.kJson import dev.ragnarok.fenrir.util.serializeble.json.JsonElement import dev.ragnarok.fenrir.util.serializeble.json.JsonObject +import dev.ragnarok.fenrir.util.serializeble.json.jsonObject -class FaveLinkDtoAdapter : AbsAdapter("FaveLinkDto") { +class FaveLinkDtoAdapter : AbsDtoAdapter("FaveLinkDto") { @Throws(Exception::class) override fun deserialize( json: JsonElement @@ -15,9 +16,9 @@ class FaveLinkDtoAdapter : AbsAdapter("FaveLinkDto") { throw Exception("$TAG error parse object") } val link = FaveLinkDto() - var root: JsonObject? = json.asJsonObject + var root: JsonObject? = json.jsonObject if (!hasObject(root, "link")) return link - root = root["link"]?.asJsonObject + root = root["link"]?.jsonObject link.id = optString(root, "id") link.description = optString(root, "description") if (hasObject(root, "photo")) { diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/FeedbackDtoAdapter.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/FeedbackDtoAdapter.kt index 3aac5cb4a..a4f12c576 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/FeedbackDtoAdapter.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/FeedbackDtoAdapter.kt @@ -6,8 +6,9 @@ import dev.ragnarok.fenrir.kJson import dev.ragnarok.fenrir.util.serializeble.json.JsonElement import dev.ragnarok.fenrir.util.serializeble.json.JsonObject import dev.ragnarok.fenrir.util.serializeble.json.decodeFromJsonElementOrNull +import dev.ragnarok.fenrir.util.serializeble.json.jsonObject -class FeedbackDtoAdapter : AbsAdapter("VKApiBaseFeedback") { +class FeedbackDtoAdapter : AbsDtoAdapter("VKApiBaseFeedback") { @Throws(Exception::class) override fun deserialize( json: JsonElement @@ -15,7 +16,7 @@ class FeedbackDtoAdapter : AbsAdapter("VKApiBaseFeedback") { if (!checkObject(json)) { throw Exception("$TAG error parse object") } - val root = json.asJsonObject + val root = json.jsonObject return when (val type = optString(root, "type")) { "follow", "friend_accepted" -> USERS_PARSER.parse( root @@ -57,7 +58,7 @@ class FeedbackDtoAdapter : AbsAdapter("VKApiBaseFeedback") { val dto = createDto() dto?.type = optString(root, "type") dto?.date = optLong(root, "date") - if (root.has("reply")) { + if (hasObject(root, "reply")) { dto?.reply = kJson.decodeFromJsonElementOrNull( VKApiComment.serializer(), @@ -165,7 +166,7 @@ class FeedbackDtoAdapter : AbsAdapter("VKApiBaseFeedback") { } dto.comments_of = when (dto.type) { "reply_comment" -> { - root.getAsJsonObject("parent")?.get("post")?.let { + root["parent"]?.asJsonObjectSafe?.get("post")?.let { kJson.decodeFromJsonElement( VKApiPost.serializer(), it @@ -174,7 +175,7 @@ class FeedbackDtoAdapter : AbsAdapter("VKApiBaseFeedback") { } "reply_comment_photo" -> { - root.getAsJsonObject("parent")?.get("photo")?.let { + root["parent"]?.asJsonObjectSafe?.get("photo")?.let { kJson.decodeFromJsonElement( VKApiPhoto.serializer(), it @@ -183,7 +184,7 @@ class FeedbackDtoAdapter : AbsAdapter("VKApiBaseFeedback") { } "reply_comment_video" -> { - root.getAsJsonObject("parent")?.get("video")?.let { + root["parent"]?.asJsonObjectSafe?.get("video")?.let { kJson.decodeFromJsonElement( VKApiVideo.serializer(), it @@ -255,7 +256,7 @@ class FeedbackDtoAdapter : AbsAdapter("VKApiBaseFeedback") { val dto = super.parse(root) dto.commented = when (dto.type) { "like_comment" -> { - root.getAsJsonObject("parent")?.get("post")?.let { + root["parent"]?.asJsonObjectSafe?.get("post")?.let { kJson.decodeFromJsonElement( VKApiPost.serializer(), it @@ -264,7 +265,7 @@ class FeedbackDtoAdapter : AbsAdapter("VKApiBaseFeedback") { } "like_comment_photo" -> { - root.getAsJsonObject("parent")?.get("photo")?.let { + root["parent"]?.asJsonObjectSafe?.get("photo")?.let { kJson.decodeFromJsonElement( VKApiPhoto.serializer(), it @@ -273,7 +274,7 @@ class FeedbackDtoAdapter : AbsAdapter("VKApiBaseFeedback") { } "like_comment_video" -> { - root.getAsJsonObject("parent")?.get("video")?.let { + root["parent"]?.asJsonObjectSafe?.get("video")?.let { kJson.decodeFromJsonElement( VKApiVideo.serializer(), it @@ -282,7 +283,7 @@ class FeedbackDtoAdapter : AbsAdapter("VKApiBaseFeedback") { } "like_comment_topic" -> { - root.getAsJsonObject("parent")?.get("topic")?.let { + root["parent"]?.asJsonObjectSafe?.get("topic")?.let { kJson.decodeFromJsonElement( VKApiTopic.serializer(), it diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/FeedbackUserArrayDtoAdapter.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/FeedbackUserArrayDtoAdapter.kt index 7eb01fa6e..3818e8e67 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/FeedbackUserArrayDtoAdapter.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/FeedbackUserArrayDtoAdapter.kt @@ -3,23 +3,25 @@ package dev.ragnarok.fenrir.api.adapters import dev.ragnarok.fenrir.api.model.feedback.UserArray import dev.ragnarok.fenrir.orZero import dev.ragnarok.fenrir.util.serializeble.json.JsonElement +import dev.ragnarok.fenrir.util.serializeble.json.jsonArray +import dev.ragnarok.fenrir.util.serializeble.json.jsonObject -class FeedbackUserArrayDtoAdapter : AbsAdapter("UserArray") { +class FeedbackUserArrayDtoAdapter : AbsDtoAdapter("UserArray") { @Throws(Exception::class) override fun deserialize( json: JsonElement ): UserArray { - val root = json.asJsonObject + val root = json.jsonObject val dto = UserArray() dto.count = optInt(root, "count") if (hasArray(root, "items")) { - val array = root.getAsJsonArray("items") + val array = root["items"]?.jsonArray dto.ids = LongArray(array?.size.orZero()) for (i in 0 until array?.size.orZero()) { if (!checkObject(array?.get(i))) { continue } - dto.ids?.set(i, optLong(array?.get(i)?.asJsonObject, "from_id", 0)) + dto.ids?.set(i, optLong(array?.get(i)?.jsonObject, "from_id", 0)) } } else { dto.ids = LongArray(0) diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/FeedbackVKOfficialDtoAdapter.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/FeedbackVKOfficialDtoAdapter.kt index 2a285e547..2f6c28e7c 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/FeedbackVKOfficialDtoAdapter.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/FeedbackVKOfficialDtoAdapter.kt @@ -15,7 +15,8 @@ import dev.ragnarok.fenrir.util.Utils import dev.ragnarok.fenrir.util.serializeble.json.* import kotlinx.serialization.builtins.ListSerializer -class FeedbackVKOfficialDtoAdapter : AbsAdapter("FeedbackVKOfficialList") { +class FeedbackVKOfficialDtoAdapter : + AbsDtoAdapter("FeedbackVKOfficialList") { @Throws(Exception::class) override fun deserialize( json: JsonElement @@ -157,7 +158,7 @@ class FeedbackVKOfficialDtoAdapter : AbsAdapter("Feedbac if (hasArray(main_item, "image_object")) { val jsonPhotos2 = main_item["image_object"]?.jsonArray dto.iconURL = - jsonPhotos2?.get(jsonPhotos2.size - 1)?.jsonObject?.get("url")?.jsonPrimitive?.content + jsonPhotos2?.get(jsonPhotos2.size - 1)?.asJsonObjectSafe?.get("url")?.asPrimitiveSafe?.content } if ("photo" == optString(main_item, "type")) { attachments.add( diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/GeoDtoAdapter.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/GeoDtoAdapter.kt index 46f8d2b03..9a5a96e58 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/GeoDtoAdapter.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/GeoDtoAdapter.kt @@ -4,7 +4,7 @@ import dev.ragnarok.fenrir.api.model.VKApiGeo import dev.ragnarok.fenrir.util.serializeble.json.JsonElement import dev.ragnarok.fenrir.util.serializeble.json.jsonObject -class GeoDtoAdapter : AbsAdapter("VKApiGeo") { +class GeoDtoAdapter : AbsDtoAdapter("VKApiGeo") { @Throws(Exception::class) override fun deserialize( json: JsonElement diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/GroupSettingsAdapter.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/GroupSettingsDtoAdapter.kt similarity index 85% rename from app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/GroupSettingsAdapter.kt rename to app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/GroupSettingsDtoAdapter.kt index 4d1fc24cb..d35eb0c78 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/GroupSettingsAdapter.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/GroupSettingsDtoAdapter.kt @@ -3,11 +3,12 @@ package dev.ragnarok.fenrir.api.adapters import dev.ragnarok.fenrir.api.model.GroupSettingsDto import dev.ragnarok.fenrir.kJson import dev.ragnarok.fenrir.util.serializeble.json.JsonElement -import dev.ragnarok.fenrir.util.serializeble.json.JsonPrimitive import dev.ragnarok.fenrir.util.serializeble.json.int +import dev.ragnarok.fenrir.util.serializeble.json.jsonArray +import dev.ragnarok.fenrir.util.serializeble.json.jsonObject import dev.ragnarok.fenrir.util.serializeble.json.jsonPrimitive -class GroupSettingsAdapter : AbsAdapter("GroupSettingsDto") { +class GroupSettingsDtoAdapter : AbsDtoAdapter("GroupSettingsDto") { @Throws(Exception::class) override fun deserialize( json: JsonElement @@ -16,7 +17,7 @@ class GroupSettingsAdapter : AbsAdapter("GroupSettingsDto") { throw Exception("$TAG error parse object") } val dto = GroupSettingsDto() - val root = json.asJsonObject + val root = json.jsonObject dto.title = optString(root, "title") dto.description = optString(root, "description") dto.address = optString(root, "address") @@ -37,7 +38,7 @@ class GroupSettingsAdapter : AbsAdapter("GroupSettingsDto") { dto.public_date = optString(root, "public_date") dto.public_date_label = optString(root, "public_date_label") val publicCategoryJson = root["public_category"] - if (publicCategoryJson is JsonPrimitive) { + if (checkPrimitive(publicCategoryJson)) { try { dto.public_category = publicCategoryJson.jsonPrimitive.int.toString() } catch (e: Exception) { @@ -45,7 +46,7 @@ class GroupSettingsAdapter : AbsAdapter("GroupSettingsDto") { } } val publicSubCategoryJson = root["public_subcategory"] - if (publicSubCategoryJson is JsonPrimitive) { + if (checkPrimitive(publicSubCategoryJson)) { try { dto.public_subcategory = publicSubCategoryJson.jsonPrimitive.int.toString() } catch (e: Exception) { @@ -54,7 +55,7 @@ class GroupSettingsAdapter : AbsAdapter("GroupSettingsDto") { } if (hasArray(root, "public_category_list")) { dto.public_category_list = parseArray( - root.getAsJsonArray("public_category_list"), emptyList(), + root["public_category_list"]?.jsonArray, emptyList(), GroupSettingsDto.PublicCategory.serializer() ) } @@ -74,6 +75,6 @@ class GroupSettingsAdapter : AbsAdapter("GroupSettingsDto") { } companion object { - private val TAG = GroupSettingsAdapter::class.java.simpleName + private val TAG = GroupSettingsDtoAdapter::class.java.simpleName } } \ No newline at end of file diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/JsonStringDtoAdapter.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/JsonStringDtoAdapter.kt index 1e93f6b54..856a96212 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/JsonStringDtoAdapter.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/JsonStringDtoAdapter.kt @@ -2,8 +2,9 @@ package dev.ragnarok.fenrir.api.adapters import dev.ragnarok.fenrir.api.model.VKApiJsonString import dev.ragnarok.fenrir.util.serializeble.json.JsonElement +import dev.ragnarok.fenrir.util.serializeble.json.jsonObject -class JsonStringDtoAdapter : AbsAdapter("VKApiJsonString") { +class JsonStringDtoAdapter : AbsDtoAdapter("VKApiJsonString") { @Throws(Exception::class) override fun deserialize( json: JsonElement @@ -12,7 +13,7 @@ class JsonStringDtoAdapter : AbsAdapter("VKApiJsonString") { if (!checkObject(json)) { return story } - val root = json.asJsonObject + val root = json.jsonObject story.json_data = root.toString() return story } diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/LikesListAdapter.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/LikesListDtoAdapter.kt similarity index 80% rename from app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/LikesListAdapter.kt rename to app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/LikesListDtoAdapter.kt index 65fde05cf..ffe149381 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/LikesListAdapter.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/LikesListDtoAdapter.kt @@ -8,8 +8,10 @@ import dev.ragnarok.fenrir.kJson import dev.ragnarok.fenrir.orZero import dev.ragnarok.fenrir.requireNonNull import dev.ragnarok.fenrir.util.serializeble.json.JsonElement +import dev.ragnarok.fenrir.util.serializeble.json.jsonArray +import dev.ragnarok.fenrir.util.serializeble.json.jsonObject -class LikesListAdapter : AbsAdapter("LikesListResponse") { +class LikesListDtoAdapter : AbsDtoAdapter("LikesListResponse") { @Throws(Exception::class) override fun deserialize( json: JsonElement @@ -18,16 +20,16 @@ class LikesListAdapter : AbsAdapter("LikesListResponse") { throw Exception("$TAG error parse object") } val response = LikesListResponse() - val root = json.asJsonObject + val root = json.jsonObject response.count = optInt(root, "count") if (hasArray(root, "items")) { - val itemsArray = root.getAsJsonArray("items") + val itemsArray = root["items"]?.jsonArray response.owners = ArrayList(itemsArray?.size.orZero()) for (i in 0 until itemsArray?.size.orZero()) { if (!checkObject(itemsArray?.get(i))) { continue } - val itemRoot = itemsArray?.get(i)?.asJsonObject + val itemRoot = itemsArray?.get(i)?.jsonObject val type = optString(itemRoot, "type") var owner: VKApiOwner? = null if ("profile" == type || "user" == type) { @@ -48,6 +50,6 @@ class LikesListAdapter : AbsAdapter("LikesListResponse") { } companion object { - private val TAG = LikesListAdapter::class.java.simpleName + private val TAG = LikesListDtoAdapter::class.java.simpleName } } \ No newline at end of file diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/LongpollUpdateAdapter.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/LongpollUpdateDtoAdapter.kt similarity index 97% rename from app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/LongpollUpdateAdapter.kt rename to app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/LongpollUpdateDtoAdapter.kt index 86636a78d..dee584620 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/LongpollUpdateAdapter.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/LongpollUpdateDtoAdapter.kt @@ -11,12 +11,12 @@ import dev.ragnarok.fenrir.util.Utils import dev.ragnarok.fenrir.util.serializeble.json.* import java.util.Collections -class LongpollUpdateAdapter : AbsAdapter("AbsLongpollEvent?") { +class LongpollUpdateDtoAdapter : AbsDtoAdapter("AbsLongpollEvent?") { @Throws(Exception::class) override fun deserialize( json: JsonElement ): AbsLongpollEvent? { - val array = json.asJsonArray + val array = json.jsonArray val action = array[0].jsonPrimitive.int return deserialize(action, array) } @@ -125,7 +125,7 @@ class LongpollUpdateAdapter : AbsAdapter("AbsLongpollEvent?") update.sourceAct = optString(extra, "source_act") update.sourceMid = optLong(extra, "source_mid") update.payload = optString(extra, "payload") - if (extra.has("keyboard")) { + if (hasObject(extra, "keyboard")) { update.keyboard = extra["keyboard"]?.let { kJson.decodeFromJsonElement( diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/LongpollUpdatesAdapter.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/LongpollUpdatesDtoAdapter.kt similarity index 74% rename from app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/LongpollUpdatesAdapter.kt rename to app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/LongpollUpdatesDtoAdapter.kt index 945c43ed9..57c33a6d0 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/LongpollUpdatesAdapter.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/LongpollUpdatesDtoAdapter.kt @@ -5,8 +5,10 @@ import dev.ragnarok.fenrir.api.model.longpoll.VKApiLongpollUpdates import dev.ragnarok.fenrir.kJson import dev.ragnarok.fenrir.util.Logger import dev.ragnarok.fenrir.util.serializeble.json.JsonElement +import dev.ragnarok.fenrir.util.serializeble.json.jsonArray +import dev.ragnarok.fenrir.util.serializeble.json.jsonObject -class LongpollUpdatesAdapter : AbsAdapter("VkApiLongpollUpdates") { +class LongpollUpdatesDtoAdapter : AbsDtoAdapter("VkApiLongpollUpdates") { @Throws(Exception::class) override fun deserialize( json: JsonElement @@ -15,13 +17,13 @@ class LongpollUpdatesAdapter : AbsAdapter("VkApiLongpollUp if (!checkObject(json)) { throw Exception("$TAG error parse object") } - val root = json.asJsonObject + val root = json.jsonObject updates.failed = optInt(root, "failed") updates.ts = optLong(root, "ts") val array = root["updates"] if (checkArray(array)) { - for (i in 0 until array.asJsonArray.size) { - val updateArray = array.asJsonArraySafe?.get(i)?.asJsonArraySafe + for (i in 0 until array.jsonArray.size) { + val updateArray = array.jsonArray[i].asJsonArraySafe val event: AbsLongpollEvent? = updateArray?.let { kJson.decodeFromJsonElement( @@ -40,6 +42,6 @@ class LongpollUpdatesAdapter : AbsAdapter("VkApiLongpollUp } companion object { - private val TAG = LongpollUpdatesAdapter::class.java.simpleName + private val TAG = LongpollUpdatesDtoAdapter::class.java.simpleName } } \ No newline at end of file diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/LyricsAdapter.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/LyricsDtoAdapter.kt similarity index 78% rename from app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/LyricsAdapter.kt rename to app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/LyricsDtoAdapter.kt index 1381d1300..a9b75d3dc 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/LyricsAdapter.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/LyricsDtoAdapter.kt @@ -4,8 +4,10 @@ import dev.ragnarok.fenrir.api.model.VKApiLyrics import dev.ragnarok.fenrir.nonNullNoEmpty import dev.ragnarok.fenrir.util.AppTextUtils import dev.ragnarok.fenrir.util.serializeble.json.JsonElement +import dev.ragnarok.fenrir.util.serializeble.json.jsonArray +import dev.ragnarok.fenrir.util.serializeble.json.jsonObject -class LyricsAdapter : AbsAdapter("VKApiLyrics") { +class LyricsDtoAdapter : AbsDtoAdapter("VKApiLyrics") { @Throws(Exception::class) override fun deserialize( json: JsonElement @@ -14,17 +16,17 @@ class LyricsAdapter : AbsAdapter("VKApiLyrics") { throw Exception("$TAG error parse object") } val response = VKApiLyrics() - val root = json.asJsonObject + val root = json.jsonObject if (hasObject(root, "lyrics")) { - root["lyrics"]?.asJsonObject?.let { + root["lyrics"]?.jsonObject?.let { val str = StringBuilder() if (hasArray(it, "text")) { - for (s in it["text"]?.asJsonArray.orEmpty()) { + for (s in it["text"]?.jsonArray.orEmpty()) { str.append(s.asPrimitiveSafe?.content.orEmpty()).append("\r\n") } } if (hasArray(it, "timestamps")) { - for (s in it["timestamps"]?.asJsonArray.orEmpty()) { + for (s in it["timestamps"]?.jsonArray.orEmpty()) { if (!checkObject(s)) { continue } @@ -52,6 +54,6 @@ class LyricsAdapter : AbsAdapter("VKApiLyrics") { } companion object { - private val TAG = LyricsAdapter::class.java.simpleName + private val TAG = LyricsDtoAdapter::class.java.simpleName } } \ No newline at end of file diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/MarketDtoAdapter.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/MarketDtoAdapter.kt index 103c1eb02..62f1cfbc4 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/MarketDtoAdapter.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/MarketDtoAdapter.kt @@ -4,8 +4,10 @@ import dev.ragnarok.fenrir.api.model.VKApiMarket import dev.ragnarok.fenrir.api.model.VKApiPhoto import dev.ragnarok.fenrir.kJson import dev.ragnarok.fenrir.util.serializeble.json.JsonElement +import dev.ragnarok.fenrir.util.serializeble.json.jsonArray +import dev.ragnarok.fenrir.util.serializeble.json.jsonObject -class MarketDtoAdapter : AbsAdapter("VKApiMarket") { +class MarketDtoAdapter : AbsDtoAdapter("VKApiMarket") { @Throws(Exception::class) override fun deserialize( json: JsonElement @@ -14,7 +16,7 @@ class MarketDtoAdapter : AbsAdapter("VKApiMarket") { throw Exception("$TAG error parse object") } val dto = VKApiMarket() - val root = json.asJsonObject + val root = json.jsonObject dto.id = optInt(root, "id") dto.owner_id = optLong(root, "owner_id") dto.weight = optInt(root, "weight") @@ -27,19 +29,19 @@ class MarketDtoAdapter : AbsAdapter("VKApiMarket") { dto.access_key = optString(root, "access_key") dto.is_favorite = optBoolean(root, "is_favorite") if (hasObject(root, "dimensions")) { - val dimensions = root["dimensions"]?.asJsonObject + val dimensions = root["dimensions"]?.jsonObject dto.dimensions = optInt(dimensions, "length").toString() + "x" + optInt( dimensions, "width" ) + "x" + optInt(dimensions, "height") + " mm" } if (hasObject(root, "price")) { - val price = root["price"]?.asJsonObject + val price = root["price"]?.jsonObject dto.price = optString(price, "text") } if (hasArray(root, "photos")) { dto.photos = ArrayList() - val temp = root.getAsJsonArray("photos") + val temp = root["photos"]?.jsonArray for (i in temp.orEmpty()) { if (!checkObject(i)) { continue diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/MessageDtoAdapter.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/MessageDtoAdapter.kt index a72c81717..f747fe883 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/MessageDtoAdapter.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/MessageDtoAdapter.kt @@ -6,8 +6,10 @@ import dev.ragnarok.fenrir.api.util.VKStringUtils import dev.ragnarok.fenrir.kJson import dev.ragnarok.fenrir.orZero import dev.ragnarok.fenrir.util.serializeble.json.JsonElement +import dev.ragnarok.fenrir.util.serializeble.json.jsonArray +import dev.ragnarok.fenrir.util.serializeble.json.jsonObject -class MessageDtoAdapter : AbsAdapter("VKApiMessage") { +class MessageDtoAdapter : AbsDtoAdapter("VKApiMessage") { @Throws(Exception::class) override fun deserialize( json: JsonElement @@ -16,7 +18,7 @@ class MessageDtoAdapter : AbsAdapter("VKApiMessage") { throw Exception("$TAG error parse object") } val dto = VKApiMessage() - val root = json.asJsonObject + val root = json.jsonObject dto.id = optInt(root, "id") dto.out = optBoolean(root, "out") dto.peer_id = optLong(root, "peer_id") @@ -43,7 +45,7 @@ class MessageDtoAdapter : AbsAdapter("VKApiMessage") { } } if (hasArray(root, "fwd_messages")) { - val fwdArray = root.getAsJsonArray("fwd_messages") + val fwdArray = root["fwd_messages"]?.jsonArray dto.fwd_messages = ArrayList(fwdArray?.size.orZero()) for (i in 0 until fwdArray?.size.orZero()) { if (!checkObject(fwdArray?.get(i))) { @@ -80,12 +82,12 @@ class MessageDtoAdapter : AbsAdapter("VKApiMessage") { dto.conversation_message_id = optInt(root, "conversation_message_id") val actionJson = root["action"] if (checkObject(actionJson)) { - dto.action = optString(actionJson.asJsonObject, "type") - dto.action_mid = optLong(actionJson.asJsonObject, "member_id") - dto.action_text = optString(actionJson.asJsonObject, "text") - dto.action_email = optString(actionJson.asJsonObject, "email") - if (hasObject(actionJson.asJsonObject, "photo")) { - val photoJson = actionJson.asJsonObject.getAsJsonObject("photo") + dto.action = optString(actionJson.jsonObject, "type") + dto.action_mid = optLong(actionJson.jsonObject, "member_id") + dto.action_text = optString(actionJson.jsonObject, "text") + dto.action_email = optString(actionJson.jsonObject, "email") + if (hasObject(actionJson.jsonObject, "photo")) { + val photoJson = actionJson.jsonObject["photo"]?.jsonObject dto.action_photo_50 = optString(photoJson, "photo_50") dto.action_photo_100 = optString(photoJson, "photo_100") dto.action_photo_200 = optString(photoJson, "photo_200") diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/NarrativesDtoAdapter.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/NarrativesDtoAdapter.kt index c96a36e41..2849246f2 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/NarrativesDtoAdapter.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/NarrativesDtoAdapter.kt @@ -4,9 +4,10 @@ import dev.ragnarok.fenrir.api.model.VKApiNarratives import dev.ragnarok.fenrir.orZero import dev.ragnarok.fenrir.util.serializeble.json.JsonElement import dev.ragnarok.fenrir.util.serializeble.json.int -import dev.ragnarok.fenrir.util.serializeble.json.jsonPrimitive +import dev.ragnarok.fenrir.util.serializeble.json.jsonArray +import dev.ragnarok.fenrir.util.serializeble.json.jsonObject -class NarrativesDtoAdapter : AbsAdapter("VKApiNarratives") { +class NarrativesDtoAdapter : AbsDtoAdapter("VKApiNarratives") { @Throws(Exception::class) override fun deserialize( json: JsonElement @@ -15,29 +16,29 @@ class NarrativesDtoAdapter : AbsAdapter("VKApiNarratives") { throw Exception("$TAG error parse object") } val dto = VKApiNarratives() - val root = json.asJsonObject + val root = json.jsonObject dto.id = optInt(root, "id") dto.owner_id = optLong(root, "owner_id") dto.title = optString(root, "title") if (hasArray(root, "story_ids")) { - val temp = root.getAsJsonArray("story_ids") + val temp = root["story_ids"]?.jsonArray dto.story_ids = IntArray(temp?.size.orZero()) { optInt(temp, it, 0) } } - if (hasObject(root, "cover") && hasArray(root.getAsJsonObject("cover"), "cropped_sizes")) { - val images = root.getAsJsonObject("cover").getAsJsonArray("cropped_sizes") + if (hasObject(root, "cover") && hasArray(root["cover"]?.jsonObject, "cropped_sizes")) { + val images = root["cover"]?.jsonObject?.get("cropped_sizes")?.jsonArray for (i in 0 until images?.size.orZero()) { if (!checkObject(images?.get(i))) { continue } - if (images?.get(i)?.asJsonObject?.get("width")?.jsonPrimitive?.int.orZero() >= 400) { - dto.cover = images?.get(i)?.asJsonObject?.get("url")?.jsonPrimitive?.content + if (images?.get(i)?.jsonObject?.get("width")?.asPrimitiveSafe?.int.orZero() >= 400) { + dto.cover = images?.get(i)?.jsonObject?.get("url")?.asPrimitiveSafe?.content break } } if (dto.cover == null) { if (checkObject(images?.get(images.size - 1))) { dto.cover = - images?.get(images.size - 1)?.asJsonObject?.get("url")?.jsonPrimitive?.content + images?.get(images.size - 1)?.jsonObject?.get("url")?.asPrimitiveSafe?.content } } } diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/NewsAdapter.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/NewsDtoAdapter.kt similarity index 71% rename from app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/NewsAdapter.kt rename to app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/NewsDtoAdapter.kt index 9bf05cccd..de7de1a4a 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/NewsAdapter.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/NewsDtoAdapter.kt @@ -5,10 +5,11 @@ import dev.ragnarok.fenrir.kJson import dev.ragnarok.fenrir.nonNullNoEmpty import dev.ragnarok.fenrir.orZero import dev.ragnarok.fenrir.util.serializeble.json.JsonElement -import dev.ragnarok.fenrir.util.serializeble.json.jsonPrimitive +import dev.ragnarok.fenrir.util.serializeble.json.jsonArray +import dev.ragnarok.fenrir.util.serializeble.json.jsonObject import dev.ragnarok.fenrir.util.serializeble.json.long -class NewsAdapter : AbsAdapter("VKApiNews") { +class NewsDtoAdapter : AbsDtoAdapter("VKApiNews") { @Throws(Exception::class) override fun deserialize( json: JsonElement @@ -17,7 +18,7 @@ class NewsAdapter : AbsAdapter("VKApiNews") { throw Exception("$TAG error parse object") } val dto = VKApiNews() - val root = json.asJsonObject + val root = json.jsonObject dto.type = optString(root, "type") dto.source_id = optLong(root, "source_id") dto.date = optLong(root, "date") @@ -29,7 +30,7 @@ class NewsAdapter : AbsAdapter("VKApiNews") { dto.mark_as_ads = optInt(root, "mark_as_ads") if (hasArray(root, "copy_history")) { dto.copy_history = parseArray( - root.getAsJsonArray("copy_history"), + root["copy_history"]?.jsonArray, emptyList(), VKApiPost.serializer() ) @@ -39,7 +40,7 @@ class NewsAdapter : AbsAdapter("VKApiNews") { dto.copy_post_date = optLong(root, "copy_post_date") dto.text = optString(root, "text") if (hasObject(root, "copyright")) { - val cop = root.getAsJsonObject("copyright") + val cop = root["copyright"]?.jsonObject val name = optString(cop, "name") val link = optString(cop, "link") name.nonNullNoEmpty { @@ -49,30 +50,30 @@ class NewsAdapter : AbsAdapter("VKApiNews") { dto.can_edit = optBoolean(root, "can_edit") dto.can_delete = optBoolean(root, "can_delete") if (hasObject(root, "comments")) { - val commentsRoot = root.getAsJsonObject("comments") + val commentsRoot = root["comments"]?.jsonObject dto.comment_count = optInt(commentsRoot, "count") dto.comment_can_post = optBoolean(commentsRoot, "can_post") } if (hasObject(root, "likes")) { - val likesRoot = root.getAsJsonObject("likes") + val likesRoot = root["likes"]?.jsonObject dto.like_count = optInt(likesRoot, "count") dto.user_like = optBoolean(likesRoot, "user_likes") dto.can_like = optBoolean(likesRoot, "can_like") dto.can_publish = optBoolean(likesRoot, "can_publish") } if (hasObject(root, "donut")) { - val donut = root.getAsJsonObject("donut") + val donut = root["donut"]?.jsonObject dto.is_donut = optBoolean(donut, "is_donut") } else { dto.is_donut = false } if (hasObject(root, "reposts")) { - val repostsRoot = root.getAsJsonObject("reposts") + val repostsRoot = root["reposts"]?.jsonObject dto.reposts_count = optInt(repostsRoot, "count") dto.user_reposted = optBoolean(repostsRoot, "user_reposted") } if (hasObject(root, "views")) { - val viewRoot = root.getAsJsonObject("views") + val viewRoot = root["views"]?.jsonObject dto.views = optInt(viewRoot, "count", 0) } if (hasArray(root, "attachments")) { @@ -81,8 +82,8 @@ class NewsAdapter : AbsAdapter("VKApiNews") { kJson.decodeFromJsonElement(VKApiAttachments.serializer(), it) } } - if (root.has("photos")) { - val photosArray = root.getAsJsonObject("photos").getAsJsonArray("items") + if (hasObject(root, "photos") && hasArray(root["photos"]?.jsonObject, "items")) { + val photosArray = root["photos"]?.jsonObject?.get("items")?.jsonArray if (dto.attachments == null) { dto.attachments = VKApiAttachments() } @@ -90,8 +91,8 @@ class NewsAdapter : AbsAdapter("VKApiNews") { dto.attachments?.append(it) } } - if (root.has("photo_tags")) { - val photosTagsArray = root.getAsJsonObject("photo_tags").getAsJsonArray("items") + if (hasObject(root, "photo_tags") && hasArray(root["photo_tags"]?.jsonObject, "items")) { + val photosTagsArray = root["photo_tags"]?.jsonObject?.get("items")?.jsonArray if (dto.attachments == null) { dto.attachments = VKApiAttachments() } @@ -102,8 +103,8 @@ class NewsAdapter : AbsAdapter("VKApiNews") { dto.attachments?.append(it) } } - if (root.has("audio")) { - val audiosArray = root.getAsJsonObject("audio").getAsJsonArray("items") + if (hasObject(root, "audio") && hasArray(root["audio"]?.jsonObject, "items")) { + val audiosArray = root["audio"]?.jsonObject?.get("items")?.jsonArray if (dto.attachments == null) { dto.attachments = VKApiAttachments() } @@ -115,8 +116,8 @@ class NewsAdapter : AbsAdapter("VKApiNews") { dto.attachments?.append(it) } } - if (root.has("video")) { - val videoArray = root.getAsJsonObject("video").getAsJsonArray("items") + if (hasObject(root, "video") && hasArray(root["video"]?.jsonObject, "items")) { + val videoArray = root["video"]?.jsonObject?.get("items")?.jsonArray if (dto.attachments == null) { dto.attachments = VKApiAttachments() } @@ -127,18 +128,18 @@ class NewsAdapter : AbsAdapter("VKApiNews") { dto.attachments?.append(it) } } - if (root.has("friends")) { - val friendsArray = root.getAsJsonObject("friends").getAsJsonArray("items") + if (hasObject(root, "friends") && hasArray(root["friends"]?.jsonObject, "items")) { + val friendsArray = root["friends"]?.jsonObject?.get("items")?.jsonArray dto.friends = ArrayList(friendsArray?.size.orZero()) for (i in 0 until friendsArray?.size.orZero()) { - val friendObj = friendsArray?.get(i)?.asJsonObject - friendObj?.get("user_id")?.jsonPrimitive?.let { dto.friends?.add(it.long) } + val friendObj = friendsArray?.get(i)?.jsonObject + friendObj?.get("user_id")?.asPrimitiveSafe?.let { dto.friends?.add(it.long) } } } return dto } companion object { - private val TAG = NewsAdapter::class.java.simpleName + private val TAG = NewsDtoAdapter::class.java.simpleName } } \ No newline at end of file diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/NewsfeedCommentDtoAdapter.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/NewsfeedCommentDtoAdapter.kt index 63fa906ab..56475564e 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/NewsfeedCommentDtoAdapter.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/NewsfeedCommentDtoAdapter.kt @@ -4,8 +4,9 @@ import dev.ragnarok.fenrir.api.model.* import dev.ragnarok.fenrir.api.model.response.NewsfeedCommentsResponse.* import dev.ragnarok.fenrir.kJson import dev.ragnarok.fenrir.util.serializeble.json.JsonElement +import dev.ragnarok.fenrir.util.serializeble.json.jsonObject -class NewsfeedCommentDtoAdapter : AbsAdapter("Dto") { +class NewsfeedCommentDtoAdapter : AbsDtoAdapter("Dto") { @Throws(Exception::class) override fun deserialize( json: JsonElement @@ -13,7 +14,7 @@ class NewsfeedCommentDtoAdapter : AbsAdapter("Dto") { if (!checkObject(json)) { throw UnsupportedOperationException() } - val root = json.asJsonObject + val root = json.jsonObject when (optString(root, "type", "post")) { "photo" -> { return PhotoDto(kJson.decodeFromJsonElement(VKApiPhoto.serializer(), root)) diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/PhotoAlbumDtoAdapter.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/PhotoAlbumDtoAdapter.kt index 46917d6ae..43d5cc56f 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/PhotoAlbumDtoAdapter.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/PhotoAlbumDtoAdapter.kt @@ -6,8 +6,10 @@ import dev.ragnarok.fenrir.api.model.VKApiPrivacy import dev.ragnarok.fenrir.kJson import dev.ragnarok.fenrir.orZero import dev.ragnarok.fenrir.util.serializeble.json.JsonElement +import dev.ragnarok.fenrir.util.serializeble.json.jsonArray +import dev.ragnarok.fenrir.util.serializeble.json.jsonObject -class PhotoAlbumDtoAdapter : AbsAdapter("VKApiPhotoAlbum") { +class PhotoAlbumDtoAdapter : AbsDtoAdapter("VKApiPhotoAlbum") { @Throws(Exception::class) override fun deserialize( json: JsonElement @@ -16,7 +18,7 @@ class PhotoAlbumDtoAdapter : AbsAdapter("VKApiPhotoAlbum") { throw Exception("$TAG error parse object") } val album = VKApiPhotoAlbum() - val root = json.asJsonObject + val root = json.jsonObject album.id = optInt(root, "id") album.thumb_id = optInt(root, "thumb_id") album.owner_id = optLong(root, "owner_id") @@ -40,7 +42,7 @@ class PhotoAlbumDtoAdapter : AbsAdapter("VKApiPhotoAlbum") { } } if (hasArray(root, "sizes")) { - val sizesArray = root.getAsJsonArray("sizes") + val sizesArray = root["sizes"]?.jsonArray album.photo = ArrayList(sizesArray?.size.orZero()) for (i in 0 until sizesArray?.size.orZero()) { album.photo?.add( @@ -51,9 +53,9 @@ class PhotoAlbumDtoAdapter : AbsAdapter("VKApiPhotoAlbum") { ) } } else if (hasObject(root, "thumb")) { - val thumb = root.getAsJsonObject("thumb") + val thumb = root["thumb"]?.jsonObject if (hasArray(thumb, "sizes")) { - val sizesArray = thumb.getAsJsonArray("sizes") + val sizesArray = thumb["sizes"]?.jsonArray album.photo = ArrayList(sizesArray?.size.orZero()) for (i in 0 until sizesArray?.size.orZero()) { album.photo?.add( diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/PhotoDtoAdapter.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/PhotoDtoAdapter.kt index 12b9b6050..725414a97 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/PhotoDtoAdapter.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/PhotoDtoAdapter.kt @@ -6,8 +6,10 @@ import dev.ragnarok.fenrir.api.model.VKApiPhoto import dev.ragnarok.fenrir.kJson import dev.ragnarok.fenrir.orZero import dev.ragnarok.fenrir.util.serializeble.json.JsonElement +import dev.ragnarok.fenrir.util.serializeble.json.jsonArray +import dev.ragnarok.fenrir.util.serializeble.json.jsonObject -class PhotoDtoAdapter : AbsAdapter("VKApiPhoto") { +class PhotoDtoAdapter : AbsDtoAdapter("VKApiPhoto") { @Throws(Exception::class) override fun deserialize( json: JsonElement @@ -16,7 +18,7 @@ class PhotoDtoAdapter : AbsAdapter("VKApiPhoto") { throw Exception("$TAG error parse object") } val photo = VKApiPhoto() - val root = json.asJsonObject + val root = json.jsonObject photo.id = optInt(root, "id") photo.album_id = optInt(root, "album_id") photo.date = optLong(root, "date") @@ -26,7 +28,7 @@ class PhotoDtoAdapter : AbsAdapter("VKApiPhoto") { photo.text = optString(root, "text") photo.access_key = optString(root, "access_key") if (hasObject(root, "likes")) { - val likesRoot = root["likes"]?.asJsonObject + val likesRoot = root["likes"]?.jsonObject photo.likes = optInt(likesRoot, "count") photo.user_likes = optBoolean(likesRoot, "user_likes") } @@ -36,17 +38,17 @@ class PhotoDtoAdapter : AbsAdapter("VKApiPhoto") { } } if (hasObject(root, "tags")) { - val tagsRoot = root["tags"]?.asJsonObject + val tagsRoot = root["tags"]?.jsonObject photo.tags = optInt(tagsRoot, "count") } if (hasObject(root, "reposts")) { - val repostsRoot = root["reposts"]?.asJsonObject + val repostsRoot = root["reposts"]?.jsonObject photo.reposts = optInt(repostsRoot, "count") } photo.can_comment = optBoolean(root, "can_comment") photo.post_id = optInt(root, "post_id") if (hasArray(root, "sizes")) { - val sizesArray = root.getAsJsonArray("sizes") + val sizesArray = root["sizes"]?.jsonArray photo.sizes = ArrayList(sizesArray?.size.orZero()) for (i in 0 until sizesArray?.size.orZero()) { if (!checkObject(sizesArray?.get(i))) { diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/PostDtoAdapter.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/PostDtoAdapter.kt index cf2ef17bb..c9d38a777 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/PostDtoAdapter.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/PostDtoAdapter.kt @@ -6,8 +6,10 @@ import dev.ragnarok.fenrir.kJson import dev.ragnarok.fenrir.nonNullNoEmpty import dev.ragnarok.fenrir.orZero import dev.ragnarok.fenrir.util.serializeble.json.JsonElement +import dev.ragnarok.fenrir.util.serializeble.json.jsonArray +import dev.ragnarok.fenrir.util.serializeble.json.jsonObject -class PostDtoAdapter : AbsAdapter("VKApiPost") { +class PostDtoAdapter : AbsDtoAdapter("VKApiPost") { @Throws(Exception::class) override fun deserialize( json: JsonElement @@ -16,11 +18,11 @@ class PostDtoAdapter : AbsAdapter("VKApiPost") { throw Exception("$TAG error parse object") } val dto = VKApiPost() - val root = json.asJsonObject + val root = json.jsonObject dto.id = getFirstInt(root, 0, "post_id", "id") dto.post_type = VKApiPost.Type.parse(optString(root, "post_type")) if (hasObject(root, "donut")) { - val donut = root.getAsJsonObject("donut") + val donut = root["donut"]?.jsonObject if (optBoolean(donut, "is_donut")) { dto.post_type = DONUT } @@ -41,7 +43,7 @@ class PostDtoAdapter : AbsAdapter("VKApiPost") { dto.date = optLong(root, "date") dto.text = optString(root, "text") if (hasObject(root, "copyright")) { - val cop = root.getAsJsonObject("copyright") + val cop = root["copyright"]?.jsonObject val name = optString(cop, "name") val link = optString(cop, "link") name.nonNullNoEmpty { @@ -67,19 +69,19 @@ class PostDtoAdapter : AbsAdapter("VKApiPost") { } } if (hasObject(root, "likes")) { - val likes = root.getAsJsonObject("likes") + val likes = root["likes"]?.jsonObject dto.likes_count = optInt(likes, "count") dto.user_likes = optBoolean(likes, "user_likes") dto.can_like = optBoolean(likes, "can_like") dto.can_publish = optBoolean(likes, "can_publish") } if (hasObject(root, "reposts")) { - val reposts = root.getAsJsonObject("reposts") + val reposts = root["reposts"]?.jsonObject dto.reposts_count = optInt(reposts, "count") dto.user_reposted = optBoolean(reposts, "user_reposted") } if (hasObject(root, "views")) { - val views = root.getAsJsonObject("views") + val views = root["views"]?.jsonObject dto.views = optInt(views, "count") } if (hasArray(root, "attachments")) { @@ -89,7 +91,7 @@ class PostDtoAdapter : AbsAdapter("VKApiPost") { } } if (hasObject(root, "donut")) { - val donut = root.getAsJsonObject("donut") + val donut = root["donut"]?.jsonObject dto.is_donut = optBoolean(donut, "is_donut") } else { dto.is_donut = false @@ -101,13 +103,13 @@ class PostDtoAdapter : AbsAdapter("VKApiPost") { dto.can_pin = optInt(root, "can_pin") == 1 dto.is_pinned = optBoolean(root, "is_pinned") if (hasArray(root, "copy_history")) { - val copyHistoryArray = root.getAsJsonArray("copy_history") + val copyHistoryArray = root["copy_history"]?.jsonArray dto.copy_history = ArrayList(copyHistoryArray?.size.orZero()) for (i in 0 until copyHistoryArray?.size.orZero()) { if (!checkObject(copyHistoryArray?.get(i))) { continue } - val copy = copyHistoryArray?.get(i)?.asJsonObject + val copy = copyHistoryArray?.get(i)?.jsonObject dto.copy_history?.add(deserialize(copy ?: continue)) } } else { diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/PostSourceDtoAdapter.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/PostSourceDtoAdapter.kt index 5e8311f25..80556d94d 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/PostSourceDtoAdapter.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/PostSourceDtoAdapter.kt @@ -2,13 +2,14 @@ package dev.ragnarok.fenrir.api.adapters import dev.ragnarok.fenrir.api.model.VKApiPostSource import dev.ragnarok.fenrir.util.serializeble.json.JsonElement +import dev.ragnarok.fenrir.util.serializeble.json.jsonObject -class PostSourceDtoAdapter : AbsAdapter("VKApiPostSource") { +class PostSourceDtoAdapter : AbsDtoAdapter("VKApiPostSource") { @Throws(Exception::class) override fun deserialize( json: JsonElement ): VKApiPostSource { - val root = json.asJsonObject + val root = json.jsonObject val dto = VKApiPostSource() dto.type = VKApiPostSource.Type.parse(optString(root, "type")) dto.platform = optString(root, "platform") diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/PrivacyDtoAdapter.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/PrivacyDtoAdapter.kt index 6fc3b6296..e377d364f 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/PrivacyDtoAdapter.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/PrivacyDtoAdapter.kt @@ -2,8 +2,10 @@ package dev.ragnarok.fenrir.api.adapters import dev.ragnarok.fenrir.api.model.VKApiPrivacy import dev.ragnarok.fenrir.util.serializeble.json.JsonElement +import dev.ragnarok.fenrir.util.serializeble.json.jsonArray +import dev.ragnarok.fenrir.util.serializeble.json.jsonObject -class PrivacyDtoAdapter : AbsAdapter("VKApiPrivacy") { +class PrivacyDtoAdapter : AbsDtoAdapter("VKApiPrivacy") { @Throws(Exception::class) override fun deserialize( json: JsonElement @@ -11,7 +13,7 @@ class PrivacyDtoAdapter : AbsAdapter("VKApiPrivacy") { if (!checkObject(json)) { return VKApiPrivacy("null") } - val root = json.asJsonObject + val root = json.jsonObject // Examples // {"category":"only_me"} @@ -19,31 +21,31 @@ class PrivacyDtoAdapter : AbsAdapter("VKApiPrivacy") { val privacy = VKApiPrivacy(optString(root, "category", "only_me")) val owners = root["owners"] if (checkObject(owners)) { - val allowed = owners.asJsonObject["allowed"] + val allowed = owners.jsonObject["allowed"] if (checkArray(allowed)) { - for (i in 0 until allowed.asJsonArray.size) { - privacy.includeOwner(optLong(allowed.asJsonArray, i)) + for (i in 0 until allowed.jsonArray.size) { + privacy.includeOwner(optLong(allowed.jsonArray, i)) } } - val excluded = owners.asJsonObject["excluded"] + val excluded = owners.jsonObject["excluded"] if (checkArray(excluded)) { - for (i in 0 until excluded.asJsonArray.size) { - privacy.excludeOwner(optLong(excluded.asJsonArray, i)) + for (i in 0 until excluded.jsonArray.size) { + privacy.excludeOwner(optLong(excluded.jsonArray, i)) } } } val lists = root["lists"] if (checkObject(lists)) { - val allowed = lists.asJsonObject["allowed"] + val allowed = lists.jsonObject["allowed"] if (checkArray(allowed)) { - for (i in 0 until allowed.asJsonArray.size) { - privacy.includeFriendsList(optLong(allowed.asJsonArray, i)) + for (i in 0 until allowed.jsonArray.size) { + privacy.includeFriendsList(optLong(allowed.jsonArray, i)) } } - val excluded = lists.asJsonObject["excluded"] + val excluded = lists.jsonObject["excluded"] if (checkArray(excluded)) { - for (i in 0 until excluded.asJsonArray.size) { - privacy.excludeFriendsList(optLong(excluded.asJsonArray, i)) + for (i in 0 until excluded.jsonArray.size) { + privacy.excludeFriendsList(optLong(excluded.jsonArray, i)) } } } diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/ProfileInfoResponseDtoAdapter.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/ProfileInfoResponseDtoAdapter.kt index 20beb3131..6c5fcca8d 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/ProfileInfoResponseDtoAdapter.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/ProfileInfoResponseDtoAdapter.kt @@ -2,9 +2,10 @@ package dev.ragnarok.fenrir.api.adapters import dev.ragnarok.fenrir.api.model.VKApiProfileInfoResponse import dev.ragnarok.fenrir.util.serializeble.json.JsonElement +import dev.ragnarok.fenrir.util.serializeble.json.jsonObject class ProfileInfoResponseDtoAdapter : - AbsAdapter("VKApiProfileInfoResponse") { + AbsDtoAdapter("VKApiProfileInfoResponse") { @Throws(Exception::class) override fun deserialize( json: JsonElement @@ -13,7 +14,7 @@ class ProfileInfoResponseDtoAdapter : throw Exception("$TAG error parse object") } val info = VKApiProfileInfoResponse() - val root = json.asJsonObject + val root = json.jsonObject if (root.has("name_request")) { info.status = 2 } else { diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/SchoolClazzDtoAdapter.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/SchoolClazzDtoAdapter.kt index 1418e9000..1ab9fb8f4 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/SchoolClazzDtoAdapter.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/SchoolClazzDtoAdapter.kt @@ -2,11 +2,11 @@ package dev.ragnarok.fenrir.api.adapters import dev.ragnarok.fenrir.api.model.database.SchoolClazzDto import dev.ragnarok.fenrir.util.serializeble.json.JsonElement -import dev.ragnarok.fenrir.util.serializeble.json.JsonPrimitive import dev.ragnarok.fenrir.util.serializeble.json.intOrNull +import dev.ragnarok.fenrir.util.serializeble.json.jsonArray import dev.ragnarok.fenrir.util.serializeble.json.jsonPrimitive -class SchoolClazzDtoAdapter : AbsAdapter("SchoolClazzDto") { +class SchoolClazzDtoAdapter : AbsDtoAdapter("SchoolClazzDto") { @Throws(Exception::class) override fun deserialize( json: JsonElement @@ -15,9 +15,9 @@ class SchoolClazzDtoAdapter : AbsAdapter("SchoolClazzDto") { throw Exception("$TAG error parse object") } val dto = SchoolClazzDto() - val root = json.asJsonArray + val root = json.jsonArray dto.id = optInt(root, 0) - if (root[1] is JsonPrimitive) { + if (checkPrimitive(root[1])) { val second = root[1].jsonPrimitive if (second.isString) { dto.title = second.content diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/ServicePlaylistResponseDtoAdapter.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/ServicePlaylistResponseDtoAdapter.kt index 2339d21e5..3cb961e9b 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/ServicePlaylistResponseDtoAdapter.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/ServicePlaylistResponseDtoAdapter.kt @@ -4,9 +4,11 @@ import dev.ragnarok.fenrir.api.model.VKApiAudioPlaylist import dev.ragnarok.fenrir.api.model.response.ServicePlaylistResponse import dev.ragnarok.fenrir.kJson import dev.ragnarok.fenrir.util.serializeble.json.JsonElement +import dev.ragnarok.fenrir.util.serializeble.json.jsonArray +import dev.ragnarok.fenrir.util.serializeble.json.jsonObject class ServicePlaylistResponseDtoAdapter : - AbsAdapter("ServicePlaylistResponse") { + AbsDtoAdapter("ServicePlaylistResponse") { @Throws(Exception::class) override fun deserialize( json: JsonElement @@ -14,11 +16,11 @@ class ServicePlaylistResponseDtoAdapter : if (!checkObject(json)) { throw Exception("$TAG error parse object") } - val root = json.asJsonObject + val root = json.jsonObject val dto = ServicePlaylistResponse() dto.playlists = ArrayList() if (checkArray(root["response"])) { - val response = root.getAsJsonArray("response") + val response = root["response"]?.jsonArray for (i in response.orEmpty()) { if (checkObject(i)) { dto.playlists?.add( diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/StoryDtoAdapter.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/StoryDtoAdapter.kt index e7919c990..fc77e02e8 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/StoryDtoAdapter.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/StoryDtoAdapter.kt @@ -5,8 +5,9 @@ import dev.ragnarok.fenrir.api.model.VKApiStory import dev.ragnarok.fenrir.api.model.VKApiVideo import dev.ragnarok.fenrir.kJson import dev.ragnarok.fenrir.util.serializeble.json.JsonElement +import dev.ragnarok.fenrir.util.serializeble.json.jsonObject -class StoryDtoAdapter : AbsAdapter("VKApiStory") { +class StoryDtoAdapter : AbsDtoAdapter("VKApiStory") { @Throws(Exception::class) override fun deserialize( json: JsonElement @@ -15,7 +16,7 @@ class StoryDtoAdapter : AbsAdapter("VKApiStory") { throw Exception("$TAG error parse object") } val story = VKApiStory() - val root = json.asJsonObject + val root = json.jsonObject story.id = optInt(root, "id") story.owner_id = optLong(root, "owner_id") story.date = optLong(root, "owner_id") @@ -39,7 +40,7 @@ class StoryDtoAdapter : AbsAdapter("VKApiStory") { } } if (hasObject(root, "link")) { - story.target_url = optString(root.getAsJsonObject("link"), "url") + story.target_url = optString(root["link"]?.jsonObject, "url") } return story } diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/TopicDtoAdapter.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/TopicDtoAdapter.kt index 9d9c8a7ba..ae9b13270 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/TopicDtoAdapter.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/TopicDtoAdapter.kt @@ -4,11 +4,10 @@ import dev.ragnarok.fenrir.api.model.CommentsDto import dev.ragnarok.fenrir.api.model.VKApiTopic import dev.ragnarok.fenrir.kJson import dev.ragnarok.fenrir.util.serializeble.json.JsonElement -import dev.ragnarok.fenrir.util.serializeble.json.JsonObject import dev.ragnarok.fenrir.util.serializeble.json.intOrNull -import dev.ragnarok.fenrir.util.serializeble.json.jsonPrimitive +import dev.ragnarok.fenrir.util.serializeble.json.jsonObject -class TopicDtoAdapter : AbsAdapter("VKApiTopic") { +class TopicDtoAdapter : AbsDtoAdapter("VKApiTopic") { @Throws(Exception::class) override fun deserialize( json: JsonElement @@ -17,7 +16,7 @@ class TopicDtoAdapter : AbsAdapter("VKApiTopic") { throw Exception("$TAG error parse object") } val dto = VKApiTopic() - val root = json.asJsonObject + val root = json.jsonObject dto.id = optInt(root, "id") dto.owner_id = optLong(root, "owner_id") dto.title = optString(root, "title") @@ -29,7 +28,7 @@ class TopicDtoAdapter : AbsAdapter("VKApiTopic") { dto.is_fixed = optBoolean(root, "is_fixed") if (root.has("comments")) { val commentsJson = root["comments"] - if (commentsJson is JsonObject) { + if (checkObject(commentsJson)) { dto.comments = kJson.decodeFromJsonElement( CommentsDto.serializer(), @@ -37,7 +36,7 @@ class TopicDtoAdapter : AbsAdapter("VKApiTopic") { ) } else { dto.comments = CommentsDto() - dto.comments?.count = commentsJson?.jsonPrimitive?.intOrNull ?: 0 + dto.comments?.count = commentsJson?.asPrimitiveSafe?.intOrNull ?: 0 } } dto.first_comment = optString(root, "first_comment") diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/UserDtoAdapter.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/UserDtoAdapter.kt index a86f97c99..674e1e85b 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/UserDtoAdapter.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/UserDtoAdapter.kt @@ -6,8 +6,10 @@ import dev.ragnarok.fenrir.api.util.VKStringUtils import dev.ragnarok.fenrir.kJson import dev.ragnarok.fenrir.orZero import dev.ragnarok.fenrir.util.serializeble.json.JsonElement +import dev.ragnarok.fenrir.util.serializeble.json.jsonArray +import dev.ragnarok.fenrir.util.serializeble.json.jsonObject -class UserDtoAdapter : AbsAdapter("VKApiUser") { +class UserDtoAdapter : AbsDtoAdapter("VKApiUser") { @Throws(Exception::class) override fun deserialize( json: JsonElement @@ -16,7 +18,7 @@ class UserDtoAdapter : AbsAdapter("VKApiUser") { throw Exception("$TAG error parse object") } val dto = VKApiUser() - val root = json.asJsonObject + val root = json.jsonObject dto.id = optLong(root, "id") if (dto.id == 0L) dto.id = optLong(root, "user_id") dto.first_name = optString(root, Fields.USER_FIELDS.FIRST_NAME) @@ -28,7 +30,7 @@ class UserDtoAdapter : AbsAdapter("VKApiUser") { dto.photo_100 = optString(root, Fields.USER_FIELDS.PHOTO_100) dto.photo_200 = optString(root, Fields.USER_FIELDS.PHOTO_200) if (hasObject(root, Fields.USER_FIELDS.LAST_SEEN)) { - val lastSeenRoot = root.getAsJsonObject(Fields.USER_FIELDS.LAST_SEEN) + val lastSeenRoot = root[Fields.USER_FIELDS.LAST_SEEN]?.jsonObject dto.last_seen = optLong(lastSeenRoot, "time") dto.platform = optInt(lastSeenRoot, Fields.USER_FIELDS.PLATFORM) } @@ -66,7 +68,7 @@ class UserDtoAdapter : AbsAdapter("VKApiUser") { } } if (hasObject(root, Fields.USER_FIELDS.PERSONAL)) { - val personal = root.getAsJsonObject(Fields.USER_FIELDS.PERSONAL) + val personal = root[Fields.USER_FIELDS.PERSONAL]?.jsonObject dto.smoking = optInt(personal, "smoking") dto.alcohol = optInt(personal, "alcohol") dto.political = optInt(personal, "political") @@ -75,7 +77,7 @@ class UserDtoAdapter : AbsAdapter("VKApiUser") { dto.inspired_by = optString(personal, "inspired_by") dto.religion = optString(personal, "religion") if (hasArray(personal, "langs")) { - val langs = personal["langs"]?.asJsonArray + val langs = personal["langs"]?.jsonArray dto.langs = Array(langs?.size.orZero()) { optString(langs, it) ?: "null" } } } diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/VideoAlbumDtoAdapter.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/VideoAlbumDtoAdapter.kt index dedf4bd93..42daaadc7 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/VideoAlbumDtoAdapter.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/VideoAlbumDtoAdapter.kt @@ -6,9 +6,10 @@ import dev.ragnarok.fenrir.kJson import dev.ragnarok.fenrir.orZero import dev.ragnarok.fenrir.util.serializeble.json.JsonElement import dev.ragnarok.fenrir.util.serializeble.json.intOrNull -import dev.ragnarok.fenrir.util.serializeble.json.jsonPrimitive +import dev.ragnarok.fenrir.util.serializeble.json.jsonArray +import dev.ragnarok.fenrir.util.serializeble.json.jsonObject -class VideoAlbumDtoAdapter : AbsAdapter("VKApiVideoAlbum") { +class VideoAlbumDtoAdapter : AbsDtoAdapter("VKApiVideoAlbum") { @Throws(Exception::class) override fun deserialize( json: JsonElement @@ -17,7 +18,7 @@ class VideoAlbumDtoAdapter : AbsAdapter("VKApiVideoAlbum") { throw Exception("$TAG error parse object") } val album = VKApiVideoAlbum() - val root = json.asJsonObject + val root = json.jsonObject album.id = optInt(root, "id") album.owner_id = optLong(root, "owner_id") album.title = optString(root, "title") @@ -30,20 +31,20 @@ class VideoAlbumDtoAdapter : AbsAdapter("VKApiVideoAlbum") { } } if (hasArray(root, "image")) { - val images = root.getAsJsonArray("image") + val images = root["image"]?.jsonArray for (i in 0 until images?.size.orZero()) { if (!checkObject(images?.get(i))) { continue } - if (images?.get(i)?.asJsonObject?.get("width")?.jsonPrimitive?.intOrNull.orZero() >= 800) { - album.image = images?.get(i)?.asJsonObject?.get("url")?.jsonPrimitive?.content + if (images?.get(i)?.jsonObject?.get("width")?.asPrimitiveSafe?.intOrNull.orZero() >= 800) { + album.image = images?.get(i)?.jsonObject?.get("url")?.asPrimitiveSafe?.content break } } if (album.image == null) { if (checkObject(images?.get(images.size - 1))) { album.image = - images?.get(images.size - 1)?.asJsonObject?.get("url")?.jsonPrimitive?.content + images?.get(images.size - 1)?.jsonObject?.get("url")?.asPrimitiveSafe?.content } } } else if (root.has("photo_800")) { diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/VideoDtoAdapter.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/VideoDtoAdapter.kt index 8820ed008..6894ea18f 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/VideoDtoAdapter.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/VideoDtoAdapter.kt @@ -7,8 +7,10 @@ import dev.ragnarok.fenrir.kJson import dev.ragnarok.fenrir.nonNullNoEmpty import dev.ragnarok.fenrir.orZero import dev.ragnarok.fenrir.util.serializeble.json.JsonElement +import dev.ragnarok.fenrir.util.serializeble.json.jsonArray +import dev.ragnarok.fenrir.util.serializeble.json.jsonObject -class VideoDtoAdapter : AbsAdapter("VKApiVideo") { +class VideoDtoAdapter : AbsDtoAdapter("VKApiVideo") { @Throws(Exception::class) override fun deserialize( json: JsonElement @@ -16,7 +18,7 @@ class VideoDtoAdapter : AbsAdapter("VKApiVideo") { if (!checkObject(json)) { throw Exception("$TAG error parse object") } - val root = json.asJsonObject + val root = json.jsonObject val dto = VKApiVideo() dto.id = optInt(root, "id") dto.owner_id = optLong(root, "owner_id") @@ -41,7 +43,7 @@ class VideoDtoAdapter : AbsAdapter("VKApiVideo") { dto.access_key = optString(root, "access_key") dto.album_id = optInt(root, "album_id") if (hasObject(root, "likes")) { - val likesRoot = root.getAsJsonObject("likes") + val likesRoot = root["likes"]?.jsonObject dto.likes = optInt(likesRoot, "count") dto.user_likes = optBoolean(likesRoot, "user_likes") } @@ -61,7 +63,7 @@ class VideoDtoAdapter : AbsAdapter("VKApiVideo") { } } if (hasObject(root, "files")) { - val filesRoot = root.getAsJsonObject("files") + val filesRoot = root["files"]?.jsonObject dto.mp4_240 = optString(filesRoot, "mp4_240") dto.mp4_360 = optString(filesRoot, "mp4_360") dto.mp4_480 = optString(filesRoot, "mp4_480") @@ -76,28 +78,28 @@ class VideoDtoAdapter : AbsAdapter("VKApiVideo") { val sz = if (dto.external.nonNullNoEmpty() && dto.external?.contains("youtube") == true) 320 else 800 if (hasArray(root, "image")) { - val images = root.getAsJsonArray("image") + val images = root["image"]?.jsonArray if (images?.size.orZero() > 0) { for (i in 0 until images?.size.orZero()) { - if (optInt(images?.get(i)?.asJsonObject, "width") >= sz) { - dto.image = optString(images?.get(i)?.asJsonObject, "url") + if (optInt(images?.get(i)?.asJsonObjectSafe, "width") >= sz) { + dto.image = optString(images?.get(i)?.asJsonObjectSafe, "url") break } } if (dto.image == null) dto.image = - optString(images?.get(images.size - 1)?.asJsonObject, "url") + optString(images?.get(images.size - 1)?.jsonObject, "url") } } else if (dto.image == null && hasArray(root, "first_frame")) { - val images = root.getAsJsonArray("first_frame") + val images = root["first_frame"]?.jsonArray if (images?.size.orZero() > 0) { for (i in 0 until images?.size.orZero()) { - if (optInt(images?.get(i)?.asJsonObject, "width") >= 800) { - dto.image = optString(images?.get(i)?.asJsonObject, "url") + if (optInt(images?.get(i)?.asJsonObjectSafe, "width") >= 800) { + dto.image = optString(images?.get(i)?.asJsonObjectSafe, "url") break } } if (dto.image == null) dto.image = - optString(images?.get(images.size - 1)?.asJsonObject, "url") + optString(images?.get(images.size - 1)?.asJsonObjectSafe, "url") } } else if (dto.image == null) { if (root.has("photo_800")) { diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/ViewersListAdapter.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/ViewersListDtoAdapter.kt similarity index 77% rename from app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/ViewersListAdapter.kt rename to app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/ViewersListDtoAdapter.kt index fc4374015..9bfb02c09 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/ViewersListAdapter.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/ViewersListDtoAdapter.kt @@ -6,8 +6,10 @@ import dev.ragnarok.fenrir.kJson import dev.ragnarok.fenrir.orZero import dev.ragnarok.fenrir.requireNonNull import dev.ragnarok.fenrir.util.serializeble.json.JsonElement +import dev.ragnarok.fenrir.util.serializeble.json.jsonArray +import dev.ragnarok.fenrir.util.serializeble.json.jsonObject -class ViewersListAdapter : AbsAdapter("ViewersListAdapter") { +class ViewersListDtoAdapter : AbsDtoAdapter("ViewersListAdapter") { @Throws(Exception::class) override fun deserialize( json: JsonElement @@ -16,16 +18,16 @@ class ViewersListAdapter : AbsAdapter("ViewersListAdapter") throw Exception("$TAG error parse object") } val response = ViewersListResponse() - val root = json.asJsonObject + val root = json.jsonObject response.count = optInt(root, "count") if (hasArray(root, "items")) { - val itemsArray = root.getAsJsonArray("items") + val itemsArray = root["items"]?.jsonArray response.ownersWithLikes = ArrayList(itemsArray?.size.orZero()) for (i in 0 until itemsArray?.size.orZero()) { if (!checkObject(itemsArray?.get(i))) { continue } - val itemRoot = itemsArray?.get(i)?.asJsonObject + val itemRoot = itemsArray?.get(i)?.jsonObject if (!hasObject(itemRoot, "user")) { continue } @@ -43,6 +45,6 @@ class ViewersListAdapter : AbsAdapter("ViewersListAdapter") } companion object { - private val TAG = ViewersListAdapter::class.java.simpleName + private val TAG = ViewersListDtoAdapter::class.java.simpleName } } \ No newline at end of file diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/catalog_v2_audio/VKApiCatalogV2LinkDtoAdapter.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/catalog_v2_audio/VKApiCatalogV2LinkDtoAdapter.kt index ec87f118d..9f595376c 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/catalog_v2_audio/VKApiCatalogV2LinkDtoAdapter.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/adapters/catalog_v2_audio/VKApiCatalogV2LinkDtoAdapter.kt @@ -1,11 +1,13 @@ package dev.ragnarok.fenrir.api.adapters.catalog_v2_audio -import dev.ragnarok.fenrir.api.adapters.AbsAdapter +import dev.ragnarok.fenrir.api.adapters.AbsDtoAdapter import dev.ragnarok.fenrir.api.model.catalog_v2_audio.VKApiCatalogV2Link import dev.ragnarok.fenrir.orZero import dev.ragnarok.fenrir.util.serializeble.json.JsonElement +import dev.ragnarok.fenrir.util.serializeble.json.jsonArray +import dev.ragnarok.fenrir.util.serializeble.json.jsonObject -class VKApiCatalogV2LinkDtoAdapter : AbsAdapter("VKApiCatalogV2Link") { +class VKApiCatalogV2LinkDtoAdapter : AbsDtoAdapter("VKApiCatalogV2Link") { @Throws(Exception::class) override fun deserialize( json: JsonElement @@ -14,19 +16,19 @@ class VKApiCatalogV2LinkDtoAdapter : AbsAdapter("VKApiCatalo throw Exception("$TAG error parse object") } val dto = VKApiCatalogV2Link() - val root = json.asJsonObject + val root = json.jsonObject dto.id = optString(root, "id") dto.url = optString(root, "url") dto.title = optString(root, "title") dto.subtitle = optString(root, "subtitle") if (hasArray(root, "image")) { - val arr = root.getAsJsonArray("image") + val arr = root["image"]?.jsonArray var max_res = 0 for (i in arr.orEmpty()) { if (!checkObject(i)) { continue } - val res = i.asJsonObject + val res = i.jsonObject val curr_res = optInt(res, "height") * optInt(res, "width") if (curr_res in (max_res + 1)..40000) { max_res = curr_res diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/model/GroupSettingsDto.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/model/GroupSettingsDto.kt index 688f9ad79..6254a377f 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/model/GroupSettingsDto.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/model/GroupSettingsDto.kt @@ -1,10 +1,10 @@ package dev.ragnarok.fenrir.api.model -import dev.ragnarok.fenrir.api.adapters.GroupSettingsAdapter +import dev.ragnarok.fenrir.api.adapters.GroupSettingsDtoAdapter import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable -@Serializable(with = GroupSettingsAdapter::class) +@Serializable(with = GroupSettingsDtoAdapter::class) class GroupSettingsDto { var title: String? = null var description: String? = null diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/model/VKApiLyrics.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/model/VKApiLyrics.kt index 6dcc2c916..5467991e2 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/model/VKApiLyrics.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/model/VKApiLyrics.kt @@ -1,9 +1,9 @@ package dev.ragnarok.fenrir.api.model -import dev.ragnarok.fenrir.api.adapters.LyricsAdapter +import dev.ragnarok.fenrir.api.adapters.LyricsDtoAdapter import kotlinx.serialization.Serializable -@Serializable(with = LyricsAdapter::class) +@Serializable(with = LyricsDtoAdapter::class) class VKApiLyrics { var text: String? = null } \ No newline at end of file diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/model/VKApiNews.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/model/VKApiNews.kt index be755cad6..acd143937 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/model/VKApiNews.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/model/VKApiNews.kt @@ -1,11 +1,11 @@ package dev.ragnarok.fenrir.api.model -import dev.ragnarok.fenrir.api.adapters.NewsAdapter +import dev.ragnarok.fenrir.api.adapters.NewsDtoAdapter import dev.ragnarok.fenrir.nonNullNoEmpty import dev.ragnarok.fenrir.orZero import kotlinx.serialization.Serializable -@Serializable(with = NewsAdapter::class) +@Serializable(with = NewsDtoAdapter::class) class VKApiNews { var type //friends_recomm //post : String? = null diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/model/longpoll/AbsLongpollEvent.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/model/longpoll/AbsLongpollEvent.kt index 03129f1c6..8ca7c21ff 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/model/longpoll/AbsLongpollEvent.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/model/longpoll/AbsLongpollEvent.kt @@ -1,9 +1,9 @@ package dev.ragnarok.fenrir.api.model.longpoll -import dev.ragnarok.fenrir.api.adapters.LongpollUpdateAdapter +import dev.ragnarok.fenrir.api.adapters.LongpollUpdateDtoAdapter import kotlinx.serialization.Serializable -@Serializable(with = LongpollUpdateAdapter::class) +@Serializable(with = LongpollUpdateDtoAdapter::class) open class AbsLongpollEvent(val action: Int) { companion object { const val ACTION_MESSAGES_FLAGS_SET = 2 diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/model/longpoll/VKApiLongpollUpdates.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/model/longpoll/VKApiLongpollUpdates.kt index 8283fa9b7..b154a8022 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/model/longpoll/VKApiLongpollUpdates.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/model/longpoll/VKApiLongpollUpdates.kt @@ -1,10 +1,10 @@ package dev.ragnarok.fenrir.api.model.longpoll -import dev.ragnarok.fenrir.api.adapters.LongpollUpdatesAdapter +import dev.ragnarok.fenrir.api.adapters.LongpollUpdatesDtoAdapter import dev.ragnarok.fenrir.util.Utils.safeCountOfMultiple import kotlinx.serialization.Serializable -@Serializable(with = LongpollUpdatesAdapter::class) +@Serializable(with = LongpollUpdatesDtoAdapter::class) class VKApiLongpollUpdates { // TODO Message edit update //{"ts":1841741106,"updates":[[5,1200880,51,26632922,1528116889,"и тд",{"title":""},{},1000771599]]} diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/model/response/ChatsInfoResponse.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/model/response/ChatsInfoResponse.kt index 8a45c1085..f7dd5e224 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/model/response/ChatsInfoResponse.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/model/response/ChatsInfoResponse.kt @@ -1,10 +1,10 @@ package dev.ragnarok.fenrir.api.model.response -import dev.ragnarok.fenrir.api.adapters.ChatsInfoAdapter +import dev.ragnarok.fenrir.api.adapters.ChatsInfoDtoAdapter import dev.ragnarok.fenrir.api.model.VKApiChat import kotlinx.serialization.Serializable -@Serializable(with = ChatsInfoAdapter::class) +@Serializable(with = ChatsInfoDtoAdapter::class) class ChatsInfoResponse { var chats: List? = null } \ No newline at end of file diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/model/response/CustomCommentsResponse.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/model/response/CustomCommentsResponse.kt index 45ccacdd0..5f3eb92ef 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/model/response/CustomCommentsResponse.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/model/response/CustomCommentsResponse.kt @@ -1,6 +1,6 @@ package dev.ragnarok.fenrir.api.model.response -import dev.ragnarok.fenrir.api.adapters.CustomCommentsResponseAdapter +import dev.ragnarok.fenrir.api.adapters.CustomCommentsResponseDtoAdapter import dev.ragnarok.fenrir.api.model.VKApiComment import dev.ragnarok.fenrir.api.model.VKApiCommunity import dev.ragnarok.fenrir.api.model.VKApiPoll @@ -8,7 +8,7 @@ import dev.ragnarok.fenrir.api.model.VKApiUser import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable -@Serializable(with = CustomCommentsResponseAdapter::class) +@Serializable(with = CustomCommentsResponseDtoAdapter::class) class CustomCommentsResponse { // Parse manually in CustomCommentsResponseAdapter var main: Main? = null diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/model/response/LikesListResponse.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/model/response/LikesListResponse.kt index 69478b7cd..52369fabd 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/model/response/LikesListResponse.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/model/response/LikesListResponse.kt @@ -1,10 +1,10 @@ package dev.ragnarok.fenrir.api.model.response -import dev.ragnarok.fenrir.api.adapters.LikesListAdapter +import dev.ragnarok.fenrir.api.adapters.LikesListDtoAdapter import dev.ragnarok.fenrir.api.model.VKApiOwner import kotlinx.serialization.Serializable -@Serializable(with = LikesListAdapter::class) +@Serializable(with = LikesListDtoAdapter::class) class LikesListResponse { var count = 0 var owners: ArrayList? = null diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/model/response/ViewersListResponse.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/model/response/ViewersListResponse.kt index aad5c725b..9dc6e1cd3 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/model/response/ViewersListResponse.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/api/model/response/ViewersListResponse.kt @@ -1,10 +1,10 @@ package dev.ragnarok.fenrir.api.model.response -import dev.ragnarok.fenrir.api.adapters.ViewersListAdapter +import dev.ragnarok.fenrir.api.adapters.ViewersListDtoAdapter import dev.ragnarok.fenrir.api.model.VKApiOwner import kotlinx.serialization.Serializable -@Serializable(with = ViewersListAdapter::class) +@Serializable(with = ViewersListDtoAdapter::class) class ViewersListResponse { var count = 0 var ownersWithLikes: ArrayList>? = null diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/fragment/accounts/AccountsPresenter.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/fragment/accounts/AccountsPresenter.kt index 33285787a..61ac5a044 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/fragment/accounts/AccountsPresenter.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/fragment/accounts/AccountsPresenter.kt @@ -12,11 +12,11 @@ import dev.ragnarok.fenrir.Includes import dev.ragnarok.fenrir.R import dev.ragnarok.fenrir.api.ApiException import dev.ragnarok.fenrir.api.Auth -import dev.ragnarok.fenrir.api.adapters.AbsAdapter.Companion.asJsonArray -import dev.ragnarok.fenrir.api.adapters.AbsAdapter.Companion.asJsonObject -import dev.ragnarok.fenrir.api.adapters.AbsAdapter.Companion.asJsonObjectSafe -import dev.ragnarok.fenrir.api.adapters.AbsAdapter.Companion.asPrimitiveSafe -import dev.ragnarok.fenrir.api.adapters.AbsAdapter.Companion.has +import dev.ragnarok.fenrir.api.adapters.AbsDtoAdapter.Companion.asJsonObjectSafe +import dev.ragnarok.fenrir.api.adapters.AbsDtoAdapter.Companion.asPrimitiveSafe +import dev.ragnarok.fenrir.api.adapters.AbsDtoAdapter.Companion.hasArray +import dev.ragnarok.fenrir.api.adapters.AbsDtoAdapter.Companion.hasObject +import dev.ragnarok.fenrir.api.adapters.AbsDtoAdapter.Companion.hasPrimitive import dev.ragnarok.fenrir.api.interfaces.INetworker import dev.ragnarok.fenrir.api.model.VKApiUser import dev.ragnarok.fenrir.api.model.response.BaseResponse @@ -394,14 +394,14 @@ class AccountsPresenter(savedInstanceState: Bundle?) : if (file.exists()) { val elem = kJson.parseToJsonElement(FileInputStream(file)).jsonObject - val exchangeToken = elem["exchange_token"]?.jsonPrimitive?.contentOrNull + val exchangeToken = elem["exchange_token"]?.asPrimitiveSafe?.contentOrNull ?: return val type = - elem["type"]?.jsonPrimitive?.intOrNull ?: return - val device = elem["device"]?.jsonPrimitive?.contentOrNull - val device_id = elem["device_id"]?.jsonPrimitive?.contentOrNull - val api_ver = elem["api_ver"]?.jsonPrimitive?.contentOrNull - val sak_version = elem["sak_version"]?.jsonPrimitive?.contentOrNull + elem["type"]?.asPrimitiveSafe?.intOrNull ?: return + val device = elem["device"]?.asPrimitiveSafe?.contentOrNull + val device_id = elem["device_id"]?.asPrimitiveSafe?.contentOrNull + val api_ver = elem["api_ver"]?.asPrimitiveSafe?.contentOrNull + val sak_version = elem["sak_version"]?.asPrimitiveSafe?.contentOrNull appendDisposable( networker.vkDirectAuth(type, device).authByExchangeToken( @@ -428,7 +428,7 @@ class AccountsPresenter(savedInstanceState: Bundle?) : ) view?.showColoredSnack(R.string.success, Color.parseColor("#AA48BE2D")) - if (elem.has("login")) { + if (hasPrimitive(elem, "login")) { Settings.get().accounts().storeLogin( user_id, elem["login"]?.jsonPrimitive?.contentOrNull ?: return@subscribe @@ -469,43 +469,43 @@ class AccountsPresenter(savedInstanceState: Bundle?) : ?.showToastWarningBottom(R.string.settings_for_another_client) } val reader = obj["fenrir_accounts"] - for (i in reader?.asJsonArray.orEmpty()) { - val elem = i.asJsonObject - val id = elem["user_id"]?.jsonPrimitive?.longOrNull ?: continue + for (i in reader?.jsonArray.orEmpty()) { + val elem = i.jsonObject + val id = elem["user_id"]?.asPrimitiveSafe?.longOrNull ?: continue if (Settings.get().accounts().registered.contains(id)) continue - val token = elem["access_token"]?.jsonPrimitive?.contentOrNull ?: continue - val Type = elem["type"]?.jsonPrimitive?.intOrNull ?: continue + val token = elem["access_token"]?.asPrimitiveSafe?.contentOrNull ?: continue + val Type = elem["type"]?.asPrimitiveSafe?.intOrNull ?: continue processNewAccount( id, token, Type, null, null, "fenrir_app", isCurrent = false, needSave = false ) - if (elem.has("login")) { + if (hasPrimitive(elem, "login")) { Settings.get().accounts().storeLogin( id, elem["login"]?.jsonPrimitive?.contentOrNull ?: continue ) } - if (elem.has("device")) { + if (hasPrimitive(elem, "device")) { Settings.get().accounts().storeDevice( id, elem["device"]?.jsonPrimitive?.contentOrNull ?: continue ) } } - if (obj.has("settings")) { - SettingsBackup().doRestore(obj["settings"]?.asJsonObject) + if (hasObject(obj, "settings")) { + SettingsBackup().doRestore(obj["settings"]?.jsonObject) view?.customToast?.setDuration(Toast.LENGTH_LONG) view?.customToast?.showToastSuccessBottom( R.string.need_restart ) } try { - if (obj.has("conversations_saved")) { - for (i in obj["conversations_saved"]?.asJsonArray.orEmpty()) { - val aid = i.asJsonObject["account_id"]?.jsonPrimitive?.long ?: continue + if (hasArray(obj, "conversations_saved")) { + for (i in obj["conversations_saved"]?.jsonArray.orEmpty()) { + val aid = i.jsonObject["account_id"]?.asPrimitiveSafe?.long ?: continue val dialogsJsonElem = - i.asJsonObject["conversation"]?.jsonArray ?: continue + i.jsonObject["conversation"]?.jsonArray ?: continue if (!dialogsJsonElem.isEmpty()) { Includes.stores.dialogs().insertDialogs( aid, kJson.decodeFromJsonElement( @@ -706,4 +706,4 @@ class AccountsPresenter(savedInstanceState: Bundle?) : init { fireLoad(false) } -} \ No newline at end of file +} diff --git a/app_filegallery/src/main/kotlin/dev/ragnarok/filegallery/api/adapters/AbsAdapter.kt b/app_filegallery/src/main/kotlin/dev/ragnarok/filegallery/api/adapters/AbsDtoAdapter.kt similarity index 89% rename from app_filegallery/src/main/kotlin/dev/ragnarok/filegallery/api/adapters/AbsAdapter.kt rename to app_filegallery/src/main/kotlin/dev/ragnarok/filegallery/api/adapters/AbsDtoAdapter.kt index 67dcce546..0f8b36180 100644 --- a/app_filegallery/src/main/kotlin/dev/ragnarok/filegallery/api/adapters/AbsAdapter.kt +++ b/app_filegallery/src/main/kotlin/dev/ragnarok/filegallery/api/adapters/AbsDtoAdapter.kt @@ -5,17 +5,15 @@ import dev.ragnarok.filegallery.kJson import dev.ragnarok.filegallery.util.serializeble.json.JsonArray import dev.ragnarok.filegallery.util.serializeble.json.JsonDecoder import dev.ragnarok.filegallery.util.serializeble.json.JsonElement +import dev.ragnarok.filegallery.util.serializeble.json.JsonNull import dev.ragnarok.filegallery.util.serializeble.json.JsonObject import dev.ragnarok.filegallery.util.serializeble.json.JsonPrimitive import dev.ragnarok.filegallery.util.serializeble.json.booleanOrNull import dev.ragnarok.filegallery.util.serializeble.json.doubleOrNull import dev.ragnarok.filegallery.util.serializeble.json.floatOrNull -import dev.ragnarok.filegallery.util.serializeble.json.int import dev.ragnarok.filegallery.util.serializeble.json.intOrNull import dev.ragnarok.filegallery.util.serializeble.json.jsonArray -import dev.ragnarok.filegallery.util.serializeble.json.jsonObject import dev.ragnarok.filegallery.util.serializeble.json.jsonPrimitive -import dev.ragnarok.filegallery.util.serializeble.json.long import dev.ragnarok.filegallery.util.serializeble.json.longOrNull import dev.ragnarok.filegallery.util.serializeble.msgpack.internal.BasicMsgPackDecoder import kotlinx.serialization.KSerializer @@ -25,7 +23,7 @@ import kotlinx.serialization.encoding.Decoder import kotlinx.serialization.encoding.Encoder import kotlin.contracts.contract -abstract class AbsAdapter(name: String) : KSerializer { +abstract class AbsDtoAdapter(name: String) : KSerializer { override val descriptor: SerialDescriptor = buildClassSerialDescriptor(name) override fun deserialize(decoder: Decoder): T { @@ -49,7 +47,7 @@ abstract class AbsAdapter(name: String) : KSerializer { fun checkPrimitive(element: JsonElement?): Boolean { contract { - returns(true) implies (element is JsonPrimitive) + returns(true) implies (element is JsonPrimitive && element !is JsonNull) } return element is JsonPrimitive } @@ -68,7 +66,7 @@ abstract class AbsAdapter(name: String) : KSerializer { } obj ?: return false if (obj.containsKey(name)) { - return obj[name] is JsonPrimitive + return checkPrimitive(obj[name]) } return false } @@ -80,7 +78,7 @@ abstract class AbsAdapter(name: String) : KSerializer { } obj ?: return false if (obj.containsKey(name)) { - return obj[name] is JsonObject + return checkObject(obj[name]) } return false } @@ -92,8 +90,7 @@ abstract class AbsAdapter(name: String) : KSerializer { } obj ?: return false if (obj.containsKey(name)) { - val element = obj[name] - return element is JsonArray && element.jsonArray.size > 0 + return checkArray(obj[name]) } return false } @@ -107,7 +104,7 @@ abstract class AbsAdapter(name: String) : KSerializer { json ?: return fallback return try { val element = json[name] - if (element is JsonPrimitive) element.content else fallback + if (checkPrimitive(element)) element.content else fallback } catch (e: Exception) { if (Constants.IS_DEBUG) { e.printStackTrace() @@ -125,7 +122,7 @@ abstract class AbsAdapter(name: String) : KSerializer { for (i in names) { try { val element = json[i] - if (element is JsonPrimitive) return element.content else continue + if (checkPrimitive(element)) return element.content else continue } catch (e: Exception) { if (Constants.IS_DEBUG) { e.printStackTrace() @@ -164,7 +161,7 @@ abstract class AbsAdapter(name: String) : KSerializer { json ?: return fallback return try { val element = json[name] - (element as? JsonPrimitive)?.intOrNull ?: fallback + if (checkPrimitive(element)) element.intOrNull ?: fallback else fallback } catch (e: Exception) { if (Constants.IS_DEBUG) { e.printStackTrace() @@ -181,7 +178,7 @@ abstract class AbsAdapter(name: String) : KSerializer { json ?: return fallback return try { val element = json[name] - (element as? JsonPrimitive)?.floatOrNull ?: fallback + if (checkPrimitive(element)) element.floatOrNull ?: fallback else fallback } catch (e: Exception) { if (Constants.IS_DEBUG) { e.printStackTrace() @@ -198,7 +195,7 @@ abstract class AbsAdapter(name: String) : KSerializer { json ?: return fallback return try { val element = json[name] - (element as? JsonPrimitive)?.doubleOrNull ?: fallback + if (checkPrimitive(element)) element.doubleOrNull ?: fallback else fallback } catch (e: Exception) { if (Constants.IS_DEBUG) { e.printStackTrace() @@ -215,7 +212,7 @@ abstract class AbsAdapter(name: String) : KSerializer { json ?: return fallback return try { val element = json[name] - (element as? JsonPrimitive)?.longOrNull ?: fallback + if (checkPrimitive(element)) element.longOrNull ?: fallback else fallback } catch (e: Exception) { if (Constants.IS_DEBUG) { e.printStackTrace() @@ -232,7 +229,7 @@ abstract class AbsAdapter(name: String) : KSerializer { return try { for (name in names) { val element = json[name] - if (element is JsonPrimitive) { + if (checkPrimitive(element)) { return element.intOrNull ?: fallback } } @@ -253,7 +250,7 @@ abstract class AbsAdapter(name: String) : KSerializer { return try { for (name in names) { val element = json[name] - if (element is JsonPrimitive) { + if (checkPrimitive(element)) { return element.longOrNull ?: fallback } } @@ -274,7 +271,7 @@ abstract class AbsAdapter(name: String) : KSerializer { array ?: return fallback return try { val opt = opt(array, index) - (opt as? JsonPrimitive)?.longOrNull ?: fallback + if (checkPrimitive(opt)) opt.longOrNull ?: fallback else fallback } catch (e: Exception) { if (Constants.IS_DEBUG) { e.printStackTrace() @@ -292,7 +289,7 @@ abstract class AbsAdapter(name: String) : KSerializer { array ?: return fallback return try { val opt = opt(array, index) - (opt as? JsonPrimitive)?.intOrNull ?: fallback + if (checkPrimitive(opt)) opt.intOrNull ?: fallback else fallback } catch (e: Exception) { if (Constants.IS_DEBUG) { e.printStackTrace() @@ -321,7 +318,7 @@ abstract class AbsAdapter(name: String) : KSerializer { array ?: return fallback return try { val opt = opt(array, index) - if (opt is JsonPrimitive) opt.content else fallback + if (checkPrimitive(opt)) opt.content else fallback } catch (e: Exception) { if (Constants.IS_DEBUG) { e.printStackTrace() @@ -483,7 +480,7 @@ abstract class AbsAdapter(name: String) : KSerializer { array ?: return LongArray(0) val list = LongArray(array.size) for (i in 0 until array.size) { - list[i] = array[i].jsonPrimitive.long + list[i] = array[i].asPrimitiveSafe?.longOrNull ?: 0 } return list } @@ -495,7 +492,7 @@ abstract class AbsAdapter(name: String) : KSerializer { array ?: return IntArray(0) val list = IntArray(array.size) for (i in 0 until array.size) { - list[i] = array[i].jsonPrimitive.int + list[i] = array[i].asPrimitiveSafe?.intOrNull ?: 0 } return list } @@ -508,28 +505,14 @@ abstract class AbsAdapter(name: String) : KSerializer { return Array(array.size) { optString(array, it) ?: "null" } } - fun JsonObject?.getAsJsonArray(name: String): JsonArray? { - return this?.get(name)?.jsonArray - } - - fun JsonObject?.getAsJsonObject(name: String): JsonObject? { - return this?.get(name)?.jsonObject - } - - val JsonElement.asJsonObject: JsonObject - get() = this as? JsonObject ?: error("JsonObject") - val JsonElement.asJsonObjectSafe: JsonObject? - get() = this as? JsonObject + get() = if (checkObject(this)) this else null val JsonElement.asPrimitiveSafe: JsonPrimitive? - get() = this as? JsonPrimitive + get() = if (checkPrimitive(this)) this else null val JsonElement.asJsonArraySafe: JsonArray? - get() = this as? JsonArray - - val JsonElement.asJsonArray: JsonArray - get() = this as? JsonArray ?: error("JsonArray") + get() = if (checkArray(this)) this else null fun JsonObject?.has(name: String): Boolean { return this?.containsKey(name) ?: false diff --git a/app_filegallery/src/main/kotlin/dev/ragnarok/filegallery/api/adapters/AudioDtoAdapter.kt b/app_filegallery/src/main/kotlin/dev/ragnarok/filegallery/api/adapters/AudioDtoAdapter.kt index d281f8f27..2a4b8caac 100644 --- a/app_filegallery/src/main/kotlin/dev/ragnarok/filegallery/api/adapters/AudioDtoAdapter.kt +++ b/app_filegallery/src/main/kotlin/dev/ragnarok/filegallery/api/adapters/AudioDtoAdapter.kt @@ -2,8 +2,9 @@ package dev.ragnarok.filegallery.api.adapters import dev.ragnarok.filegallery.model.Audio import dev.ragnarok.filegallery.util.serializeble.json.JsonElement +import dev.ragnarok.filegallery.util.serializeble.json.jsonObject -class AudioDtoAdapter : AbsAdapter