#Json Parsing on Kotlin
1 messages · Page 1 of 1 (latest)
<@&1008423204219531294> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
You can use ktor-serialization-kotlinx-json if you don't want to use jackson or gson
But if you are using kotlin jvm just use jackson
Can use what...
Also what is wrong with using java types?
what are those?
Json serialization/deserialiaztion libraries
boring to type and not 100% reliable
Want to try mine?

But like, what should I use? I'm just doing something like a discord bot
are you using maven or gradle
and can you give an example of some json you want to interpret?
erm... no? it has config stuff
replace the actual words with abc, def, etc?
{
"botToken": "I'm not saying that",
"clientId": "also not saying that",
"clientSecret": "no.",
}
Detected code, here are some useful tools:
import dev.mccue.json.Json
import dev.mccue.json.JsonDecoder
data class Config(
val botToken: String,
val clientId: String,
val clientSecret: String
) {
companion object {
fun fromJson(json: Json): Config {
return Config(
JsonDecoder.field(json, "botToken", { JsonDecoder.string(it) }),
JsonDecoder.field(json, "clientId", { JsonDecoder.string(it) }),
JsonDecoder.field(json, "clientSecret", { JsonDecoder.string(it) }),
)
}
}
}
fun main(args: Array<String>) {
val example = """{
"botToken": "I'm not saying that",
"clientId": "also not saying that",
"clientSecret": "no.",
}"""
val json = Json.readString(example);
println(json)
println(Config.fromJson(json))
}
Detected code, here are some useful tools:
<dependency>
<groupId>dev.mccue</groupId>
<artifactId>json</artifactId>
<version>0.1.2</version>
</dependency>
Detected code, here are some useful tools:
not even 1.0 though...
then why isn't it 1.0 yet?
xd
because im still working on it, changing it based on feedback of folks trying it
the more folks try it the more confident i am in some of the choices
which is exactly what I am trying to do here
the important part - the json parsing - does work
I don't have another way to figure out if I have "the right api" than asking strangers to try it though
you would be the first person to use it from Kotlin
...
you're not helping yourself there
🤷♂️
@thick fog just show simple example of deserialization a config
i have
oh im blind
looks pretty simple
which classes are you trying to parse into so that you are using java.lang
because kotlin provides type aliases for a lot of stuff
strings, maps, etc
it's all in kotlin
and if you run kotlin-jvm, it just gets hooked into java types
I didn't put if (Math.random() < 0.1) { fuck you } in it
and if you do happen to run into a bug, just lmk
i'll fix it
and regardless, you should deserialize jsons into your own types, which makes the question even weirder
what?