Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support of merging JsObject's #135

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

ataraxer
Copy link

I believe that a simple fields Map's concatenation of two objects should achieve the expected behaviour.

Resolves #72.

@Twisterius
Copy link

LGTM

@johanatan
Copy link

Was this ever merged? Is there any other way to merge JsObjects in the current mainline?

@rleibman
Copy link

I'm using the following, which fits my needs better than a field replacement:

import spray.json.JsValue
import scala.annotation.tailrec

package object util {
  implicit class PimpedJsObject(obj: JsObject) {
    def merge(obj2: JsValue): JsObject = {

      def mergeLoop(val1: JsValue, val2: JsValue): JsValue = {
        (val1, val2) match {
          case (obj1: JsObject, obj2: JsObject) => {
            val map1 = obj1.fields
            val map2 = obj2.fields
            val diff1 = map1.keySet.diff(map2.keySet)
            val diff2 = map2.keySet.diff(map1.keySet)
            val intersect = map1.keySet.intersect(map2.keySet)
            val merged = intersect.map { key =>
              val val1 = map1(key)
              val val2 = map2(key)
              (val1, val2) match {
                case (val1: JsObject, val2: JsObject) => (key -> mergeLoop(val1, val2))
                case (_, val2) => (key -> val2)
              }
            }.toMap
            val distinct = map1.filter(t => diff1(t._1)) ++ map2.filter(t => diff2(t._1))
            JsObject(distinct ++ merged)
          }
          case (_, val2) => val2
        }
      }
      mergeLoop(obj, obj2).asJsObject
    }
  }
}```

…erge

# Conflicts:
#	src/main/scala/spray/json/JsValue.scala
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

JsObjects do not merge
5 participants