scala - Using SprayJson when json fields are optional -
i writing scala restful api , using sprayjson parse json being passed in during post call. example, have json structure looks this:
{"a", "b", "c", "d", "e", "f", "g", "h"}
fields a, b, c , h required others not. have custom json formatter case class. various reasons, way need structure case class requires me custom json formatter.
here code snippet of read function in formatter:
def read(value: jsvalue) = { value.asjsobject.getfields("a", "b", "c", "d", "e", "f", "g", "h") case seq(jsstring(a),jsstring(b),jsstring(c),jsstring(d),jsstring(e),jsstring(f),jsstring(g),jsstring(h)) new object(a,b,c,d,e,f,g,h) case _ => throw new deserializationexception("object expected") }
how can implement above without having numerous case strings matching every possible permutation of fields may come in?
im not familiar spray-json, if not existed fields treated kind of jnull
can try this:
implicit def jsvaluetostring(v: jsvalue): string = v match { case jsstring(s) => s case _ => null } ... case seq(jsstring(a),jsstring(b),jsstring(c), dopt, eopt,fopt,gopt,jsstring(h)) => new object(a,b,c,dopt,eopt,fopt,gopt,h)
Comments
Post a Comment