Skip to content

Commit 55c40dd

Browse files
committed
JSON improvements for Kotlin
1 parent 93e1bd0 commit 55c40dd

File tree

5 files changed

+21
-26
lines changed

5 files changed

+21
-26
lines changed

templates/android/library/src/main/java/io/package/json/PreciseNumberAdapter.kt.twig

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
package {{ sdk.namespace | caseDot }}.json
22

3-
import com.google.gson.Gson
43
import com.google.gson.TypeAdapter
54
import com.google.gson.stream.JsonReader
65
import com.google.gson.stream.JsonToken.*
76
import com.google.gson.stream.JsonWriter
7+
import {{ sdk.namespace | caseDot }}.extensions.gson
88
import java.io.IOException
99

1010
internal class PreciseNumberAdapter : TypeAdapter<Any?>() {
1111

12-
private val delegate = Gson()
13-
.getAdapter(Any::class.java)
12+
private val delegate = gson.getAdapter(Any::class.java)
1413

1514
@Throws(IOException::class)
1615
override fun write(out: JsonWriter?, value: Any?) {

templates/kotlin/build.gradle.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ dependencies {
3838
implementation("com.squareup.okhttp3:logging-interceptor")
3939
implementation("com.google.code.gson:gson:2.9.0")
4040

41-
testImplementation 'org.jetbrains.kotlin:kotlin-test-junit'
41+
testImplementation("org.jetbrains.kotlin:kotlin-test-junit")
4242
}
4343

4444
test {

templates/kotlin/src/main/kotlin/io/appwrite/Client.kt.twig

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package {{ sdk.namespace | caseDot }}
22

3-
import com.google.gson.GsonBuilder
4-
import com.google.gson.reflect.TypeToken
53
import {{ sdk.namespace | caseDot }}.exceptions.{{ spec.title | caseUcfirst }}Exception
64
import {{ sdk.namespace | caseDot }}.extensions.fromJson
7-
import {{ sdk.namespace | caseDot }}.json.PreciseNumberAdapter
5+
import {{ sdk.namespace | caseDot }}.extensions.toJson
86
import {{ sdk.namespace | caseDot }}.models.InputFile
97
import {{ sdk.namespace | caseDot }}.models.UploadProgress
108
import kotlinx.coroutines.CoroutineScope
@@ -47,11 +45,6 @@ class Client @JvmOverloads constructor(
4745

4846
private val job = Job()
4947

50-
private val gson = GsonBuilder().registerTypeAdapter(
51-
object : TypeToken<Map<String, Any>>(){}.type,
52-
PreciseNumberAdapter()
53-
).create()
54-
5548
lateinit var http: OkHttpClient
5649

5750
private val headers: MutableMap<String, String>
@@ -252,7 +245,8 @@ class Client @JvmOverloads constructor(
252245
}
253246
builder.build()
254247
} else {
255-
gson.toJson(filteredParams)
248+
filteredParams
249+
.toJson()
256250
.toRequestBody("application/json".toMediaType())
257251
}
258252

@@ -422,10 +416,8 @@ class Client @JvmOverloads constructor(
422416
.use(BufferedReader::readText)
423417

424418
val error = if (response.headers["content-type"]?.contains("application/json") == true) {
425-
val map = gson.fromJson<Map<String, Any>>(
426-
body,
427-
object : TypeToken<Map<String, Any>>(){}.type
428-
)
419+
val map = body.fromJson<Map<String, Any>>()
420+
429421
{{ spec.title | caseUcfirst }}Exception(
430422
map["message"] as? String ?: "",
431423
(map["code"] as Number).toInt(),
@@ -464,10 +456,7 @@ class Client @JvmOverloads constructor(
464456
it.resume(true as T)
465457
return
466458
}
467-
val map = gson.fromJson<Map<String, Any>>(
468-
body,
469-
object : TypeToken<Map<String, Any>>(){}.type
470-
)
459+
val map = body.fromJson<Map<String, Any>>()
471460
it.resume(
472461
converter?.invoke(map) ?: map as T
473462
)

templates/kotlin/src/main/kotlin/io/appwrite/extensions/JsonExtensions.kt.twig

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
package {{ sdk.namespace | caseDot }}.extensions
22

33
import com.google.gson.Gson
4-
5-
val gson = Gson()
4+
import com.google.gson.GsonBuilder
5+
import com.google.gson.reflect.TypeToken
6+
import {{ sdk.namespace | caseDot }}.json.PreciseNumberAdapter
7+
8+
val gson: Gson = GsonBuilder()
9+
.registerTypeAdapter(
10+
object : TypeToken<Map<String, Any>>() {}.type,
11+
PreciseNumberAdapter()
12+
)
13+
.create()
614

715
fun Any.toJson(): String =
816
gson.toJson(this)

templates/kotlin/src/main/kotlin/io/appwrite/json/PreciseNumberAdapter.kt.twig

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
package {{ sdk.namespace | caseDot }}.json
22

3-
import com.google.gson.Gson
43
import com.google.gson.TypeAdapter
54
import com.google.gson.stream.JsonReader
65
import com.google.gson.stream.JsonToken.*
76
import com.google.gson.stream.JsonWriter
7+
import {{ sdk.namespace | caseDot }}.extensions.gson
88
import java.io.IOException
99

1010
internal class PreciseNumberAdapter : TypeAdapter<Any?>() {
1111

12-
private val delegate = Gson()
13-
.getAdapter(Any::class.java)
12+
private val delegate = gson.getAdapter(Any::class.java)
1413

1514
@Throws(IOException::class)
1615
override fun write(out: JsonWriter?, value: Any?) {

0 commit comments

Comments
 (0)