2
編譯這是我的代碼:的toJSON(地圖( 「成功」 - >真實的, 「消息」 - > STR))不能在play2
import play.api.mvc._
import play.api.libs.json._
import play.api.libs.json.Json._
import play.api.libs.json.Writes._
class BaseController extends Controller with Secured with DefaultWrites {
private implicit def str2json(str: String) = new {
def asSuccessJson = toJson(Map("success" -> true, "message" -> str)) // (*)
def asFailedJson = toJson(Map("success" -> false, "message" -> str)) // (*)
}
}
但它不能在兩個編譯(*)
線。該錯誤信息是:
Multiple markers at this line
- No Json deserializer found for type scala.collection.immutable.Map[java.lang.String,Any]. Try
to implement an implicit Writes or Format for this type.
- not enough arguments for method toJson: (implicit tjs:
play.api.libs.json.Writes[scala.collection.immutable.Map[java.lang.String,Any]])
play.api.libs.json.JsValue.Unspecified value parameter tjs.
我必須把它寫成:
def asSuccessJson = toJson(Map("success" -> true.toString, "message" -> str))
通知true.toString
。它工作但無聊。
如何解決?
等待下一個版本,目前一個不完美。 – Freewind 2012-03-28 14:55:34
當前的api有點粗糙,但它帶來了我們所需要的一切,沒有任何魔法,任何內省。這些原則將保留一些糖和一些新的工具...耐心;) – mandubian 2012-03-28 15:00:56
理論上可以編寫一個序列化程序,在運行時嘗試序列化現有序列化器的所有值的Map [String,Any]?無法編寫格式不可知的API層,但無法在運行時序列化Any(如果可能) – Ciantic 2012-07-15 08:31:04