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.

DSL Builders only accept primitives, and are somewhat difficult to "build"

See original GitHub issue

Building JSON is a bit difficult with Klaxon, since the DSL for building them uses nesting to create complex objects. While that’s fine for Java, it would be more idiomatic to use a more “builder” style for creating JsonObjects.

This is how you make a JsonObject in Klaxon:

json {
  obj("colors" to 
    obj(
      "grey" to "#333",
      "red" to array(255, 0, 0), 
      "green" to array(0, 255, 0),
      "others" to obj(
        "white" to "#fff"
      )
    )
  )
}

This results in:

{
  "colors": {
    "grey": "#333",
    "red": [255, 0, 0],
    "green": [0, 255, 0],
    "others": {
      "white": "#fff"
    }
  }
}

The current method does not allow for any custom serialization (as far as I know?), and nests obj calls inside of parenthesis to create it’s own DSL. All of the builder code must be included on adjacent lines, and you can’t insert your own code between each line. This assumes that you have all of the information you need to build the JSON object before you decide to build it.

It might be beneficial to create a DSL that looks more like this:

json {
  obj("colors") {
    put("grey", "#333")
    putArray("red", listOf(255, 0, 0))
    putArray("green", listOf(0, 255, 0))
    obj("others") {
      put("white", "#fff")
    }
  }
}

This format allows arbitrary code to be ran between each call and lets you “build” a JSON object more programmatically. You can insert code between each line, making this a bit more Kotlin friendly.

Also, the ability to convert arbitrary object within the tree would also be great: Supplying a value outside of the range of primitive values could be converted to it’s JSON equivalent.

Thank you, Klaxon is great! ❤️

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
cbeustcommented, Aug 4, 2020

Fair point. I just implemented this, take a look and let me know if this addresses your suggestion:

https://github.com/cbeust/klaxon/commit/0179fedab54bb9f5824d5008d6f6faedcdcf17e1

0reactions
Totaluscommented, Apr 6, 2022

Could we also allow to use the obj() function without specifying a key so that we can populate the root fields of the object ?

This way the following expression could work:

json {
  obj {
    put("hello", "world")
  }
}

Resulting in:

{
   "hello": "world"
}

Currently there seem to be no easy way to do that. (See #316)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Bountysource
DSL Builders only accept primitives, and are somewhat difficult to "build "
Read more >
Easy JSON in Kotlin with a Type-Safe Builder DSL - Dev Genius
JSON DSL Syntax​​ Using the Kotlin DSL, we have operators for array and object and primitive creation (through string , int , long...
Read more >
Chapter 11. DSL construction - Kotlin in Action
In this chapter, we'll discuss how you can design expressive and idiomatic APIs for your Kotlin classes through the use of domain-specific languages...
Read more >
DSL Builders
DSL BUILDERS LLC. 100% VETERAN-OWNED. SERVICE-DISABLED VETERAN-CONTROLLED BUSINESS. SBA HUBZONE CERTIFICATION #41963. CCB# 192282. SITE BY DESIGNPOINT.
Read more >
Are the builder pattern and a DSL equivalent - or is one more ...
A DSL is a language for a specific domain i.e it has primitives and composition rules that allows you to talk about a...
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