Skip to content

Commit fed6a5a

Browse files
committed
Fix integer gson parsing
1 parent 3f19f5b commit fed6a5a

File tree

4 files changed

+79
-3
lines changed

4 files changed

+79
-3
lines changed

src/SDK/Language/Android.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@ public function getFiles()
140140
'template' => '/android/library/src/main/java/io/appwrite/extensions/CollectionExtensions.kt.twig',
141141
'minify' => false,
142142
],
143+
[
144+
'scope' => 'default',
145+
'destination' => '/library/src/main/java/{{ sdk.namespace | caseSlash }}/json/PreciseNumberAdapter.kt',
146+
'template' => '/android/library/src/main/java/io/appwrite/json/PreciseNumberAdapter.kt.twig',
147+
'minify' => false,
148+
],
143149
[
144150
'scope' => 'default',
145151
'destination' => '/library/src/main/java/{{ sdk.namespace | caseSlash }}/models/RealtimeModels.kt',

src/SDK/Language/Kotlin.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public function getTypeName($type)
9797
{
9898
switch ($type) {
9999
case self::TYPE_INTEGER:
100+
return 'Long';
100101
case self::TYPE_NUMBER:
101102
return 'Double';
102103
case self::TYPE_STRING:

templates/android/library/src/main/java/io/appwrite/Client.kt.twig

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

33
import android.content.Context
44
import android.content.pm.PackageManager
5-
import com.google.gson.Gson
5+
import com.google.gson.GsonBuilder
6+
import com.google.gson.reflect.TypeToken
67
import io.appwrite.appwrite.BuildConfig
78
import {{ sdk.namespace | caseDot }}.exceptions.{{ spec.title | caseUcfirst }}Exception
89
import {{ sdk.namespace | caseDot }}.extensions.fromJson
10+
import {{ sdk.namespace | caseDot }}.json.PreciseNumberAdapter
911
import kotlinx.coroutines.CoroutineScope
1012
import kotlinx.coroutines.Dispatchers
1113
import kotlinx.coroutines.Job
@@ -45,7 +47,10 @@ class Client @JvmOverloads constructor(
4547

4648
private val job = Job()
4749

48-
private val gson = Gson()
50+
private val gson = GsonBuilder().registerTypeAdapter(
51+
object : TypeToken<Map<String, Any>>(){}.type,
52+
PreciseNumberAdapter()
53+
).create()
4954

5055
lateinit var http: OkHttpClient
5156

@@ -358,7 +363,7 @@ class Client @JvmOverloads constructor(
358363
}
359364
val map = gson.fromJson<Map<String, Any>>(
360365
body,
361-
HashMap::class.java
366+
object : TypeToken<Map<String, Any>>(){}.type
362367
)
363368
it.resume(
364369
convert?.invoke(map) ?: map as T
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package {{ sdk.namespace | caseDot }}.json
2+
3+
import com.google.gson.Gson
4+
import com.google.gson.TypeAdapter
5+
import com.google.gson.stream.JsonReader
6+
import com.google.gson.stream.JsonToken.*
7+
import com.google.gson.stream.JsonWriter
8+
import java.io.IOException
9+
10+
internal class PreciseNumberAdapter : TypeAdapter<Any?>() {
11+
12+
private val delegate = Gson()
13+
.getAdapter(Any::class.java)
14+
15+
@Throws(IOException::class)
16+
override fun write(out: JsonWriter?, value: Any?) {
17+
delegate.write(out, value)
18+
}
19+
20+
@Throws(IOException::class)
21+
override fun read(input: JsonReader): Any? {
22+
return when (input.peek()) {
23+
BEGIN_ARRAY -> {
24+
val list = mutableListOf<Any?>()
25+
input.beginArray()
26+
while (input.hasNext()) {
27+
list.add(read(input))
28+
}
29+
input.endArray()
30+
list
31+
}
32+
BEGIN_OBJECT -> {
33+
val map = mutableMapOf<String, Any?>()
34+
input.beginObject()
35+
while (input.hasNext()) {
36+
map[input.nextName()] = read(input)
37+
}
38+
input.endObject()
39+
map
40+
}
41+
STRING -> {
42+
input.nextString()
43+
}
44+
NUMBER -> {
45+
val numberString = input.nextString()
46+
if (numberString.indexOf('.') != -1) {
47+
numberString.toDouble()
48+
} else {
49+
numberString.toLong()
50+
}
51+
}
52+
BOOLEAN -> {
53+
input.nextBoolean()
54+
}
55+
NULL -> {
56+
input.nextNull()
57+
null
58+
}
59+
else -> {
60+
throw IllegalStateException()
61+
}
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)