# Kotlin JSON wrapper for Jackson

**URL:** <https://discuss.kotlinlang.org/t/kotlin-json-wrapper-for-jackson/604>\
**Category:** Uncategorized\
**Created:** [April 6, 2014, 11:17pm UTC](https://discuss.kotlinlang.org/t/kotlin-json-wrapper-for-jackson/604 "2014-04-06T23:17:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrewoma](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@andrewoma](https://discuss.kotlinlang.org/u/andrewoma)\
**Post date:** [April 6, 2014, 11:17pm UTC](https://discuss.kotlinlang.org/t/kotlin-json-wrapper-for-jackson/604/1 "2014-04-06T23:17:57Z")

</div>

Greeting all,

I’ve done a proof of concept JSON wrapper for Jackson based on Play’s JSON support and ended up with: [https://github.com/andrewoma/kson](https://github.com/andrewoma/kson)

It’s the result of an afternoon’s hacking, so it’s definitely not a production library. However, it seemed promising, so thought I’d publish it as is.

Here’s an example from [https://github.com/andrewoma/kson/blob/master/kson/src/test/java/com/github/andrewoma/kson/JsValueTest.kt](https://github.com/andrewoma/kson/blob/master/kson/src/test/java/com/github/andrewoma/kson/JsValueTest.kt)

```
val value = JsObject(

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;"firstName" to "Andrew".json,

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;"lastName" to "O'Malley".json,

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;"age" to 21.json,

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;"adult" to true.json,

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;"address" to JsObject(

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;"number" to "88".json,

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;"street" to "Chapel Street".json,

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;"suburb" to "Windsor".json,

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;"state" to "VIC".json,

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;"postCode" to "3181".json

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;),

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;"pets" to JsArray(listOf(

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;JsObject(

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;"kind" to "dog".json,

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;"name" to "Rover".json

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;),

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;JsObject(

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;"kind" to "cat".json,

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;"name" to "Kitty".json

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;)

 &nbsp;&nbsp;)
```

I’m still keen to experiment with some kind of builder syntax (or hear of other experiments - I know about klaxon).

A question for the Kotlin devs: is the conversion operator still planned for Kotlin? See [http://youtrack.jetbrains.com/issue/KT-1752#comment=27-318887](http://youtrack.jetbrains.com/issue/KT-1752#comment=27-318887)

If feel uneasy adding extension functions and properties to base types like String. A conversion operator (with the correct precedence) would allow things like the following without possible namespace clashes:

```kotlin
fun String.convert() : JsValue = JsString(this)
```

```kotlin

```

---

<div class="post-metadata">

**Author:** ![abreslav](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abreslav/32/8279_2.png) [@abreslav](https://discuss.kotlinlang.org/u/abreslav)\
**Post date:** [April 7, 2014, 6:40am UTC](https://discuss.kotlinlang.org/t/kotlin-json-wrapper-for-jackson/604/2 "2014-04-07T06:40:19Z")

</div>

Minor suggestions:

- JsArray could take a vararg parameter, not a list
- Maybe shorten ".json" to ".js"

The conversion operator we are not yet sure about. I'd say in this case having a ".js" suffix would be nearly the same as the conversion operator

---

<div class="post-metadata">

**Author:** ![andrewoma](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@andrewoma](https://discuss.kotlinlang.org/u/andrewoma)\
**Post date:** [April 7, 2014, 7:56am UTC](https://discuss.kotlinlang.org/t/kotlin-json-wrapper-for-jackson/604/3 "2014-04-07T07:56:15Z")

</div>

Hi Andrey,

Thanks for the feedback and update on the conversion operator status.

I actually tried vararg for JsArray first and it does make the example above prettier.

However, it means using JsArray(\*list.copyToArrary()) when using it for non-builder use-cases and I figure these would be the norm for arrays. I guess I’ll add a function to support both styles.

Off-topic: I think you made a great call to drop full-blown pattern matching in Kotlin. I ported some of the serialization code from Scala that used pattern matching extensively and I think the Kotlin port is more readable without it.

Cheers,  
Andrew

---

<div class="post-metadata">

**Author:** ![abreslav](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abreslav/32/8279_2.png) [@abreslav](https://discuss.kotlinlang.org/u/abreslav)\
**Post date:** [April 7, 2014, 9:34am UTC](https://discuss.kotlinlang.org/t/kotlin-json-wrapper-for-jackson/604/4 "2014-04-07T09:34:32Z")

</div>

> However, it means using JsArray(\*list.copyToArrary()) when using it for non-builder use-cases and I figure these would be the norm for arrays. I guess I'll add a function to support both styles.

Yes, I think you can provide two overloaded versions to avoid this problem.

> Off-topic: I think you made a great call to drop full-blown pattern matching in Kotlin. I ported some of the serialization code from Scala that used pattern matching extensively and I think the Kotlin port is more readable without it.

Thanks for the feedback! Could you provide us with a side-by-side Scala and Kotlin code for comparison?

---

<div class="post-metadata">

**Author:** ![andrewoma](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@andrewoma](https://discuss.kotlinlang.org/u/andrewoma)\
**Post date:** [April 7, 2014, 10:21am UTC](https://discuss.kotlinlang.org/t/kotlin-json-wrapper-for-jackson/604/5 "2014-04-07T10:21:33Z")

</div>

Hi Andrey,

> However, it means using JsArray(\*list.copyToArrary()) when using it for non-builder use-cases and I figure these would be the norm for arrays. I guess I'll add a function to support both styles.
> 
> Yes, I think you can provide two overloaded versions to avoid this problem.

Yep, I've provided "secondary constructors" via functions as follows:

```kotlin
public fun JsArray(vararg values: JsValue): JsValue = JsArray(values.toList())
public data class JsArray(val values: List<JsValue>) : JsValue()
```

I’ve also renamed .json to .js and agree it’s better (and committed to github). So the current version looks like this:

```kotlin
    val value = JsObject(
           "firstName" to "Andrew".js,
           "lastName" to "O'Malley".js,
           "age" to 21.js,
           "adult" to true.js,
           "address" to JsObject(
                   "number" to "88".js,
                   "street" to "Chapel Street".js,
                   "suburb" to "Windsor".js,
                   "state" to "VIC".js,
                   "postCode" to "3181".js
           ),
           "pets" to JsArray(
                   JsObject(
                           "kind" to "dog".js,
                           "name" to "Rover".js
                   ),
                   JsObject(
                           "kind" to "cat".js,
                           "name" to "Kitty".js
                   )
           )
    )
 
```

> Off-topic: I think you made a great call to drop full-blown pattern matching in Kotlin. I ported some of the serialization code from Scala that used pattern matching extensively and I think the Kotlin port is more readable without it.
> 
> Thanks for the feedback! Could you provide us with a side-by-side Scala and Kotlin code for comparison?

Kotlin: [https://github.com/andrewoma/kson/blob/master/kson/src/main/java/com/github/andrewoma/kson/KsonModule.kt](https://github.com/andrewoma/kson/blob/master/kson/src/main/java/com/github/andrewoma/kson/KsonModule.kt) Scala: [https://github.com/playframework/playframework/blob/master/framework/src/play-json/src/main/scala/play/api/libs/json/JsValue.scala](https://github.com/playframework/playframework/blob/master/framework/src/play-json/src/main/scala/play/api/libs/json/JsValue.scala)

Compare the deserialize methods:

- The cool thing is both are completely type safe and no casting is required
- When porting, I found the Scala one confusing to unravel:
  - It matches on the same token multiple times (for expected and error cases)
  - It destructures lists as part of the matching, effectively popping off the stack
  - The function return is combination of matching plus guards, plus defaulting
  - The matching introduces a bunch of new variable names in the match, but the Kotlin version can just use the same variable (automatically cast)

Naturally, I’m biased as I ported the Kotlin one, but I find it easier to follow without all the matching.

Cheers,  
Andrew

---

<div class="post-metadata">

**Author:** ![abreslav](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abreslav/32/8279_2.png) [@abreslav](https://discuss.kotlinlang.org/u/abreslav)\
**Post date:** [April 7, 2014, 10:36am UTC](https://discuss.kotlinlang.org/t/kotlin-json-wrapper-for-jackson/604/6 "2014-04-07T10:36:45Z")

</div>

Thanks again

---

<div class="post-metadata">

**Author:** ![bashor](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/bashor/32/1431_2.png) [@bashor](https://discuss.kotlinlang.org/u/bashor)\
**Post date:** [April 7, 2014, 11:49am UTC](https://discuss.kotlinlang.org/t/kotlin-json-wrapper-for-jackson/604/7 "2014-04-07T11:49:59Z")

</div>

Probably [this blogpost](http://hadihariri.com/2013/05/29/creating-builders-in-kotlin-the-results-of-the-kotlin-workshop/) will be interesting for You, if you didn't see it already.

---

<div class="post-metadata">

**Author:** ![andrewoma](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@andrewoma](https://discuss.kotlinlang.org/u/andrewoma)\
**Post date:** [April 7, 2014, 1:33pm UTC](https://discuss.kotlinlang.org/t/kotlin-json-wrapper-for-jackson/604/8 "2014-04-07T13:33:40Z")

</div>

Thanks for that - I hadn't seen it. There's some interesting ideas in there.

I might try to reproduce a couple and see how they fare.

Some certainly look cleaner than my experiment, but I have some doubts as to whether they guarantee integrity, allow traversal, compose etc.

Cheers,  
Andrew
