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.

Use KotlinX instead of GDX for JSONs?

See original GitHub issue

Neither a bug report nor a feature request.

AFAICT, the GDX JSON tools used by JsonParser.kt work by creating new instances of classes, with zero arguments, and then assigning values to their members from the JSON file.

It was also designed only for Java, I believe, and does not support Kotlin-specific idioms/features.

This seems to create several limitations:

  • No support for data classes.
  • Requires default initializers for all properties. (Adds noise to code, leaves room for silent failure if the .JSON fails to define some required value.)
  • No support for immutable val properties. (Again, creates unnecessary room for silent failures.)
  • No/poor support for deserializing into interfaces/arg-requiring constructors. (E.G. Map<>, probably List<> and Array<>.)
  • Ugly SomeClass::class.java syntax.

For the IPC protocol in #5608, I decided to go with the official KotlinX serialization library as standards compliance is more important there, and it’s been great for (de)serializing objects with known structure.

KotlinX also describes itself as supporting a “lenient” deserialization mode, which looks like it should be largely compatible with the “Javascript” and “Minimal”-style unquoted JSON files currently parsed with GDX (and you can write custom deserializers too, though I found it a bit painful to do so):

https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/json.md

Might it be a good idea to switch JsonParser to use the official KotlinX JSON tools instead of GDX?

Comparisons

KotlinX:

data class TileSpec(
	val tileYield: Map<String, Int>,
	val someList: List<String>,
	val immutableNumber: Int
)

val tileSpec = Json().decodeFromString<TileSpec>(jsonString)

GDX:

class TileSpec() {
	var tileYield = HashMap<String, Int>()
	var someList = ArrayList<String>()
	var immutableNumber = 0
}

val tileSpec = Json().fromJson(TileSpec::class.java, jsonString)

IMO the biggest thing is (apparently) being forced to use mutables. Most stuff that gets loaded from JSONS is probably also stuff that shouldn’t ever change.


Some of much of this may just be incorrect usage on my part, but I’m not sure how much if any.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
yairm210commented, Nov 14, 2021

So yeah, this is a no-go in the core project from a cost benefit perspective

0reactions
will-cacommented, Nov 16, 2021

@logicminimal He said “cost-benefit”, so… The benefit’s also fairly small, just some minor syntactic and semantic QoL improvments. And in addition to the size, a lot of (if not most of) the existing classes would also have to be changed to make use of it, so the cost is pretty major if you’re not rounding your available resources up to .

Read more comments on GitHub >

github_iconTop Results From Across the Web

Kotlinx Json vs Gson. Let's look at how new Kotlin… - Medium
As you can see, gson library totally ignores your default values and uses 0 to complete missing primitive field and null instead of...
Read more >
Using libGDX with Kotlin
Kotlin is a modern statically typed JVM language from JetBrains, the creators of IntelliJ IDEA (Kotlin supports Eclipse too).
Read more >
parsing JSON created on python3 server side [beginner tutorial]
0:00 python3 syntax0:50 libGDX Kotlin syntaxtxt ... libGDX Kotlin client side: parsing JSON created on python3 server side [beginner ...
Read more >
KTX: Kotlin support for LibGDX applications
Based on libGDX. Extends a robust multi-platform game framework with highly modular and minimally opinionated Kotlin utilities.
Read more >
[Solved]-Decode JSON file with Kotlin vs Swift-kotlin
Related Query · Decode JSON file with Kotlin vs Swift · How get and use json from file with kotlin android? · Access...
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