Json 使用自定义媒体类型播放框架自定义解组器
我有一个自定义JSON媒体类型,如:Json 使用自定义媒体类型播放框架自定义解组器,json,scala,playframework,unmarshalling,media-type,Json,Scala,Playframework,Unmarshalling,Media Type,我有一个自定义JSON媒体类型,如:application/vnd.custom+JSON。我为我的MyCustomObj编写了读者和作者,或者更确切地说是格式化程序 我想为这种内容类型编写封送器和解封器。我和马歇尔勒的合作很成功,但没有让解编者为游戏而工作!JSON支持。代码段: val `application/vnd.custom+json` = MediaTypes.register(MediaType.custom("application/vnd.custom+json")) tr
application/vnd.custom+JSON
。我为我的MyCustomObj
编写了读者和作者,或者更确切地说是格式化程序
我想为这种内容类型编写封送器和解封器。我和马歇尔勒的合作很成功,但没有让解编者为游戏而工作!JSON支持。代码段:
val `application/vnd.custom+json` = MediaTypes.register(MediaType.custom("application/vnd.custom+json"))
trait PlayMyCustomSupport extends PlayMyCustomFormat with PlayJsonSupport {
implicit val playMyCustomMarshaller =
Marshaller.delegate[MyCustomObj, JsValue](`application/vnd.custom+json`)(Json.toJson(_))
// This ".asOpt.get" is just a hack to test if it works. Will implement later properly.
implicit val playMyCustomUnmarshaller =
Unmarshaller.delegate[JsValue, MyCustom](`application/vnd.custom+json`)(Json.fromJson[MyCustomObj](_).asOpt.get)
}
我的测试看起来像:
"allow unmarshalling a Json to a my custom object" in {
val myCustomObj = MyCustomObj("foobar")
val httpEntity = HttpEntity(`application/vnd.custom+json`, """{"custom":"foobar"}""")
assert(httpEntity.as[MyCustomObj] === Right(myCustomObj))
}
当我运行测试时,会出现如下错误:
[info] - allow unmarshalling a Json to a my custom object *** FAILED ***
[info] Left(UnsupportedContentType(Expected 'application/json')) did not equal Right(MyCustomObj(foobar))
所以我的自定义媒体类型没有注册还是什么?奇怪的是,上面的马歇尔勒工作得很好
谢谢
最好的