-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* implemented json interpolator * implemented default arguments for derived JsonFormats * changed default imports for besom.json.* and moved default behavior to besom.json.custom.* * added docs for besom-json --------- Co-authored-by: Paweł Prażak <[email protected]>
- Loading branch information
1 parent
0673b6c
commit 4b10f3f
Showing
16 changed files
with
843 additions
and
45 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
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
73 changes: 70 additions & 3 deletions
73
besom-json/src/test/scala/besom/json/DerivedFormatsSpec.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 |
---|---|---|
@@ -1,18 +1,85 @@ | ||
package besom.json | ||
package besom.json.test | ||
|
||
import org.specs2.mutable.* | ||
|
||
class DerivedFormatsSpec extends Specification { | ||
|
||
"The derives keyword" should { | ||
"behave as expected" in { | ||
import DefaultJsonProtocol.* | ||
given JsonProtocol = DefaultJsonProtocol | ||
import besom.json.* | ||
|
||
case class Color(name: String, red: Int, green: Int, blue: Int) derives JsonFormat | ||
val color = Color("CadetBlue", 95, 158, 160) | ||
|
||
color.toJson.convertTo[Color] mustEqual color | ||
} | ||
|
||
"be able to support default argument values" in { | ||
import besom.json.* | ||
|
||
case class Color(name: String, red: Int, green: Int, blue: Int = 160) derives JsonFormat | ||
val color = Color("CadetBlue", 95, 158) | ||
|
||
val json = """{"name":"CadetBlue","red":95,"green":158}""" | ||
|
||
color.toJson.convertTo[Color] mustEqual color | ||
json.parseJson.convertTo[Color] mustEqual color | ||
} | ||
|
||
"be able to support missing fields when there are default argument values" in { | ||
import besom.json.* | ||
|
||
case class Color(name: String, red: Int, green: Int, blue: Option[Int] = None) derives JsonFormat | ||
val color = Color("CadetBlue", 95, 158) | ||
|
||
val json = """{"green":158,"red":95,"name":"CadetBlue"}""" | ||
|
||
color.toJson.compactPrint mustEqual json | ||
color.toJson.convertTo[Color] mustEqual color | ||
json.parseJson.convertTo[Color] mustEqual color | ||
} | ||
|
||
"be able to write and read nulls for optional fields" in { | ||
import besom.json.custom.* | ||
|
||
locally { | ||
given jp: JsonProtocol = new DefaultJsonProtocol { | ||
override def writeNulls = true | ||
override def requireNullsForOptions = true | ||
} | ||
import jp.* | ||
|
||
case class Color(name: String, red: Int, green: Int, blue: Option[Int]) derives JsonFormat | ||
val color = Color("CadetBlue", 95, 158, None) | ||
|
||
val json = """{"blue":null,"green":158,"red":95,"name":"CadetBlue"}""" | ||
|
||
color.toJson.compactPrint mustEqual json | ||
|
||
color.toJson.convertTo[Color] mustEqual color | ||
json.parseJson.convertTo[Color] mustEqual color | ||
|
||
val noExplicitNullJson = """{"green":158,"red":95,"name":"CadetBlue"}""" | ||
noExplicitNullJson.parseJson.convertTo[Color] must throwA[DeserializationException] | ||
} | ||
|
||
locally { | ||
given jp2: JsonProtocol = new DefaultJsonProtocol { | ||
override def writeNulls = false | ||
override def requireNullsForOptions = false | ||
} | ||
import jp2.* | ||
|
||
case class Color(name: String, red: Int, green: Int, blue: Option[Int]) derives JsonFormat | ||
val color = Color("CadetBlue", 95, 158, None) | ||
|
||
val json = """{"green":158,"red":95,"name":"CadetBlue"}""" | ||
|
||
color.toJson.compactPrint mustEqual json | ||
|
||
color.toJson.convertTo[Color] mustEqual color | ||
json.parseJson.convertTo[Color] mustEqual color | ||
} | ||
} | ||
} | ||
} |
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
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,3 @@ | ||
package besom.json | ||
|
||
export besom.util.JsonInterpolator.* |
Oops, something went wrong.