scala - argonaut - separating failures and successes when extracting List[A] -


i have instance of decodejson[a] particular type, a, , have instance of json, call j. i'd j.as[list[a]]. however, json coming me 3rd party, , if of items in array missing fields, i'd log errors items , collect items can parsed (instead of failing entire decoding).

it seems me want end (list[(string, cursorhistory)], list[a]). is, decoded elements end in list on right, , errors items not parsed accumulated on left. looks lot monadplus.separate method in scalaz.

i can think of couple ways of getting there. 1 way like:

j.as[list[json]].map(_.map(_.as[a].result).separate) 

this should give me decoderesult[list[(string, cursorhistory)], list[a]] desired. however, throw away cursor information in json array items failed located. use zipwithindex, starts pretty messy.

to me seems better solution this:

implicit def decoderesultdecodejson[a: decodejson]: decodejson[decoderesult[a]] =   decodearr(_.as[a]) // outer decoderesult success  j.as[list[decoderesult[a]]].map(_.map(_.result)).separate) 

this give me same output type, cursorhistory should populated include information stepping elements of array.

here example:

val j = json.array(list("1", "2", "foo").map(json.jstring): _*) val r = j.as[list[decoderesult[int]]].map(_.map(_.result).separate) // r decoderesult[(list[(string, cursorhistory)], list[int])] = // decoderesult(\/-((list((int,cursorhistory([->,->,\\]))),list(1, 2)))) 

this seems behave reasonably (ignoring terrible list concatenation happening). however, seems sort of thing common use-case, i'm wondering if there simpler or built-in way this. if not, might submit pr argonaut decodejson[decodejson[a]] instance , see if interested.

you can use hcursor decoder, rather json decoder, preserves history:

j.as[list[hcursor]].map(_.map(_.as[int].result).separate) 

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 -