Skip to content

Instantly share code, notes, and snippets.

@kulikov
Created January 30, 2014 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kulikov/8713645 to your computer and use it in GitHub Desktop.
Save kulikov/8713645 to your computer and use it in GitHub Desktop.
package com.digsolab.euler.util.format
import org.scalatest.FlatSpec
import org.scalatest.matchers.MustMatchers
import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.annotation.JsonInclude.Include
case class Person(name: String, age: Int)
@JsonInclude(Include.NON_EMPTY)
case class OptionPerson(name: String, email: List[Person], age: Int)
class JsonSupportSpec
extends FlatSpec
with JsonSupport
with MustMatchers {
val testJson =
"""{"name": "John Smith", "age": 33}"""
val testJson2 =
"""{"name": "John Smith", "age": 33, email: {"a":{"name": "John Smith 1", "age": 33}, "b":{"name": "John Smith 2", "age": 33}}}"""
val testJsonList =
"""[{"name": "John Smith", "age": 33}, {"name": "Ken Devid", "age": 23}, {"name": "Shara Connor", "age": 87}]"""
"JsonSupport" must "parse test json to case class" in {
val res = deserialize(testJson, classOf[Person])
res.age must be (33)
res.name must be ("John Smith")
}
"JsonSupport" must "parse test json to case class by generic type" in {
val res = deserialize[Person](testJson)
res.age must be (33)
res.name must be ("John Smith")
}
it must "parse test json to Map" in {
val res = deserialize[Map[String, Any]](testJson)
res.size must be (2)
res.get("name") must be (Some("John Smith"))
res.get("age") must be (Some(33))
}
it must "parse test json to List[Person]" in {
val res = deserialize[List[Person]](testJsonList)
res.size must be (3)
res(0).name must be ("John Smith")
res(0).age must be (33)
res(1).name must be ("Ken Devid")
res(1).age must be (23)
res(2).name must be ("Shara Connor")
res(2).age must be (87)
}
it must "parse test json to List[Person] form class" in {
val res = deserialize(testJsonList, classOf[List[Person]])
res.size must be (3)
res(0).name must be ("John Smith")
res(0).age must be (33)
res(1).name must be ("Ken Devid")
res(1).age must be (23)
res(2).name must be ("Shara Connor")
res(2).age must be (87)
}
it must "parse case class with Option" in {
val res = deserialize(testJson, classOf[OptionPerson])
println(s"\n | $res \n |\n")
println(s"\n | ${serialize(OptionPerson("", Nil, 23))} \n |\n")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment