Skip to content

Commit 7c02d10

Browse files
committed
json compare
1 parent d0195c3 commit 7c02d10

5 files changed

Lines changed: 254 additions & 10 deletions

File tree

imitate/build.gradle

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
plugins {
2+
id 'org.jetbrains.kotlin.plugin.serialization' version '2.2.10'
3+
}
4+
15
if (imitate_isApp.toBoolean()) {
26
apply plugin: 'com.android.application'
37
} else {
@@ -222,16 +226,23 @@ dependencies {
222226

223227
// from 2021
224228
implementation 'com.beust:klaxon:5.6'
225-
implementation 'com.alibaba:fastjson:2.0.57'
229+
// implementation 'com.alibaba:fastjson:2.0.57'
230+
implementation 'com.alibaba:fastjson:1.1.71.android'
226231
implementation 'com.google.code.gson:gson:2.13.1'
227232
implementation libs.dagger
228233
kapt libs.dagger.compiler
229234

235+
implementation("com.squareup.moshi:moshi:1.15.2") // 核心库
236+
implementation("com.squareup.moshi:moshi-kotlin:1.15.2") // Kotlin 扩展(支持数据类)
237+
kapt("com.squareup.moshi:moshi-kotlin-codegen:1.15.2") // 代码生成(替代反射)
238+
230239
// coroutines
231240
// 👇 依赖协程核心库
232241
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_coroutines"
233242
// 👇 依赖当前平台所对应的平台库
234243
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlin_coroutines_android"
235244

245+
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.9.0")
246+
236247
}
237248
apply from: file("../gradle/report_apk_size_after_package.gradle")

imitate/src/main/java/com/engineer/imitate/KotlinRootActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class KotlinRootActivity : AppCompatActivity() {
9191

9292
private fun autoStartPage() {
9393
val fragment: Fragment =
94-
ARouter.getInstance().build("/anim/fresco").navigation(this) as Fragment
94+
ARouter.getInstance().build("/anim/coroutines").navigation(this) as Fragment
9595
currentFragment = fragment
9696
viewBinding.contentKotlinRoot.content.visibility = View.VISIBLE
9797
viewBinding.contentKotlinRoot.index.visibility = View.GONE
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package com.engineer.imitate.model.java;
2+
3+
import java.util.List;
4+
5+
public class Schools {
6+
private int id = 0;
7+
private String province;
8+
private List<School> schoolList;
9+
private int type = 0;
10+
private String code = "aa";
11+
12+
public int getId() {
13+
return id;
14+
}
15+
16+
public void setId(int id) {
17+
this.id = id;
18+
}
19+
20+
public String getProvince() {
21+
return province;
22+
}
23+
24+
public void setProvince(String province) {
25+
this.province = province;
26+
}
27+
28+
public List<School> getSchoolList() {
29+
return schoolList;
30+
}
31+
32+
public void setSchoolList(List<School> schoolList) {
33+
this.schoolList = schoolList;
34+
}
35+
36+
public int getType() {
37+
return type;
38+
}
39+
40+
public void setType(int type) {
41+
this.type = type;
42+
}
43+
44+
public String getCode() {
45+
return code;
46+
}
47+
48+
public void setCode(String code) {
49+
this.code = code;
50+
}
51+
52+
@Override
53+
public String toString() {
54+
return "Schools(province=" + province + ", schoolList=" + schoolList + ")";
55+
}
56+
57+
58+
public static class School {
59+
60+
public static class Location {
61+
private float lat = 0f;
62+
private float lng = 0f;
63+
64+
public float getLat() {
65+
return lat;
66+
}
67+
68+
public void setLat(float lat) {
69+
this.lat = lat;
70+
}
71+
72+
public float getLng() {
73+
return lng;
74+
}
75+
76+
public void setLng(float lng) {
77+
this.lng = lng;
78+
}
79+
80+
@Override
81+
public String toString() {
82+
return "Location{" + "lat=" + lat + ", lng=" + lng + '}';
83+
}
84+
}
85+
86+
private String name;
87+
private String province;
88+
private String city;
89+
private String area;
90+
private String address;
91+
private Location location;
92+
93+
public String getName() {
94+
return name;
95+
}
96+
97+
public void setName(String name) {
98+
this.name = name;
99+
}
100+
101+
public String getProvince() {
102+
return province;
103+
}
104+
105+
public void setProvince(String province) {
106+
this.province = province;
107+
}
108+
109+
public String getCity() {
110+
return city;
111+
}
112+
113+
public void setCity(String city) {
114+
this.city = city;
115+
}
116+
117+
public String getArea() {
118+
return area;
119+
}
120+
121+
public void setArea(String area) {
122+
this.area = area;
123+
}
124+
125+
public String getAddress() {
126+
return address;
127+
}
128+
129+
public void setAddress(String address) {
130+
this.address = address;
131+
}
132+
133+
public Location getLocation() {
134+
return location;
135+
}
136+
137+
public void setLocation(Location location) {
138+
this.location = location;
139+
}
140+
141+
@Override
142+
public String toString() {
143+
return "School{" + "name='" + name + '\'' + ", province='" + province + '\'' + ", city='" + city + '\'' + ", area='" + area + '\'' + ", address='" + address + '\'' + ", location=" + location + '}';
144+
}
145+
}
146+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.engineer.imitate.model.sub
2+
3+
import android.util.Log
4+
import kotlinx.serialization.Serializable
5+
import kotlinx.serialization.json.Json
6+
7+
val json = Json {}
8+
9+
@Serializable
10+
class School {
11+
12+
@Serializable
13+
class Location {
14+
var lat = 0f
15+
var lng = 0f
16+
override fun toString(): String {
17+
return "Location{" + "lat=" + lat + ", lng=" + lng + '}'
18+
}
19+
}
20+
21+
var name: String? = null
22+
23+
var province: String? = null
24+
25+
var city: String? = null
26+
27+
var area: String? = null
28+
29+
var address: String? = null
30+
31+
var location: Location? = null
32+
33+
override fun toString(): String {
34+
return "School{" + "name='" + name + '\'' + ", province='" + province + '\'' + ", city='" + city + '\'' + ", area='" + area + '\'' + ", address='" + address + '\'' + ", location=" + location + '}'
35+
}
36+
}
37+
38+
@Serializable
39+
data class Schools(
40+
var id: Int = 0,
41+
var province: String? = null,
42+
var schoolList: List<School>? = null,
43+
var type: Int = 0,
44+
var code: String? = "aa"
45+
) {
46+
47+
48+
override fun toString(): String {
49+
return "Schools(province=$province, schoolList=$schoolList)"
50+
}
51+
}
52+
53+
fun parseWithKotlinxSerializable(jsonStr: String): List<Schools>? {
54+
val s = System.currentTimeMillis()
55+
56+
val list = json.decodeFromString<List<Schools>>(jsonStr)
57+
Log.e("Coroutines", "parseWithKotlinxSerializable cost ${System.currentTimeMillis() - s}")
58+
return list
59+
}

imitate/src/main/java/com/engineer/imitate/ui/fragments/CoroutinesPlayGround.kt

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import com.beust.klaxon.Klaxon
2020
import com.engineer.imitate.R
2121
import com.engineer.imitate.model.School
2222
import com.engineer.imitate.model.Schools
23+
import com.engineer.imitate.model.sub.parseWithKotlinxSerializable
2324
import com.engineer.imitate.room.SchoolRepository
2425
import com.engineer.imitate.util.IOTool
2526
import com.engineer.imitate.util.bind
@@ -29,6 +30,10 @@ import com.google.android.material.tabs.TabLayout
2930
import com.google.android.material.tabs.TabLayoutMediator
3031
import com.google.gson.Gson
3132
import 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
3237
import io.reactivex.Observable
3338
import io.reactivex.android.schedulers.AndroidSchedulers
3439
import 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

219229
data class Item(val title: String, val fragment: Fragment, val size: Int)
220230

221-
222231
private 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

229238
private 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

236252
private 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

Comments
 (0)