&%@#$^_-=+?/<>\|~`*() how to properly es...
# serialization
h
&%@#$^_-=+?/<>\|~`*() how to properly escape all this symbols in kotlinx.serialization ?
v
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
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
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
so compose navigation is causing this not serialization?
v
I have no idea about compose navigation or what it does. But I guess so, yes.
h
I am using
Json.encodeToString
to encode the model. I guess this is the problem then
v
No, that gives you a proper JSON String. The problem is what you then do with it.
h
I decode it with
Json.decodeFromString
and send as parameter to composable
v
Yeah, but check what you give in to
decodeFromString
. Where you get the argument from is where it most likely breaks.
s
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.
181 Views