Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update/decode maps support #5

Merged
merged 5 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions config.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ext {
def major = 0
def minor = 0
def patch = 5
def patch = 6

buildConfig = [
"minSdk" : 21,
Expand All @@ -14,4 +14,4 @@ ext {
"version" : "$major.$minor.$patch",
"versionCode": major * 100 * 100 + minor * 100 + patch
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import com.infinum.halley.core.serializers.hal.models.HalResource
import com.infinum.halley.core.serializers.shared.delegates.DeserializerDelegate
import kotlin.reflect.KClass
import kotlin.reflect.KProperty1
import kotlinx.serialization.SerializationException
import kotlinx.serialization.builtins.ArraySerializer
import kotlinx.serialization.builtins.ListSerializer
import kotlinx.serialization.builtins.MapSerializer
import kotlinx.serialization.builtins.SetSerializer
import kotlinx.serialization.json.JsonDecoder
import kotlinx.serialization.json.JsonNull
Expand Down Expand Up @@ -53,6 +55,7 @@ internal class PrimitiveDeserializerDelegate<T : HalResource>(
result,
jsonObject[name]?.jsonPrimitive?.contentOrNull
)
Map::class -> decodeMap()
else -> if (property.isCollection()) {
decodeCollection()
} else {
Expand Down Expand Up @@ -106,4 +109,19 @@ internal class PrimitiveDeserializerDelegate<T : HalResource>(

property.setField(result, value)
}

private fun decodeMap() {
val mapType = property.returnType
val keyType = mapType.arguments[0].type ?: throw SerializationException("Cannot determine map key type")
val valueType = mapType.arguments[1].type ?: throw SerializationException("Cannot determine map value type")

val value = decoder.json.decodeFromJsonElement(
MapSerializer(
decoder.serializersModule.serializer(keyType),
decoder.serializersModule.serializer(valueType)
),
jsonObject[name]?.jsonObject ?: JsonObject(emptyMap())
)
property.setField(result, value)
}
}
4 changes: 3 additions & 1 deletion halley-core/src/main/resources/META-INF/proguard/halley.pro
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,6 @@
-dontwarn org.bouncycastle.**
-dontwarn org.openjsse.**
-dontwarn org.joda.**
-dontwarn org.slf4j.**
-dontwarn org.slf4j.**

-keep class * extends com.infinum.halley.core.serializers.hal.models.HalResource { *; }
18 changes: 9 additions & 9 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
}
dependencies {
classpath libs.tools.gradle
// classpath libs.library.plugin
classpath libs.library.plugin
}
}

Expand All @@ -15,7 +15,7 @@ plugins {
alias libs.plugins.kotlin.android
}

// apply plugin: "com.infinum.halley.plugin"
apply plugin: "com.infinum.halley.plugin"

android {
compileSdkVersion buildConfig.compileSdk
Expand Down Expand Up @@ -70,13 +70,13 @@ android {
}

dependencies {
// implementation libs.library.retrofit
// implementation libs.library.ktor
implementation project(":halley-core")
implementation project(":halley-retrofit")
implementation project(":halley-ktor")
implementation libs.library.retrofit
implementation libs.library.ktor
// implementation project(":halley-core")
// implementation project(":halley-retrofit")
// implementation project(":halley-ktor")

// implementation libs.kotlin.core
implementation libs.kotlin.core
implementation libs.bundles.androidx
implementation libs.material

Expand All @@ -88,4 +88,4 @@ dependencies {

implementation libs.bundles.coroutines
implementation libs.bundles.rxjava
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ data class ProfileResource(
val user: UserResource? = null,

@Transient
val age: Int = 40
val age: Int = 40,

@SerialName(value = "favouriteMeals")
val favouriteMeals: Map<String, String>? = null,

@SerialName(value = "categorizedAnimals")
val categorizedAnimals: Map<String, List<AnimalResource>>? = null

) : HalResource
// CPD-ON
25 changes: 24 additions & 1 deletion sample/src/main/resources/profile.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,29 @@
},
"name": "Niko",
"timezone": "Europe/Zagreb",
"favouriteMeals": {
"breakfast": "pancakes",
"lunch": "pasta",
"dinner": "lasagna"
},
"categorizedAnimals": {
"mammals": [
{
"name": "Lion",
"age": 3
},
{
"name": "Tiger",
"type": "2"
}
],
"birds": [
{
"name": "Eagle",
"age": 5
}
]
},
"_links": {
"self": {
"href": "http://localhost:8080/api/Profile/self"
Expand All @@ -22,4 +45,4 @@
"href": "http://localhost:8080/api/Profile/block"
}
}
}
}
Loading