jsonscalacirce

How to pattern match a io.circe.Json object?


I want to compare two Jsons. For that I created a recursive function that uses Pattern matching.

Something like this:

(json1, json2) match {
  case (obj1: JsonObject, obj2: JsonObject) =>
    ...
  case (arr1: JsonArray, arr2: JsonArray) =>
    ...
  case _ =>
    ...
}

json1 and json2 are from the type io.circe.Json.

I tried different things:

My working solution uses the provided if-checks, like case _ if expJson.isArray && resJson.isArray =>.

Is there a better way?


Solution

  • We're not supposed to match.

    You can use methods Json#fold, Json#arrayOrObject, Json#foldWith or Json#isArray/asArray/isObject/asObject

    json1.arrayOrObject(
      handleRest(),
      arr1 => json2.arrayOrObject(
        handleRest(),
        arr2 => handleArrays(arr1, arr2),
        _    => handleRest()
      ),
      obj1 => json2.arrayOrObject(
        handleRest(),
        _    => handleRest(),
        obj2 => handleObjects(obj1, obj2)
      )
    )
    
    def handleObjects(obj1: JsonObject, obj2: JsonObject) = ???
    def handleArrays(arr1: Vector[Json], arr2: Vector[Json]) = ???
    def handleRest() = ???
    

    Classes JNull, JBoolean, JNumber, JString, JArray, JObject are package-private (the package is io.circe). If you really want to access them and match you can write some your code inside the package io.circe.