This commit is contained in:
Tony Tam 2012-12-03 13:48:00 -08:00
parent 69fe17380c
commit 8b5e638409
2 changed files with 190 additions and 190 deletions

View File

@ -6,38 +6,38 @@ import org.json4s.jackson.JsonMethods._
import org.json4s.native.Serialization.{read, write}
class ModelPropertySerializer extends CustomSerializer[ModelProperty] (formats => ({
case json =>
implicit val fmts: Formats = formats
ModelProperty(
`type` = (json \ "type").extractOrElse(""),
required = ((json \ "required").extractOrElse("false")).toBoolean,
description = (json \ "description").extractOpt[String],
allowableValues = (json \ "allowableValues").extract[AllowableValuesFoo],
items = (json \ "items").extractOpt[ModelRef]
)
case json =>
implicit val fmts: Formats = formats
ModelProperty(
`type` = (json \ "type").extractOrElse(""),
required = ((json \ "required").extractOrElse("false")).toBoolean,
description = (json \ "description").extractOpt[String],
allowableValues = (json \ "allowableValues").extract[AllowableValuesFoo],
items = (json \ "items").extractOpt[ModelRef]
)
}, {
case x: ModelProperty =>
implicit val fmts = formats
("type" -> x.`type`) ~
("required" -> x.required) ~
("description" -> x.description) ~
("allowableValues" -> {
x.allowableValues match {
case Any => JNothing // don't serialize when not a concrete type
case e:AllowableValuesFoo => Extraction.decompose(x.allowableValues)
case _ => JNothing
}
}) ~
("items" -> Extraction.decompose(x.items))
case x: ModelProperty =>
implicit val fmts = formats
("type" -> x.`type`) ~
("required" -> x.required) ~
("description" -> x.description) ~
("allowableValues" -> {
x.allowableValues match {
case Any => JNothing // don't serialize when not a concrete type
case e:AllowableValuesFoo => Extraction.decompose(x.allowableValues)
case _ => JNothing
}
}) ~
("items" -> Extraction.decompose(x.items))
}))
class ModelRefSerializer extends CustomSerializer[ModelRef](formats => ({
case json =>
implicit val fmts: Formats = formats
ModelRef(
(json \ "$ref").extract[String],
(json \ "type").extract[String]
)
(json \ "$ref").extract[String],
(json \ "type").extract[String]
)
}, {
case x: ModelRef =>
implicit val fmts = formats
@ -49,16 +49,16 @@ class AllowableValuesSerializer extends CustomSerializer[AllowableValuesFoo](for
case json =>
implicit val fmts: Formats = formats
json \ "valueType" match {
case JString(x) if x.equalsIgnoreCase("list") =>
case JString(x) if x.equalsIgnoreCase("list") =>
AllowableListValues((json \ "values").extract[List[String]])
case JString(x) if x.equalsIgnoreCase("range") =>
case JString(x) if x.equalsIgnoreCase("range") =>
AllowableRangeValues((json \ "min").extract[String], (json \ "max").extract[String])
case _ => Any
}
}
}, {
case AllowableListValues(values, "LIST") =>
implicit val fmts = formats
("valueType" -> "LIST") ~ ("values" -> Extraction.decompose(values))
("valueType" -> "LIST") ~ ("values" -> Extraction.decompose(values))
case AllowableRangeValues(min, max) =>
("valueType" -> "RANGE") ~ ("min" -> min) ~ ("max" -> max)
("valueType" -> "RANGE") ~ ("min" -> min) ~ ("max" -> max)
}))

View File

@ -12,184 +12,184 @@ import org.scalatest.matchers.ShouldMatchers
@RunWith(classOf[JUnitRunner])
class ModelRefSerializationTest extends FlatSpec with ShouldMatchers {
implicit val formats = DefaultFormats + new ModelRefSerializer
implicit val formats = DefaultFormats + new ModelRefSerializer
it should "deserialize a model ref" in {
val jsonString = """
{
"$ref":"Foo",
"type":"Bar"
}
"""
val json = parse(jsonString)
json.extract[ModelRef] match {
case p: ModelRef => {
p.ref should be ("Foo")
p.`type` should be ("Bar")
}
case _ => fail("expected type ModelRef")
}
}
it should "deserialize a model ref" in {
val jsonString = """
{
"$ref":"Foo",
"type":"Bar"
}
"""
val json = parse(jsonString)
json.extract[ModelRef] match {
case p: ModelRef => {
p.ref should be ("Foo")
p.`type` should be ("Bar")
}
case _ => fail("expected type ModelRef")
}
}
it should "serialize a model ref" in {
val ref = ModelRef("Foo", "Bar")
write(ref) should be ("""{"$ref":"Foo","type":"Bar"}""")
}
it should "serialize a model ref" in {
val ref = ModelRef("Foo", "Bar")
write(ref) should be ("""{"$ref":"Foo","type":"Bar"}""")
}
}
@RunWith(classOf[JUnitRunner])
class ModelPropertySerializationTest extends FlatSpec with ShouldMatchers {
implicit val formats = DefaultFormats + new AllowableValuesSerializer + new ModelPropertySerializer
implicit val formats = DefaultFormats + new AllowableValuesSerializer + new ModelPropertySerializer
it should "deserialize a model property with allowable values and ref" in {
val jsonString = """
{
"type":"string",
"required":false,
"description":"nice",
"allowableValues": {
"valueType":"LIST",
"values":["1","2","3"]
},
"items":{
"$ref":"Foo",
"type":"Bar"
}
}
"""
val json = parse(jsonString)
json.extract[ModelProperty] match {
case p: ModelProperty => {
p.`type` should be ("string")
p.required should be (false)
p.description should be (Some("nice"))
p.allowableValues match {
case e: AllowableListValues => e.values should be (List("1","2","3"))
case _ => fail("expected allowable values")
}
p.items match {
case e: ModelRef => {
e.ref should be ("Foo")
e.`type` should be ("Bar")
}
case _ => fail("expected type ModelProperty")
}
}
case _ => fail("expected type ModelProperty")
}
}
it should "deserialize a model property with allowable values and ref" in {
val jsonString = """
{
"type":"string",
"required":false,
"description":"nice",
"allowableValues": {
"valueType":"LIST",
"values":["1","2","3"]
},
"items":{
"$ref":"Foo",
"type":"Bar"
}
}
"""
val json = parse(jsonString)
json.extract[ModelProperty] match {
case p: ModelProperty => {
p.`type` should be ("string")
p.required should be (false)
p.description should be (Some("nice"))
p.allowableValues match {
case e: AllowableListValues => e.values should be (List("1","2","3"))
case _ => fail("expected allowable values")
}
p.items match {
case e: ModelRef => {
e.ref should be ("Foo")
e.`type` should be ("Bar")
}
case _ => fail("expected type ModelProperty")
}
}
case _ => fail("expected type ModelProperty")
}
}
it should "serialize a model property with allowable values and ref" in {
val p = ModelProperty("string", false, Some("nice"), AllowableListValues(List("a","b")),Some(ModelRef("Foo","Bar")))
write(p) should be ("""{"type":"string","required":false,"description":"nice","allowableValues":{"valueType":"LIST","values":["a","b"]},"items":{"$ref":"Foo","type":"Bar"}}""")
}
it should "serialize a model property with allowable values and ref" in {
val p = ModelProperty("string", false, Some("nice"), AllowableListValues(List("a","b")),Some(ModelRef("Foo","Bar")))
write(p) should be ("""{"type":"string","required":false,"description":"nice","allowableValues":{"valueType":"LIST","values":["a","b"]},"items":{"$ref":"Foo","type":"Bar"}}""")
}
it should "deserialize a model property with allowable values" in {
val jsonString = """
{
"type":"string",
"required":false,
"description":"nice",
"allowableValues": {
"valueType":"LIST",
"values":["1","2","3"]
}
}
"""
val json = parse(jsonString)
json.extract[ModelProperty] match {
case p: ModelProperty => {
p.`type` should be ("string")
p.required should be (false)
p.description should be (Some("nice"))
p.allowableValues match {
case e: AllowableListValues => e.values should be (List("1","2","3"))
case _ => fail("expected allowable values")
}
}
case _ => fail("expected type ModelProperty")
}
}
it should "deserialize a model property with allowable values" in {
val jsonString = """
{
"type":"string",
"required":false,
"description":"nice",
"allowableValues": {
"valueType":"LIST",
"values":["1","2","3"]
}
}
"""
val json = parse(jsonString)
json.extract[ModelProperty] match {
case p: ModelProperty => {
p.`type` should be ("string")
p.required should be (false)
p.description should be (Some("nice"))
p.allowableValues match {
case e: AllowableListValues => e.values should be (List("1","2","3"))
case _ => fail("expected allowable values")
}
}
case _ => fail("expected type ModelProperty")
}
}
it should "serialize a model property with allowable values" in {
val p = ModelProperty("string", false, Some("nice"), AllowableListValues(List("a","b")))
write(p) should be ("""{"type":"string","required":false,"description":"nice","allowableValues":{"valueType":"LIST","values":["a","b"]}}""")
}
it should "serialize a model property with allowable values" in {
val p = ModelProperty("string", false, Some("nice"), AllowableListValues(List("a","b")))
write(p) should be ("""{"type":"string","required":false,"description":"nice","allowableValues":{"valueType":"LIST","values":["a","b"]}}""")
}
it should "deserialize a model property" in {
val jsonString = """
{
"type":"string",
"required":false,
"description":"nice",
"allowableValues": {
"valueType":"LIST",
"values":["1","2","3"]
}
}
"""
val json = parse(jsonString)
json.extract[ModelProperty] match {
case p: ModelProperty => {
p.`type` should be ("string")
p.required should be (false)
p.description should be (Some("nice"))
}
case _ => fail("expected type ModelProperty")
}
}
it should "deserialize a model property" in {
val jsonString = """
{
"type":"string",
"required":false,
"description":"nice",
"allowableValues": {
"valueType":"LIST",
"values":["1","2","3"]
}
}
"""
val json = parse(jsonString)
json.extract[ModelProperty] match {
case p: ModelProperty => {
p.`type` should be ("string")
p.required should be (false)
p.description should be (Some("nice"))
}
case _ => fail("expected type ModelProperty")
}
}
it should "serialize a model property" in {
val p = ModelProperty("string", false, Some("nice"))
write(p) should be ("""{"type":"string","required":false,"description":"nice"}""")
}
it should "serialize a model property" in {
val p = ModelProperty("string", false, Some("nice"))
write(p) should be ("""{"type":"string","required":false,"description":"nice"}""")
}
}
@RunWith(classOf[JUnitRunner])
class AllowableValuesSerializersTest extends FlatSpec with ShouldMatchers {
implicit val formats = DefaultFormats + new AllowableValuesSerializer
implicit val formats = DefaultFormats + new AllowableValuesSerializer
it should "deserialize allowable value list" in {
val allowableValuesListString = """
{
"valueType":"LIST",
"values":["1","2","3"]
}
"""
val json = parse(allowableValuesListString)
json.extract[AllowableValuesFoo] match {
case avl: AllowableListValues => {
avl.valueType should be ("LIST")
avl.values should be (List("1","2","3"))
}
}
}
it should "deserialize allowable value list" in {
val allowableValuesListString = """
{
"valueType":"LIST",
"values":["1","2","3"]
}
"""
val json = parse(allowableValuesListString)
json.extract[AllowableValuesFoo] match {
case avl: AllowableListValues => {
avl.valueType should be ("LIST")
avl.values should be (List("1","2","3"))
}
}
}
it should "serialize allowable values list" in {
val l = AllowableListValues(List("1","2","3"))
write(l) should be ("""{"valueType":"LIST","values":["1","2","3"]}""")
}
it should "serialize allowable values list" in {
val l = AllowableListValues(List("1","2","3"))
write(l) should be ("""{"valueType":"LIST","values":["1","2","3"]}""")
}
it should "deserialize allowable values range" in {
val allowableValuesRangeString = """
{
"valueType":"RANGE",
"min":"abc",
"max":3
}
"""
val json = parse(allowableValuesRangeString)
json.extract[AllowableValuesFoo] match {
case avr: AllowableRangeValues => {
avr.min should be ("abc")
avr.max should be ("3")
}
case _ => fail("wrong type returned, should be AllowabeValuesList")
}
}
it should "deserialize allowable values range" in {
val allowableValuesRangeString = """
{
"valueType":"RANGE",
"min":"abc",
"max":3
}
"""
val json = parse(allowableValuesRangeString)
json.extract[AllowableValuesFoo] match {
case avr: AllowableRangeValues => {
avr.min should be ("abc")
avr.max should be ("3")
}
case _ => fail("wrong type returned, should be AllowabeValuesList")
}
}
it should "serialize allowable values range" in {
val l = AllowableRangeValues("-1", "3")
write(l) should be ("""{"valueType":"RANGE","min":"-1","max":"3"}""")
}
it should "serialize allowable values range" in {
val l = AllowableRangeValues("-1", "3")
write(l) should be ("""{"valueType":"RANGE","min":"-1","max":"3"}""")
}
}