json - how to convert JString to Int with json4s -


we switched jerkson json4s, discovered default de-serialization behavior of these 2 libraries far same.

one of issues we're having receive json input number field represented string instead of number

//example json object string representation of "id" {     "id" : "12545" }   //example json object number representation of "id" {     "id" : 12345 } 

these needs deserialized following class

case class example(id:int) 

this our general setup deserializing json arbitrary classes

import org.json4s.native.serialization._ import org.json4s._ import org.json4s.native.jsonmethods._  object json {     implicit val formats = defaultformats     def parse[t](json:string)(implicit mf: manifest[t]):t =  {         org.json4s.native.jsonmethods.parse(input).extract[t]     } } 

but whenever attempt parse json object string representation of id throws , exception message:

no usable value offers no usable value id not know how convert jstring(12545) int

i've been looking around way set custom reader integer fields attempts parse strings integers i've yet find solution covers our use case. need general catch solution in order support legacy applications.

anyone know how achieve this?

thanks, ended serializer object:

object stringtolong extends customserializer[long](format => ({ case jstring(x) => x.tolong }, { case x: long => jint(x) })) implicit val formats = defaultformats + stringtolong 

Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -