Sunday, 24 March 2013

The simplest dynamically typed json parsing with Dynamic in Scala 2.10

I love the way dynamically typed languages handle json or xml parsing. With Scala 2.10 we can do it using Dynamic trait. Here is a first draft.
Please feel free to copy/paste/reuse at your own risk.

1 comment:

  1. I wrote new library [dijon](https://github.com/pathikrit/dijon) - it written purely in less than 100 lines of dependency free Scala. It uses Scala dynamic types too e.g.

    val age = 9
    val rick = json"""{ "name": "rick", "age": $age}"""
    assert(rick.age == 9)
    rick.age = if (rick.age.as[Double].get > 18) "adult" else "youth"
    rick.address = `{}`
    rick.address.city = "Seattle"
    rick.address.isMain = true
    assert(rick == json"""{ "name": "rick", "age": "youth", "address": {"city": "Seattle", "isMain": true}}""")

    ReplyDelete