#Json Parsing on Kotlin

1 messages · Page 1 of 1 (latest)

dry garden
#

How can I parse json on kotlin? I've seen some libraries that can do it, but they parse to Java types, meaning that I would have to use java.lang.*, instead of kotlin.*, which doesn't look right to me.
Is there a kotlin focused library to parse json?

sudden bridgeBOT
#

<@&1008423204219531294> please have a look, thanks.

sudden bridgeBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

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.

sudden torrent
#

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

dry garden
#

Can use what...

sudden torrent
#

Also what is wrong with using java types?

sudden torrent
#

Json serialization/deserialiaztion libraries

dry garden
thick fog
#

Want to try mine?

sudden torrent
dry garden
#

But like, what should I use? I'm just doing something like a discord bot

thick fog
#

and can you give an example of some json you want to interpret?

dry garden
#

the one with pom.xml

dry garden
thick fog
#

replace the actual words with abc, def, etc?

dry garden
#
{
  "botToken": "I'm not saying that",
  "clientId": "also not saying that",
  "clientSecret": "no.",
}
sudden bridgeBOT
thick fog
#

gotcha

#

one second

#

(havent used kotlin in a bit)

dry garden
#

already have a config class.

#

I don't know how to parse it

thick fog
#
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))
}
sudden bridgeBOT
thick fog
#
    <dependency>
      <groupId>dev.mccue</groupId>
      <artifactId>json</artifactId>
      <version>0.1.2</version>
    </dependency>
sudden bridgeBOT
dry garden
thick fog
#

its my library

#

i am right here

#

you get personal support

dry garden
#

then why isn't it 1.0 yet?

sudden torrent
#

xd

thick fog
#

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

dry garden
#

...

dry garden
thick fog
#

🤷‍♂️

sudden torrent
#

@thick fog just show simple example of deserialization a config

sudden torrent
#

oh im blind

dry garden
#

ok, it worked

#

just dunno if it's reliable though

sterile imp
#

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

thick fog
#

and if you do happen to run into a bug, just lmk

#

i'll fix it

sterile imp
#

and regardless, you should deserialize jsons into your own types, which makes the question even weirder

thick fog
#

i mean - if it works now its reliable for your input

#

its not going to explode randomly