question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add Kotlin Serialization JSON compatibility

See original GitHub issue

The kotlinx.serialization module for JSON doesn’t have easy support or tutorials in Javalin docs.

If you could, please add it for integration with this library.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:20 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
tipsycommented, Jan 10, 2021

Sorry @Freezystem, I completely missed your reply!

You can read a bit about extension functions here: https://kotlinlang.org/docs/reference/extensions.html. You would have to do something like:

fun Context.kotlinxJson() {

}

With your desired signatures.

Then you can import these when you want to use it, and call them like ctx.kotlinxJson().

1reaction
rbygravecommented, Oct 15, 2020

In terms of writing a generic object to JSON with Gson or Jackson we see that they have options for using Class<?> and an additional option for using java.lang.reflect.Type (Gson) or for Jackson it’s own JavaType or TypeReference<?>.

Gson:

java.lang.String toJson​(java.lang.Object src)
java.lang.String toJson​(java.lang.Object src, java.lang.reflect.Type typeOfSrc)

https://www.javadoc.io/static/com.google.code.gson/gson/2.8.6/com.google.gson/com/google/gson/Gson.html#toJson(java.lang.Object,java.lang.reflect.Type)

Jackson:

ObjectWriter	writerFor(Class<?> rootType)
ObjectWriter	writerFor(JavaType rootType)
ObjectWriter	writerFor(TypeReference<?> rootType)

http://fasterxml.github.io/jackson-databind/javadoc/2.10/com/fasterxml/jackson/databind/ObjectMapper.html#writerFor-java.lang.Class-

Conceptually in Java land for Gson it would suggest having an additional/overloaded toJson() that took java.lang.reflect.Type - but that wouldn’t work for Jackson which has it’s own JavaType and TypeReference (but for myself I don’t think we need this for Jackson and it would be worth checking Gson - as in, is this more a kotlin/kotlinx.serialization issue/requirement? I should confirm when Jackson and Gson need generic type on serialisation to json).

Hmmm.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Serialization | Kotlin
kotlinx.serialization provides sets of libraries for all supported platforms – JVM, JavaScript, Native – and for various serialization formats – ...
Read more >
Add Kotlin Serialization JSON compatibility #1080 - GitHub
The kotlinx.serialization module for JSON doesn't have easy support or tutorials in Javalin docs. If you could, please add it for integration ...
Read more >
Using the Kotlin Serialization Library for Tough JSON ...
We use the Kotlin serialization library, because it's compatible with Kotlin, has a faster runtime than Gson or Moshi, and can run on...
Read more >
kotlinx.serialization 1.2 Released: High-Speed JSON ...
JSON serialization is faster than ever before. Version 1.2 is up to twice as fast as ... Kotlin 1.5 type system additions are...
Read more >
Understanding Kotlin data serialization - LogRocket Blog
Kotlin uses a process called data serialization to convert data into other compatible data formats. This process is important because it ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found