@@ -20,6 +20,7 @@ import com.beust.klaxon.Klaxon
2020import com.engineer.imitate.R
2121import com.engineer.imitate.model.School
2222import com.engineer.imitate.model.Schools
23+ import com.engineer.imitate.model.sub.parseWithKotlinxSerializable
2324import com.engineer.imitate.room.SchoolRepository
2425import com.engineer.imitate.util.IOTool
2526import com.engineer.imitate.util.bind
@@ -29,6 +30,10 @@ import com.google.android.material.tabs.TabLayout
2930import com.google.android.material.tabs.TabLayoutMediator
3031import com.google.gson.Gson
3132import com.google.gson.reflect.TypeToken
33+
34+ import com.squareup.moshi.Moshi
35+ import com.squareup.moshi.Types
36+ import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
3237import io.reactivex.Observable
3338import io.reactivex.android.schedulers.AndroidSchedulers
3439import io.reactivex.schedulers.Schedulers
@@ -67,21 +72,26 @@ class CoroutinesFragment : Fragment() {
6772 progress = view.findViewById(R .id.progress)
6873 school_view_pager2 = view.findViewById(R .id.school_view_pager2)
6974 school_tab_layout = view.findViewById(R .id.school_tab_layout)
75+ useCoroutine2 { parseWithKlaxon(it) }
76+ useCoroutine2 { parseWithFastJson(it) }
77+ useCoroutine2 { parseWithFastJson1(it) }
7078 useCoroutine2 { parseWithGson(it) }
79+ useCoroutine2 { parseWithMoshi(it) }
80+ useCoroutine2 { parseWithKotlinxSerializable(it) }
7181 }
7282
73- private fun useCoroutine2 (block : (String ) -> List < Schools > ? ) {
83+ private fun useCoroutine2 (block : (String ) -> Unit ) {
7484 Thread .currentThread().name.lg(TAG )
7585 CoroutineScope (Dispatchers .IO ).launch {
7686 Thread .currentThread().name.lg(TAG )
7787 val start = System .currentTimeMillis()
7888 val json = IOTool .readStrFromAssets(" school.json" , context)
7989 Log .e(TAG , " read json cost ${System .currentTimeMillis() - start} " )
8090 val list = block(json)
81- saveToRoom(list)
91+ // saveToRoom(list)
8292 withContext(Dispatchers .Main ) {
83- Thread .currentThread().name.lg(TAG )
84- setUpPager(list)
93+ // Thread.currentThread().name.lg(TAG)
94+ // // setUpPager(list)
8595 progress.visibility = View .GONE
8696 }
8797 }
@@ -218,25 +228,43 @@ class ListFragment : Fragment() {
218228
219229data class Item (val title : String , val fragment : Fragment , val size : Int )
220230
221-
222231private fun parseWithKlaxon (json : String ): List <Schools >? {
223232 val s = System .currentTimeMillis()
224233 val list = Klaxon ().parseArray<Schools >(json)
225- Log .e(TAG , " parse json cost ${System .currentTimeMillis() - s} " )
234+ Log .e(TAG , " parseWithKlaxon cost ${System .currentTimeMillis() - s} " )
226235 return list
227236}
228237
229238private fun parseWithFastJson (json : String ): List <Schools >? {
230239 val s = System .currentTimeMillis()
231240 val list = JSONObject .parseArray(json, Schools ::class .java)
232- Log .e(TAG , " parse json cost ${System .currentTimeMillis() - s} " )
241+ Log .e(TAG , " parseWithFastJson cost ${System .currentTimeMillis() - s} " )
242+ return list
243+ }
244+
245+ private fun parseWithFastJson1 (json : String ): List <com.engineer.imitate.model.java.Schools >? {
246+ val s = System .currentTimeMillis()
247+ val list = JSONObject .parseArray(json, com.engineer.imitate.model.java.Schools ::class .java)
248+ Log .e(TAG , " parseWithFastJson1 cost ${System .currentTimeMillis() - s} " )
233249 return list
234250}
235251
236252private fun parseWithGson (json : String ): List <Schools >? {
237253 val s = System .currentTimeMillis()
238254 val gson = Gson ()
239255 val list: List <Schools > = gson.fromJson(json, object : TypeToken <List <Schools >>() {}.type)
240- Log .e(TAG , " parse json cost ${System .currentTimeMillis() - s} " )
256+ Log .e(TAG , " parseWithGson cost ${System .currentTimeMillis() - s} " )
257+ return list
258+ }
259+
260+ private fun parseWithMoshi (json : String ) :List <Schools >? {
261+ val s = System .currentTimeMillis()
262+
263+ val moshi = Moshi .Builder ()
264+ .addLast(KotlinJsonAdapterFactory ()) // 支持 Kotlin 数据类
265+ .build()
266+ val adapter = moshi.adapter<List <Schools >>(Types .newParameterizedType(List ::class .java, Schools ::class .java))
267+ val list = adapter.fromJson(json)
268+ Log .e(TAG , " parseWithMoshi cost ${System .currentTimeMillis() - s} " )
241269 return list
242270}
0 commit comments