aboutsummaryrefslogtreecommitdiff
path: root/shared/src/test/scala/FieldNameTests.scala
blob: cda583770d43409aa3e1b46ae24e293eb6b5491f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package spray.json

import org.scalatest._

class FieldNameTests extends FlatSpec with FormatTests {

  case class A(camelCASE: String, `__a_aB__`: Int, `a-a_B`: Int)
  case class B(camelCaseA: A)
  case class C(abA: String)

  trait All extends DefaultJsonProtocol with DerivedFormats {
    implicit val aFormat = jsonFormat[A]
    implicit val bFormat = jsonFormat[B]
    implicit val cFormat = jsonFormat[C]
  }

  {
    object Protocol extends All with SnakeCase
    import Protocol._
    "snake_case" should behave like checkRoundtrip(
      B(A("helloWorld", 0, 0)),
      """{"camel_case_a":{"camel_case":"helloWorld","__a_a_b__":0,"a-a_b":0}}"""
    )
    "abA" should "serialize correctly" in {
      assert(C("test").toJson === """{"ab_a":"test"}""".parseJson)
    }
  }

  {
    object Protocol extends All with KebabCase
    import Protocol._
    "kebab-case" should behave like checkRoundtrip(
      B(A("helloWorld", 0, 0)),
      """{"camel-case-a":{"camel-case":"helloWorld","__a_a-b__":0,"a-a_b":0}}"""
    )
  }

}