Play 2.1 Scala JSON parsing harder than Jerkson? -


with jerkson, able parse string containing json array, this:

com.codahale.jerkson.json.parse[array[credentials]](contents) 

where contents string containing following:

[{"awsaccountname":"mslinn","accesskey":"blahblah","secretkey":"blahblah"}] 

... , array of credentials.

(brief diversion) tried similar using new json parser play 2.1 , scala using different data. simple parse, following works fine. case class (s3file) defines unapply method necessary work:

case class s3file(accountname: string,                   bucketname: string,                   endpoint: string = ".s3.amazonaws.com")  implicit val s3fileformat = json.format[s3file] val jsvalue = json.parse(stringcontainingjson) json.fromjson(jsvalue).get 

let's reconsider original string called contents containing json. collections, array of objects has no unapply method. means technique showed in the diversion above won't work. tried create throwaway case class purpose:

case class arraycreds(payload: array[credentials])  implicit val credsformat = json.format[arraycreds] val jsvalue = json.parse(contents) val credarray = json.fromjson(jsvalue).get.payload 

... unfortunately, fails:

no unapply function found [error]         implicit val credsformat = json.format[arraycreds] [error]                                               ^ [error]       /blah.scala:177: diverging implicit expansion type play.api.libs.json.reads[t] [error] starting method arrayreads in trait defaultreads [error]         val credarray = json.fromjson(jsvalue).get [error]                                      ^ 

is there simple way of parsing array of json using play 2.1's new json parser? expect throwaway case class wrong approach, , implicit need instead:

implicit val credsformat = json.format[credentials] 

but don't understand how write rest of deserialization in simple manner. of code examples have seen rather verbose, seems contrary spirit of scala. ideal incantation simple jerkson's incantation.

thanks,

mike

i think you're looking for:

scala> import play.api.libs.json._ import play.api.libs.json._  scala> case class credentials(awsaccountname: string, accesskey: string, secretkey: string) defined class credentials  scala> implicit val credentialsfmt = json.format[credentials] credentialsfmt: play.api.libs.json.oformat[credentials] = play.api.libs.json.oformat$$anon$1@1da9be95  scala> val js = """[{"awsaccountname":"mslinn","accesskey":"blahblah","secretkey":"blahblah"}]"""  js: string = [{"awsaccountname":"mslinn","accesskey":"blahblah","secretkey":"blahblah"}]  scala> json.fromjson[seq[credentials]](json.parse(js)) res3: play.api.libs.json.jsresult[seq[credentials]] = jssuccess(list(credentials(mslinn,blahblah,blahblah)),) 

hth,

julien


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -