scala - Deserialize body of result which is a byte array to json response -
i'm trying take body of result , deserialize json.
val str = """ {"protectedbitcoinaddress":"molm1zdpbsh81eykhfcolz3myqusy9frrg"} """ val request : jsvalue = json.parse(str) println("request: " + request.tostring) val result : future[result] = createunsignednlocktime()(fakerequest().withjsonbody(request)) whenready(result, timeout(19 seconds), interval(5 millis)) { res => println("==============================") val e: play.api.libs.iteratee.enumerator[array[byte]] = res.body val bodyasstr: scala.concurrent.future[string] = e.run(play.api.libs.iteratee.iteratee.fold("") { (memo, next) => memo + next }) println("body string: " + await.result(bodyasstr,5 seconds)) val bodyasjsvalue: scala.concurrent.future[play.api.libs.json.jsvalue] = e.map(play.api.libs.json.json.parse) println("body js: " + await.result(bodyasjsvalue, 5 seconds)) println("==============================") }
how take of type enumerator[scala.array[scala.byte]]
, convert json object in playframework?
is want ?
[info] starting scala interpreter... [info] welcome scala version 2.10.4 (java hotspot(tm) 64-bit server vm, java 1.7.0_79). type in expressions have them evaluated. type :help more information. scala>import play.api.mvc.{result, results} import play.api.mvc.{result, results} scala>import play.api.libs.iteratee.{enumerator, iteratee} import play.api.libs.iteratee.{enumerator, iteratee} scala>import play.api.libs.json.json import play.api.libs.json.json scala>import scala.concurrent.{await, future} import scala.concurrent.{await, future} scala>import scala.concurrent.duration._ import scala.concurrent.duration._ scala>import scala.concurrent.executioncontext.implicits.global import scala.concurrent.executioncontext.implicits.global scala>val result: result = results.ok(json.stringify(json.tojson(map("k" -> "v")))).as("application/json") result: play.api.mvc.result = result(200, map(content-type -> application/json)) scala>val enumerator: enumerator[array[byte]] = result.body enumerator: play.api.libs.iteratee.enumerator[array[byte]] = play.api.libs.iteratee.enumerator$$anon$18@70d59654 scala>val bodyasbyteseventually: future[array[byte]] = enumerator.run(iteratee.fold(array.empty[byte]) { (memo, nextchunk) => memo ++ nextchunk }) bodyasbyteseventually: scala.concurrent.future[array[byte]] = scala.concurrent.impl.promise$defaultpromise@8786f46 scala>val bodyasstreventually = bodyasbyteseventually.map(bytes => new string(bytes)) bodyasstreventually: scala.concurrent.future[string] = scala.concurrent.impl.promise$defaultpromise@442b7dd6 scala>val bodyasstr = await.result(bodyasstreventually, 1.seconds) bodyasstr: string = {"k":"v"} // in general, not block unless know doing.
Comments
Post a Comment