Browse Source

sample: gson支持kt默认值

drake 1 year ago
parent
commit
256599c062

+ 0 - 1
sample/build.gradle

@@ -79,7 +79,6 @@ dependencies {
     implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.2" // JSON序列化库, 首选推荐使用
     implementation "org.jetbrains.kotlinx:kotlinx-serialization-protobuf:1.2.0"  // protobuf序列化
     implementation "com.squareup.moshi:moshi-kotlin:1.14.0" // JSON序列化库, 强校验, JSON字段缺失会导致解析异常, 故不推荐
-    implementation "org.jetbrains.kotlin:kotlin-reflect:1.7.10"
     implementation 'com.google.code.gson:gson:2.8.6' // JSON序列化库, 会导致kotlin默认值无效, 故不推荐
     implementation 'com.alibaba:fastjson:1.2.73' // JSON序列化库, 会导致kotlin默认值无效(除非引入kt-reflect), 不推荐
 

+ 5 - 4
sample/src/main/java/com/drake/net/sample/model/GameModel.kt

@@ -1,12 +1,13 @@
 package com.drake.net.sample.model
 
-@kotlinx.serialization.Serializable
-data class GameModel(
-    var total: Int = 0,
+class GameModel(
+    var total: Int = 23,
+    var firstName: String = "ssss",
     var list: List<Data> = listOf()
 ) {
 
-    @kotlinx.serialization.Serializable
+    val lastName = "吴彦祖"
+
     data class Data(
         var id: Int = 0,
         var img: String = "",

+ 1 - 1
sample/src/main/java/com/drake/net/sample/ui/fragment/converter/GsonConvertFragment.kt

@@ -23,7 +23,7 @@ class GsonConvertFragment :
         scopeNetLife {
             binding.tvFragment.text = Get<GameModel>(Api.GAME) {
                 converter = GsonConverter() // 单例转换器, 此时会忽略全局转换器, 在Net中可以直接解析List等嵌套泛型数据
-            }.await().list[0].name
+            }.await().firstName
         }
     }
 

+ 1 - 1
sample/src/main/res/navigation/nav_converter.xml

@@ -3,7 +3,7 @@
     xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:tools="http://schemas.android.com/tools"
     android:id="@+id/nav_converter"
-    app:startDestination="@id/serializationConvertFragment">
+    app:startDestination="@id/gsonConvertFragment">
 
     <fragment
         android:id="@+id/serializationConvertFragment"