Skip to content

Commit

Permalink
22
Browse files Browse the repository at this point in the history
22
  • Loading branch information
umerov1999 committed Apr 21, 2023
1 parent 2815419 commit 617ad81
Show file tree
Hide file tree
Showing 77 changed files with 665 additions and 423 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,7 +23,7 @@ import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlin.contracts.contract

abstract class AbsAdapter<T>(name: String) : KSerializer<T> {
abstract class AbsDtoAdapter<T>(name: String) : KSerializer<T> {
override val descriptor: SerialDescriptor = buildClassSerialDescriptor(name)

override fun deserialize(decoder: Decoder): T {
Expand All @@ -49,7 +47,7 @@ abstract class AbsAdapter<T>(name: String) : KSerializer<T> {

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
}
Expand All @@ -68,7 +66,7 @@ abstract class AbsAdapter<T>(name: String) : KSerializer<T> {
}
obj ?: return false
if (obj.containsKey(name)) {
return obj[name] is JsonPrimitive
return checkPrimitive(obj[name])
}
return false
}
Expand All @@ -80,7 +78,7 @@ abstract class AbsAdapter<T>(name: String) : KSerializer<T> {
}
obj ?: return false
if (obj.containsKey(name)) {
return obj[name] is JsonObject
return checkObject(obj[name])
}
return false
}
Expand All @@ -92,8 +90,7 @@ abstract class AbsAdapter<T>(name: String) : KSerializer<T> {
}
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
}
Expand All @@ -107,7 +104,7 @@ abstract class AbsAdapter<T>(name: String) : KSerializer<T> {
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()
Expand All @@ -125,7 +122,7 @@ abstract class AbsAdapter<T>(name: String) : KSerializer<T> {
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()
Expand Down Expand Up @@ -164,7 +161,7 @@ abstract class AbsAdapter<T>(name: String) : KSerializer<T> {
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()
Expand All @@ -181,7 +178,7 @@ abstract class AbsAdapter<T>(name: String) : KSerializer<T> {
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()
Expand All @@ -198,7 +195,7 @@ abstract class AbsAdapter<T>(name: String) : KSerializer<T> {
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()
Expand All @@ -215,7 +212,7 @@ abstract class AbsAdapter<T>(name: String) : KSerializer<T> {
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()
Expand All @@ -232,7 +229,7 @@ abstract class AbsAdapter<T>(name: String) : KSerializer<T> {
return try {
for (name in names) {
val element = json[name]
if (element is JsonPrimitive) {
if (checkPrimitive(element)) {
return element.intOrNull ?: fallback
}
}
Expand All @@ -253,7 +250,7 @@ abstract class AbsAdapter<T>(name: String) : KSerializer<T> {
return try {
for (name in names) {
val element = json[name]
if (element is JsonPrimitive) {
if (checkPrimitive(element)) {
return element.longOrNull ?: fallback
}
}
Expand All @@ -274,7 +271,7 @@ abstract class AbsAdapter<T>(name: String) : KSerializer<T> {
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()
Expand All @@ -292,7 +289,7 @@ abstract class AbsAdapter<T>(name: String) : KSerializer<T> {
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()
Expand Down Expand Up @@ -321,7 +318,7 @@ abstract class AbsAdapter<T>(name: String) : KSerializer<T> {
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()
Expand Down Expand Up @@ -483,7 +480,7 @@ abstract class AbsAdapter<T>(name: String) : KSerializer<T> {
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
}
Expand All @@ -495,7 +492,7 @@ abstract class AbsAdapter<T>(name: String) : KSerializer<T> {
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
}
Expand All @@ -508,28 +505,14 @@ abstract class AbsAdapter<T>(name: String) : KSerializer<T> {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import dev.ragnarok.fenrir.util.serializeble.json.JsonElement

import dev.ragnarok.fenrir.util.serializeble.json.jsonObject

class ArticleDtoAdapter : AbsAdapter<VKApiArticle>("VKApiArticle") {
class ArticleDtoAdapter : AbsDtoAdapter<VKApiArticle>("VKApiArticle") {
@Throws(Exception::class)
override fun deserialize(
json: JsonElement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>("VKApiAttachments") {
class AttachmentsDtoAdapter : AbsDtoAdapter<VKApiAttachments>("VKApiAttachments") {
@Throws(Exception::class)
override fun deserialize(
json: JsonElement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>("VKApiAttachments.Entry") {
class AttachmentsEntryDtoAdapter : AbsDtoAdapter<VKApiAttachments.Entry>("VKApiAttachments.Entry") {
@Throws(Exception::class)
override fun deserialize(
json: JsonElement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>("VKApiAudio") {
class AudioDtoAdapter : AbsDtoAdapter<VKApiAudio>("VKApiAudio") {
@Throws(Exception::class)
override fun deserialize(
json: JsonElement
Expand All @@ -28,7 +29,7 @@ class AudioDtoAdapter : AbsAdapter<VKApiAudio>("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<String, String> = HashMap(arr?.size.orZero())
for (i in arr.orEmpty()) {
if (!checkObject(i)) {
Expand All @@ -44,13 +45,13 @@ class AudioDtoAdapter : AbsAdapter<VKApiAudio>("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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>("VKApiAudioPlaylist") {
class AudioPlaylistDtoAdapter : AbsDtoAdapter<VKApiAudioPlaylist>("VKApiAudioPlaylist") {
@Throws(Exception::class)
override fun deserialize(
json: JsonElement
Expand All @@ -24,43 +25,43 @@ class AudioPlaylistDtoAdapter : AbsAdapter<VKApiAudioPlaylist>("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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>("VKApiChat") {
class ChatDtoAdapter : AbsDtoAdapter<VKApiChat>("VKApiChat") {
@Throws(Exception::class)
override fun deserialize(
json: JsonElement
Expand All @@ -19,7 +20,7 @@ class ChatDtoAdapter : AbsAdapter<VKApiChat>("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")
Expand All @@ -28,11 +29,11 @@ class ChatDtoAdapter : AbsAdapter<VKApiChat>("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()
Expand All @@ -42,7 +43,7 @@ class ChatDtoAdapter : AbsAdapter<VKApiChat>("VKApiChat") {
if (!checkObject(userElement)) {
continue
}
val jsonObject = userElement.asJsonObject
val jsonObject = userElement.jsonObject
val type = optString(jsonObject, "type")
val chatUserDto = ChatUserDto()
chatUserDto.type = type
Expand Down
Loading

0 comments on commit 617ad81

Please sign in to comment.