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
toshi
committed
Jan 19, 2015
0 parents
commit 3da54a9
Showing
7 changed files
with
109 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/RUNNING_PID | ||
/logs/ | ||
/project/*-shim.sbt | ||
/project/project/ | ||
/project/target/ | ||
/target/ |
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,12 @@ | ||
name := """play-json-naming""" | ||
|
||
version := "1.0" | ||
|
||
scalaVersion := "2.11.5" | ||
|
||
crossScalaVersions := Seq("2.10.4", "2.11.5") | ||
|
||
libraryDependencies ++= Seq( | ||
"com.typesafe.play" %% "play-json" % "2.3.7" % "provided", | ||
"org.scalatest" %% "scalatest" % "2.1.6" % "test" | ||
) |
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,4 @@ | ||
#Activator-generated Properties | ||
#Mon Jan 19 19:48:42 JST 2015 | ||
template.uuid=a855816c-0367-44ba-9adb-6a903f6ad599 | ||
sbt.version=0.13.7 |
25 changes: 25 additions & 0 deletions
25
src/main/scala/com/github/tototoshi/play/json/JsonNaming.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,25 @@ | ||
package com.github.tototoshi.play.json | ||
|
||
import play.api.libs.json._ | ||
|
||
object JsonNaming { | ||
|
||
private def mapKeys[A, B](m: Seq[(A, B)])(f: A => A): Seq[(A, B)] = | ||
m.map { case (k, v) => (f(k), v) } | ||
|
||
def snakecase[T](format: Format[T]): Format[T] = new Format[T] { | ||
def reads(json: JsValue): JsResult[T] = { | ||
format.reads(json match { | ||
case obj: JsObject => JsObject(mapKeys(obj.fields)(StringUtil.camelcase)) | ||
case x => x | ||
}) | ||
} | ||
|
||
def writes(o: T): JsValue = { | ||
format.writes(o) match { | ||
case obj: JsObject => JsObject(mapKeys(obj.fields)(StringUtil.snakecase)) | ||
case x => x | ||
} | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/scala/com/github/tototoshi/play/json/StringUtil.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,19 @@ | ||
package com.github.tototoshi.play.json | ||
|
||
import play.api.libs.json._ | ||
|
||
private[json] object StringUtil { | ||
|
||
def camelcase(s: String): String = (s.split("_").toList match { | ||
case head :: tail => head :: tail.map(_.capitalize) | ||
case x => x | ||
}).mkString | ||
|
||
def snakecase(s: String): String = s.foldLeft(new StringBuilder) { | ||
case (s, c) if Character.isUpperCase(c) => | ||
s.append("_").append(Character.toLowerCase(c)) | ||
case (s, c) => | ||
s.append(c) | ||
}.toString | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
src/test/scala/com/github/tototoshi/play/json/JsonNamingSuite.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,25 @@ | ||
package com.github.tototoshi.play.json | ||
|
||
import org.scalatest._ | ||
import play.api.libs.json._ | ||
|
||
case class Name(firstName: String, lastName: String) | ||
case class User(id: Int, nameData: Name) | ||
|
||
class JsonNamingSuite extends FunSuite with ShouldMatchers { | ||
|
||
|
||
implicit val nameFormat = JsonNaming.snakecase(Json.format[Name]) | ||
implicit val userFormat = JsonNaming.snakecase(Json.format[User]) | ||
|
||
val jsonString = """{"id":1,"name_data":{"first_name":"Toshiyuki","last_name":"Takahashi"}}""" | ||
|
||
test("read") { | ||
Json.parse(jsonString).validate[User].get should be(User(1, Name("Toshiyuki", "Takahashi"))) | ||
} | ||
|
||
test("write") { | ||
Json.toJson(User(1, Name("Toshiyuki", "Takahashi"))) should be(Json.parse(jsonString)) | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
src/test/scala/com/github/tototoshi/play/json/StringUtilSuite.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,18 @@ | ||
package com.github.tototoshi.play.json | ||
|
||
import org.scalatest._ | ||
import play.api.libs.json._ | ||
|
||
class StringUtilSuite extends FunSuite with ShouldMatchers { | ||
|
||
test("camelCase -> snake_case") { | ||
StringUtil.snakecase("play") should be("play") | ||
StringUtil.snakecase("playJson") should be("play_json") | ||
} | ||
|
||
test("snake_case -> camelCase") { | ||
StringUtil.camelcase("play") should be("play") | ||
StringUtil.camelcase("play_json") should be("playJson") | ||
} | ||
|
||
} |