How to resolve inferred type containing Serializable

1.6k views Asked by At

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

1

There are 1 answers

3
Dima On

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.