Skip to content

iantabolt/json2s

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

51 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

json2s

Generate scala case classes from an example json object or an array of several example objects. Extensions may possibly include xml or similar other input types.

The root of the project contains a play web app to demo the functionality but the actual implementation can be found in the "core" module (it is pretty small).

The app can be viewed at https://json2s.herokuapp.com/

Motivation

Many scala Json libraries allow for json => case class conversion like so:

scala> import org.json4s._
scala> import org.json4s.native.JsonParser.parse
scala> implicit val formats = DefaultFormats // Brings in default date formats etc.
scala> case class Child(name: String, age: Int, birthdate: Option[java.util.Date])
scala> case class Address(street: String, city: String)
scala> case class Person(name: String, address: Address, children: List[Child])
scala> val json = parse("""
         { "name": "joe",
           "address": {
             "street": "Bulevard",
             "city": "Helsinki"
           },
           "children": [
             {
               "name": "Mary",
               "age": 5,
               "birthdate": "2004-09-04T18:06:22Z"
             },
             {
               "name": "Mazy",
               "age": 3
             }
           ]
         }
       """)

scala> json.extract[Person]
res0: Person = Person(joe,Address(Bulevard,Helsinki),List(Child(Mary,5,Some(Sat Sep 04 18:06:22 EEST 2004)), Child(Mazy,3,None)))

(taken from http://json4s.org/#extracting-values)

This makes for seamless access of json data through scala code. This is a huge step up from manually mapping json objects to classes, but there is still some tedious work to be done. Namely, creating the case classes to correspond to the json objects (in the above example, Child, Address, and Person). This can get quite difficult if you need to handle a large json object (ie from an API response), since if even one field or type is wrong, the extract will fail.

This project will attempt to automatically create the case classes for you so that, in combination with extract, any json API that provides example output can be quickly wrapped in awesome scala code.

Example basic functionality

scala> import lt.tabo.json2s.JsonToScala
scala> JsonToScala("""
          { "name": "joe",
            "address": {
              "street": "Bulevard",
              "city": "Helsinki"
            },
            "children": [
              {
                "name": "Mary",
                "age": 5,
                "birthdate": "2004-09-04T18:06:22Z"
              },
              {
                "name": "Mazy",
                "age": 3
              }
            ]
          }
        """, "Person")
res0: String =
case class Address(street: String, city: String)
case class Child(age: Int, name: String, birthdate: Option[java.util.Date])
case class Person(name: String, address: Address, children: List[Child])

Features

  • handles java.util.Date, more to come possibly?
  • convert array names to singular class names (eg children: List[Child])
  • handle names with invalid scala characters by putting them in backticks

TODO

  • add documentation, tests

About

Generate scala case classes from an example json object.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published