Deserialize BigDecimal in scala with json4s return empty list -
given json:
{ "id": "1", "details": [{ "tax": [{ "amount": 1 }, { "amount": 2 }] }] }
i'm trying reading in way:
lazy val amounts: list[bigdecimal] = parse(json) \\ "amount" \ classof[jdecimal]
but it's giving me empty list, while using jdouble
this:
lazy val amounts: list[double] = parse(json) \\ "amount" \ classof[jdouble]
it's giving me correct list.
how can directly read list of bigdecimals
?
shortly can solve using extract
method target type
conversion, like:
val amounts = parse(json) \ "details" \ "tax" \ "amount" implicit val formats = defaultformats val decimals = amounts.extract[list[bigdecimal]] > list(1, 2)
explanation:
when read amounts
it's element type jint
not jdecimal
,
val amounts = parse(json) \ "details" \ "tax" \ "amount" > jarray(list(jint(1), jint(2)))
as can see it's jint
type amounts
.
and extracting class
type:
def \[a <: jvalue](clazz: class[a]): list[a#values] = finddirect(jv.children, typepredicate(clazz) _).asinstanceof[list[a]] map { _.values }
it's predicating
clazz
, amounts
's element type jint
, it's return empty list.
Comments
Post a Comment