scala - spray json attribute with special char -
i have json attribute name has special char. trying parse spray json. below code how can attribute name in json @xml:lang parsed case class.
import spray.json._ import defaultjsonprotocol._ object specialcharinname extends app { case class person(name: string, `@xml:lang`: string) val json = """ {"name":"myname", "@xml:lang":"us"} """ object personprotocol extends defaultjsonprotocol { implicit val personformat = jsonformat2(person) } import personprotocol._ val person = json.parsejson val personclass = person.convertto[person] println(personclass) }
the code above throws exception below
[error] (run-main-0) spray.json.deserializationexception: object missing required member '@xml$colonlang' spray.json.deserializationexception: object missing required member '@xml$colonlang' @ spray.json.package$.deserializationerror(package.scala:23) @ spray.json.productformats$class.fromfield(productformats.scala:60) @ c.c.s.f.v.d.specialcharinname$personprotocol$.fromfield(specialcharinname.scala:12) @ spray.json.productformatsinstances$$anon$2.read(productformatsinstances.scala:56) @ spray.json.productformatsinstances$$anon$2.read(productformatsinstances.scala:46) @ spray.json.jsvalue.convertto(jsvalue.scala:31) @ c.c.s.f.v.d.specialcharinname$.delayedendpoint$com$comcast$sv$fabric$vimond$domain$specialcharinname$1(specialcharinname.scala:20) @ c.c.s.f.v.d.specialcharinname$delayedinit$body.apply(specialcharinname.scala:6) @ scala.function0$class.apply$mcv$sp(function0.scala:40) @ scala.runtime.abstractfunction0.apply$mcv$sp(abstractfunction0.scala:12) @ scala.app$$anonfun$main$1.apply(app.scala:76) @ scala.app$$anonfun$main$1.apply(app.scala:76) @ scala.collection.immutable.list.foreach(list.scala:381) @ scala.collection.generic.traversableforwarder$class.foreach(traversableforwarder.scala:35) @ scala.app$class.main(app.scala:76) @ c.c.s.f.v.d.specialcharinname$.main(specialcharinname.scala:6) @ c.c.s.f.v.d.specialcharinname.main(specialcharinname.scala) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:497) caused by: java.util.nosuchelementexception: key not found: @xml$colonlang @ scala.collection.maplike$class.default(maplike.scala:228) @ scala.collection.abstractmap.default(map.scala:59) @ scala.collection.maplike$class.apply(maplike.scala:141) @ scala.collection.abstractmap.apply(map.scala:59) @ spray.json.productformats$class.fromfield(productformats.scala:57) @ c.c.s.f.v.d.specialcharinname$personprotocol$.fromfield(specialcharinname.scala:12) @ spray.json.productformatsinstances$$anon$2.read(productformatsinstances.scala:56) @ spray.json.productformatsinstances$$anon$2.read(productformatsinstances.scala:46) @ spray.json.jsvalue.convertto(jsvalue.scala:31) @ c.c.s.f.v.d.specialcharinname$.delayedendpoint$com$comcast$sv$fabric$vimond$domain$specialcharinname$1(specialcharinname.scala:20) @ c.c.s.f.v.d.specialcharinname$delayedinit$body.apply(specialcharinname.scala:6) @ scala.function0$class.apply$mcv$sp(function0.scala:40) @ scala.runtime.abstractfunction0.apply$mcv$sp(abstractfunction0.scala:12) @ scala.app$$anonfun$main$1.apply(app.scala:76) @ scala.app$$anonfun$main$1.apply(app.scala:76) @ scala.collection.immutable.list.foreach(list.scala:381) @ scala.collection.generic.traversableforwarder$class.foreach(traversableforwarder.scala:35) @ scala.app$class.main(app.scala:76) @ c.c.s.f.v.d.specialcharinname$.main(specialcharinname.scala:6) @ c.c.s.f.v.d.specialcharinname.main(specialcharinname.scala) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:497)
thanks in advance.
saad
import spray.json._ import defaultjsonprotocol._ object specialcharinname extends app { case class person(name: string, `@xml:lang`: string) val json = """ {"name":"myname", "@xml:lang":"us"} """ object personprotocol extends defaultjsonprotocol { implicit val personformat = jsonformat(person.apply, "name", "@xml:lang") } import personprotocol._ val person = json.parsejson val personclass = person.convertto[person] println(personclass) }
enjoy :)
simply put, use dedicated jsonformat
method accepts builder method (like apply
) , list of named properties must present in json document being parsed. default 1 doesn't work in case.
Comments
Post a Comment