1

Stumped on this one. Working with PlayJSON and their JsObject.

Wart remover is marking the map call with a carrot(^) and saying at that line Inferred type containing Serializable. Code is functional and working but wondering more about why tool is flagging this and how to remove this error.

    def getPrintVersionOfJsonObj(jsObj: JsObject): JsObject = {
     val fieldValueMap: Seq[(String,JsValue)] = jsObj.fields.map {
      case (fieldName, fieldValue: JsNumber)  => (fieldName, fieldValue)
      case (fieldName, fieldValue: JsBoolean) => (fieldName, fieldValue)
      case (fieldName, fieldValue: JsString)  => (fieldName, fieldValue)
      case (fieldName, fieldValue: JsArray)   => (fieldName, convertJsArrayToPrettyString(fieldValue))
      case (fieldName, fieldValue: JsObject)  => (fieldName, getPrintVersionOfJsonObj(fieldValue))
      case (fieldName, fieldValue: JsValue)   => (fieldName, JsString(Json.prettyPrint(fieldValue)))
  }

  JsObject(fieldValueMap)
}

JSObject.fields is scala.collection.Seq[scala.Tuple2[scala.Predef.String, play.api.libs.json.JsValue]]

What I find confusing is you have known return type for that val fieldValueMap and you know the type of jsObj.fields

Barry
  • 1,800
  • 2
  • 25
  • 46

1 Answers1

0

Are you sure it even compiles (I don't see how if it does)?

The first (for example) case of your match, returns (String, JsNumber), while the fourth one returns (what seems to be) (String, String). The only common super type of these two types is Serializable, so, that's what it ends up being.

Dima
  • 39,570
  • 6
  • 44
  • 70
  • the fourth one returns ```(String,JsString)``` and JsString is ```case class JsString(value: String) extends JsValue``` – Barry Mar 29 '16 at 20:18
  • In that case, wartremover is wrong (provided that next line also returns a `JsValue`). Does the code actually compile? – Dima Mar 29 '16 at 20:36
  • yea compiles and works as intended recently we added wart remover to the project and was trying to work through the issues – Barry Mar 29 '16 at 21:31