Skip to content

Instantly share code, notes, and snippets.

@gustavofranke
Created January 30, 2018 16:36
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 gustavofranke/4a7bd9b03777f2559a88c41d45171935 to your computer and use it in GitHub Desktop.
Save gustavofranke/4a7bd9b03777f2559a88c41d45171935 to your computer and use it in GitHub Desktop.
package com.enernoc.fdr.api
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import org.joda.time.DateTime
import org.joda.time.format.DateTimeFormat
import org.scalatest.FunSuite
import rapture.json._
import rapture.json.jsonBackends.lift._
class RaptureAndYouSuite extends FunSuite {
test("from jvm to json") {
val p1 = Person("toto", List(Address("du chat", 3, "Lille")), None)
val p2 = Person("titi", List(Address("du chien", 2, "Lille")), Some("maid"))
val jsonStr = Json(List(p1, p2)).toString
println(s"jsonStr:\n$jsonStr")
}
test("json as Person") {
val json2 =
json"""
{"addresses":[{"city":"Lille","number":2,"street":"du chien"}],"name":"titi","maidenName":"maid"}
"""
assert(Person("titi", List(Address("du chien", 2, "Lille")), Some("maid")) == json2.as[Person])
println(s"json as Person: ${json2.as[Person]}")
}
test("json as List Person, with json literal") {
val json =
json"""[
{"addresses":[{"city":"Lille","number":3,"street":"du chat"}],"name":"toto"},
{"addresses":[{"city":"Lille","number":2,"street":"du chien"}],"name":"titi","maidenName":"maid"}]
"""
assert(
List(Person("toto", List(Address("du chat", 3, "Lille")), None), Person("titi", List(Address("du chien", 2, "Lille")), Some("maid"))) ==
json.as[List[Person]])
println(s"json as List Person: ${json.as[List[Person]]}")
}
test("json as List Person, with normal string") {
val str =
"""[
{"addresses":[{"city":"Lille","number":3,"street":"du chat"}],"name":"tutu"},
{"addresses":[{"city":"Lille","number":2,"street":"du chien"}],"name":"titi","maidenName":"maid"}]
"""
assert(
List(Person("tutu", List(Address("du chat", 3, "Lille")), None), Person("titi", List(Address("du chien", 2, "Lille")), Some("maid"))) ==
Json.parse(str).as[List[Person]])
println(s"with normal string: ${Json.parse(str).as[List[Person]]}")
}
test("dates serialisation") {
import HolidayExtractor._
val src =
json"""
{
"person": {
"addresses": [{"city":"Lille","number":2,"street":"du chien"}],
"name": "John Doe"
},
"start": "2017-08-01 23:00:00+00",
"finish": "2017-08-31 23:00:00+00"
}
"""
println(src.as[Holiday])
// println(
// src.as[Vector[Json]].map(_.as[Holiday](itemExtractor, modes.throwExceptions.apply()))
// )
}
test("dates") {
val theDate = "2010-06-25 21:09:41.152254"
val formatter: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS")
println(s" 1: ${LocalDateTime.parse(theDate, formatter)}")
val date2 = "2017-08-01 23:00:00+00"
val formatter2: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss+SS")
println(s" 2: ${LocalDateTime.parse(date2, formatter2)}")
val dateTime = "2017-08-01 23:00:00+00"
val dtf = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss+SS")
val jodatime = dtf.parseDateTime(dateTime)
println(jodatime)
}
}
case class Person(name: String, addresses: List[Address], maidenName: Option[String])
case class Address(street: String, number: Int, city: String)
case class Holiday(person: Person, start: DateTime, finish: DateTime)
object HolidayExtractor {
implicit val dateTimeExtractor: Extractor[DateTime, Json] {
type Throws = DataGetException with Exception
} = Json.extractor[String].map[DateTime](dateTime => {
DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss+SS").parseDateTime(dateTime)
})
implicit val itemExtractor: Extractor[Holiday, Json] {
type Throws = DataGetException with Exception
} = Json.extractor[Json].map { json =>
Holiday(json.person.as[Person], json.start.as[DateTime], json.finish.as[DateTime])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment