-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/main/scala/com/github/tminglei/slickpg/PgPlayJsonSupport.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.github.tminglei.slickpg | ||
|
||
import scala.slick.driver.PostgresDriver | ||
import scala.slick.lifted.{TypeMapper, Column} | ||
|
||
trait PgPlayJsonSupport extends json.PgJsonExtensions { driver: PostgresDriver => | ||
import play.api.libs.json._ | ||
|
||
type JSONType = JsValue | ||
|
||
trait JsonImplicits { | ||
implicit val jsonTypeMapper = | ||
new utils.GenericTypeMapper[JsValue]("json", | ||
(v) => Json.parse(v), | ||
(v) => Json.stringify(v) | ||
) | ||
|
||
implicit def jsonColumnExtensionMethods(c: Column[JsValue])( | ||
implicit tm: TypeMapper[JsValue], tm1: TypeMapper[List[String]]) = { | ||
new JsonColumnExtensionMethods[JsValue](c) | ||
} | ||
implicit def jsonOptionColumnExtensionMethods(c: Column[Option[JsValue]])( | ||
implicit tm: TypeMapper[JsValue], tm1: TypeMapper[List[String]]) = { | ||
new JsonColumnExtensionMethods[Option[JsValue]](c) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
src/test/scala/com/github/tminglei/slickpg/PgPlayJsonSupportTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package com.github.tminglei.slickpg | ||
|
||
import org.junit._ | ||
import org.junit.Assert._ | ||
import play.api.libs.json._ | ||
|
||
class PgPlayJsonSupportTest { | ||
import MyPostgresDriver4.simple._ | ||
|
||
val db = Database.forURL(url = "jdbc:postgresql://localhost/test?user=test", driver = "org.postgresql.Driver") | ||
|
||
case class JsonBean(id: Long, json: JsValue) | ||
|
||
object JsonTestTable extends Table[JsonBean]("JsonTest2") { | ||
def id = column[Long]("id", O.AutoInc, O.PrimaryKey) | ||
def json = column[JsValue]("json") | ||
|
||
def * = id ~ json <> (JsonBean, JsonBean unapply _) | ||
} | ||
|
||
//------------------------------------------------------------------------------ | ||
|
||
val testRec1 = JsonBean(33L, Json.parse(""" { "a":101, "b":"aaa", "c":[3,4,5,9] } """)) | ||
val testRec2 = JsonBean(35L, Json.parse(""" [ {"a":"v1","b":2}, {"a":"v5","b":3} ] """)) | ||
|
||
@Test | ||
def testJsonFunctions(): Unit = { | ||
db withSession { implicit session: Session => | ||
JsonTestTable.insert(testRec1) | ||
JsonTestTable.insert(testRec2) | ||
|
||
val json1 = Json.parse(""" {"a":"v1","b":2} """) | ||
val json2 = Json.parse(""" {"a":"v5","b":3} """) | ||
|
||
val q0 = JsonTestTable.where(_.id === testRec2.id.bind).map(_.json) | ||
println(s"sql0 = ${q0.selectStatement}") | ||
assertEquals(JsArray(List(json1,json2)), q0.first()) | ||
|
||
// pretty(render(JsNumber(101))) will get "101", but parse("101") will fail, since json string must start with '{' or '[' | ||
// println(s"'+>' sql = ${q1.selectStatement}") | ||
// assertEquals(JsNumber(101), q1.first()) | ||
|
||
val q11 = JsonTestTable.where(_.json.+>>("a") === "101".bind).map(_.json.+>>("c")) | ||
println(s"'+>>' sql = ${q11.selectStatement}") | ||
assertEquals("[3,4,5,9]", q11.first()) | ||
|
||
val q12 = JsonTestTable.where(_.json.+>>("a") === "101".bind).map(_.json.+>("c")) | ||
println(s"'+>' sql = ${q12.selectStatement}") | ||
assertEquals(JsArray(List(JsNumber(3), JsNumber(4), JsNumber(5), JsNumber(9))), q12.first()) | ||
|
||
// json array's index starts with 0 | ||
val q2 = JsonTestTable.where(_.id === testRec2.id).map(_.json.~>(1)) | ||
println(s"'~>' sql = ${q2.selectStatement}") | ||
assertEquals(json2, q2.first()) | ||
|
||
val q21 = JsonTestTable.where(_.id === testRec2.id).map(_.json.~>>(1)) | ||
println(s"'~>>' sql = ${q21.selectStatement}") | ||
assertEquals("""{"a":"v5","b":3}""", q21.first()) | ||
|
||
val q3 = JsonTestTable.where(_.id === testRec2.id).map(_.json.arrayLength) | ||
println(s"'arrayLength' sql = ${q3.selectStatement}") | ||
assertEquals(2, q3.first()) | ||
|
||
val q4 = JsonTestTable.where(_.id === testRec2.id).map(_.json.arrayElements) | ||
println(s"'arrayElements' sql = ${q4.selectStatement}") | ||
assertEquals(List(json1, json2), q4.list()) | ||
|
||
val q41 = JsonTestTable.where(_.id === testRec2.id).map(_.json.arrayElements) | ||
println(s"'arrayElements' sql = ${q41.selectStatement}") | ||
assertEquals(json1, q41.first()) | ||
|
||
val q5 = JsonTestTable.where(_.id === testRec1.id).map(_.json.objectKeys) | ||
println(s"'objectKeys' sql = ${q5.selectStatement}") | ||
assertEquals(List("a","b","c"), q5.list()) | ||
|
||
val q51 = JsonTestTable.where(_.id === testRec1.id).map(_.json.objectKeys) | ||
println(s"'objectKeys' sql = ${q51.selectStatement}") | ||
assertEquals("a", q51.first()) | ||
} | ||
} | ||
|
||
//------------------------------------------------------------------------------ | ||
|
||
@Before | ||
def createTables(): Unit = { | ||
db withSession { implicit session: Session => | ||
JsonTestTable.ddl create | ||
} | ||
} | ||
|
||
@After | ||
def dropTables(): Unit = { | ||
db withSession { implicit session: Session => | ||
JsonTestTable.ddl drop | ||
} | ||
} | ||
} |