similar to <@UBBFK40RZ> above I am a bit stumped w...
# serialization
d
similar to @Nikky above I am a bit stumped where to go next when an api response either empty object
{}
or the structure that my serialiser expects e.g.
{"id": "ABC123", "status": "STATUS"}
. I have tried a number of approaches that i’ve documented on this SO ticket; https://stackoverflow.com/questions/56541201/parsing-empty-object-with-kotlinx-serialization Would anyone be able to point out where I am going wrong?
l
@David I think you're supposed to use
encodeDefaults
:```Json(encodeDefaults = true).parse(YOUR_SERIALIZER, YOUR_STRING)```
d
Hey @Lawik ! Thanks for responding! I remember in the blur of things I tried adding this. Going to give it another go!
l
I just checked some of my code with a default value and I didn't use
encodeDefaults
, only annotated the property with
@Optional
Weird that it doesn't work for you 🤔
Are you sure
@Optional
isn't required anymore in 1.3.30?
d
I have a number of other API calls where the default value replacement for
@Optional
is working as expected (error like this aren’t thrown!) …I think what is different about this one is that the API returns a 200 with this empty object which is definitely not of the shape I’ve told it the response will be since all fields on the object are gone.
Are you sure
@Optional
isn’t required anymore in 1.3.30?
checks
Ah see the pinned message in this room from our buddies at JB.
✔️ 1
although, I just saw JW’s update below that. Checks retrofit..
phew, I’m ok
Tried with just
encodeDefaults=true
, no dice.
s
encodeDefaults
affects serialization, not deserialization
I've tried both 1.3.30 and 1.3.31 with 0.11.0 library and
Json.nonstrict.parse(GeoCoordinate.serializer(), """{}""")
works well for me. You can submit an issue with an attached sample project to reproduce, if you're sure that this is not a problem with some old Kotlin version stuck somewhere in dependencies
d
runs
gradlew dependecies
!
n
I found that i used kotlin 1.3.20 and that's why it broke
d
Ah, this looks suspicious;
Copy code
kotlinCompilerClasspath
\--- org.jetbrains.kotlin:kotlin-compiler-embeddable:1.3.21
     +--- org.jetbrains.kotlin:kotlin-stdlib:1.3.21
     |    +--- org.jetbrains.kotlin:kotlin-stdlib-common:1.3.21
     |    \--- org.jetbrains:annotations:13.0
     +--- org.jetbrains.kotlin:kotlin-script-runtime:1.3.21
     +--- org.jetbrains.kotlin:kotlin-reflect:1.3.21
     |    \--- org.jetbrains.kotlin:kotlin-stdlib:1.3.21 (*)
     \--- org.jetbrains.intellij.deps:trove4j:1.0.20181211

kotlinCompilerPluginClasspath
+--- org.jetbrains.kotlin:kotlin-serialization:1.3.21
+--- org.jetbrains.kotlin:kotlin-scripting-compiler-embeddable:1.3.21
\--- org.jetbrains.kotlin:kotlin-sam-with-receiver:1.3.21
so most of the runtime dependencies are 1.3.31 of transitively resolving from 1.3.30 -> 1.3.31 but the
compileOnly
and
compiler*
configurations are stuck somehow with 1.3.21
I think we’re onto it….
I have documented what was wrong on the SO by the way. This is now resolved. https://stackoverflow.com/questions/56541201/parsing-empty-object-with-kotlinx-serialization
👍 1