gnieh/diffson

for play json: validation type JsResult throws JsResultException

Closed this issue · 5 comments

example:

val str =
    """
      |[{"op_no_correct":"replace","path":"/somePath","value":"xxx"}]
    """.stripMargin

 Json.parse(str).validate[JsonPatch[JsValue]] match {
    case JsSuccess(d, _) =>
      println("Success")
    case JsError(_) =>
      println("Error")
  }

should be processed correctly in pattern matching, but this does not happen, and an exception occurs.
it seems that the problem is here:

https://github.com/gnieh/diffson/blob/master/playJson/shared/src/main/scala/diffson/playJson/DiffsonProtocol.scala#L170

P.S.
Sorry for my english i use translator.

Hi,

This is the expected behavior with the implementation you pointed out, because as[T] returns a T or throws an exception.

It is a bit weird that play-json does not have a safe version of as that returns a JsResult. I am not a big expert (or even fan) of the play-json API, but I guess we could rewrite it to be safer by first validating each op as an Operation[JsValue]. I will give it a shot.

Ok, so validate is the safe as I was thinking about, I got confused by the name... I will switch to that.

Published in last 4.0.0-SNAPSHOT. Your code now works I you expect it.

thank you very much, I will wait for release

Version 4.0.0-M3 is now available, let me know if it works for you.