https://kotlinlang.org logo
#serialization
Title
# serialization
h

Hasan Nagizade

02/21/2023, 12:00 PM
&%@#$^_-=+?/<>\|~`*() how to properly escape all this symbols in kotlinx.serialization ?
v

Vampire

02/21/2023, 12:07 PM
You don't need to, it is more a question of the format you serialize to or from. If you for example serialize to or from XML, some of these need to be escaped, but you don't need to do anything for that, the concrete serialization implementation cares about that. If you send the serialized form via some means like in the URL of an HTTP request, or in the body of an HTTP request, or as content of a WSDL call or whatever, you have to make sure that on that level the characters are encoded properly. Like with your
&
question before. You probably had it in the URL or body of a HTTP request and without proper escaping it was interpreted as separator between two parameters and thus your JSON ended right before the ampersand and thus you got an EOF in the middle of a String literal.
h

Hasan Nagizade

02/21/2023, 12:10 PM
I got that
&
inside a json.
UrlEncoder
helps to fix that but most of this symbols are not supported by
UrlEncoder
. I am getting exact same error for them. so in order to avoid problems in future I decided to find a way to escape all this now
the interesting part is this becomes problem when I send the string with symbols via model in jetpack compose navigation. in normal way serialization is not throwing any errors
v

Vampire

02/21/2023, 12:12 PM
As I said, it is not a question of escaping it in the JSON which does not have a problem with these characters. It is a problem of where else you use the JSON like when sending it over the wire.
h

Hasan Nagizade

02/21/2023, 12:13 PM
so compose navigation is causing this not serialization?
v

Vampire

02/21/2023, 12:14 PM
I have no idea about compose navigation or what it does. But I guess so, yes.
h

Hasan Nagizade

02/21/2023, 12:15 PM
I am using
Json.encodeToString
to encode the model. I guess this is the problem then
v

Vampire

02/21/2023, 12:20 PM
No, that gives you a proper JSON String. The problem is what you then do with it.
h

Hasan Nagizade

02/21/2023, 12:22 PM
I decode it with
Json.decodeFromString
and send as parameter to composable
v

Vampire

02/21/2023, 12:33 PM
Yeah, but check what you give in to
decodeFromString
. Where you get the argument from is where it most likely breaks.
s

Stylianos Gakis

03/20/2023, 12:20 PM
Interesting that you mention kotlinx.serialization + androidx.navigation, since there is exactly one library which does what you are talking about https://github.com/kiwicom/navigation-compose-typed I’d suggest if you haven’t solved this yet for yourself you can take a look at how it does it (by using a
<http://android.net|android.net>.Uri.Builder
as far as I can tell) and makes it work with kotlinx.serialization + androidx.navigation.
131 Views