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.

How do I represent arbitrary data?

See original GitHub issue

Suppose I need to parse JSON and the only sample I’ve seen is { "thing": {} }.

I’d like to let the type of thing be a dynamic object value, or even JsonValue, but I am not sure how to accomplish this.

import com.beust.klaxon.*

data class TopLevel(val thing: Thing)
data class Thing()

val klaxon = Klaxon()
klaxon.parse<TopLevel>("{ \"thing\": {} }")

This doesn’t work because data classes cannot have empty constructors, and I really want to allow TopLevel.thing to be a dynamic object.

I tried data class TopLevel(val thing: JsonValue) and it didn’t consider that a suitable constructor for the data.

Any suggestions?


P.S. I tried to join #klaxon on Slack but a @jetbrains email was required.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
dvdsglcommented, May 6, 2018

This works:

import com.beust.klaxon.*

data class Top(val thing: JsonObject)

val convertJsonObject = object: Converter {
    override fun canConvert(cls: Class<*>) = cls == JsonObject::class.java
    override fun toJson(value: Any): String = (value as JsonObject).toJsonString()
    override fun fromJson(jv: JsonValue): Any = jv.inside
}

fun main(args: Array<String>) {
	val klaxon = Klaxon().converter(convertJsonObject)
	val top = klaxon.parse<Top>("""{ "thing": { "name": 6 } }""")!!
	val json = klaxon.toJsonString(top)
	print(json)
}
0reactions
cbeustcommented, May 6, 2018

Oh nice find!

Read more comments on GitHub >

github_iconTop Results From Across the Web

3 Choosing how to represent data
Tables, charts and graphs are all ways of representing data, and they can be used for two broad purposes. The first is to...
Read more >
Self-Ref - Introduction to Arbitrary-Sized Data - YouTube
04a - Self Reference. Self-Ref - Introduction to Arbitrary -Sized Data. 9.9K views 9 years ago. Systematic Program Design.
Read more >
Arbitrary unit - Wikipedia
In science and technology, an arbitrary unit or procedure defined unit (p.d.u.) is a relative unit of measurement to show the ratio of...
Read more >
II Arbitrarily Large Data - How to Design Programs
If a problem statement is about information of arbitrary size, you need a self-referential data definition to represent it.
Read more >
Which type do I use to represent an arbitrary blob in Java?
At this point, the code should be agnostic as to what's in the data. What is an appropriate type for the data field?...
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