Scala Play Framework JSON JsNull using json4s -


i'm new scala. how handle jsnull value in code?

i'm using json4s convert json map. should somehow converting jsnull option?

example:

play json : creating json

val jsona: jsvalue = json.obj(       "name" -> "bob",       "location" -> "irvine",       "resident" -> "no",       "nick-name" -> "bigwig",       "age" -> "6",       "role" -> jsnull,       "car" -> "bmw",       "multiple-residents" -> jsarray(seq(         jsobject(seq(           "name" -> jsstring("fiver"),           "age" -> jsnumber(4),           "role" -> jsobject(seq(                       "position" -> jsstring("fiver"),                       "" -> jsnumber(4),                       "role" -> jsstring("janitor")                     ))         ))       )) ) 

json4s : parsing json

var jsonamap:map[string, any] = map()   val jsonastring: string = json.stringify(jsona)   jsonamap = jsonstrtomap(jsonastring)  

after jsvalue converted map using json4s looks this:

map(name -> bob, location -> irvine, role -> null, resident -> no, car -> bmw, multiple-residents -> list(map(name -> fiver, age -> 4, role -> map(position -> fiver,  -> 4, role -> janitor))), age -> 6, nick-name -> bigwig) 

when create create list of values, end null value in list. once i'm pattern matching values of list end trying patter match null not possible (i'm sure i'm not supposed use card cases, i'm learning) :

for(i <- 0 beforevalslist.length - 1){   beforevalslist(i) match {      case _ : map[_,_] =>        comparejson(         beforevalslist(i).asinstanceof[map[string,any]],          aftervalslist(i).asinstanceof[map[string,any]],          rdeltabefore, rdeltaafter, samekeylist(i).tostring()       )     case _ if (beforevalslist(i) != aftervalslist(i)) =>        // if i'm recursion, build new map , add me        // deltas key->value pair       rdeltabefore += samekeylist(i).tostring -> beforevalslist(i)       rdeltaafter += samekeylist(i).tostring -> aftervalslist(i)     case _ =>        println("catch all: " + beforevalslist(i).tostring          + " " + aftervalslist(i).tostring)   }  } 

json4s converts jsnull null. should null check:

if(!beforevalslist(i) == null){       beforevalslist(i) match{...} } 

or there way me change null option when i'm putting values map list?

i'm not sure best practices , why jsno4s changes jsnull null instead of option, , whether or not that's possible.

cheers.

i'm still not sure how handle jsnull, null (or none if use option), function difference between map[string, any] before , after can simplified :

type jsonmap = map[string, any]  def getmapdiffs(mapbefore: jsonmap, mapafter: jsonmap) : (jsonmap, jsonmap) = {   val samekeys = mapbefore.keyset intersect mapafter.keyset   val startacc = (map.empty[string, any], map.empty[string, any])   samekeys.foldleft(startacc){ case (acc @ (deltabefore, deltaafter), key) =>     (mapbefore(key), mapafter(key)) match {       // 2 maps -> add map diff recursively before diff , after diff       case (beforemap: map[_, _], aftermap: map[_, _]) =>         val (deltab, deltaa) =            getmapdiffs(beforemap.asinstanceof[jsonmap], aftermap.asinstanceof[jsonmap])         (deltabefore + (key -> deltab), deltaafter + (key -> deltaa))       // values before , after different       // add values before diff , after diff       case (beforevalue, aftervalue) if beforevalue != aftervalue =>         (deltabefore + (key -> beforevalue), deltaafter + (key -> aftervalue))       // keep existing diff         case _ => acc     }     } } 

which can used as:

val (mapbefore, mapafter) = (   map("a" -> "alpha", "b" -> "beta", "c" -> "gamma", "d" -> map("e" -> "epsilon")),   map("a" -> "alpha", "b" -> list("beta"), "c" -> null, "d" -> map("e" -> 3)) )  val (deltabefore, deltaafter) = getmapdiffs(mapbefore, mapafter) // deltabefore: jsonmap = map(b -> beta, c -> gamma, d -> map(e -> epsilon)) // deltaafter: jsonmap = map(b -> list(beta), c -> null, d -> map(e -> 3))  deltabefore.tolist // list[(string, any)] = list((b,beta), (c,gamma), (d,map(e -> epsilon))) 

Comments