Skip to content

Instantly share code, notes, and snippets.

@hamnis
Last active July 20, 2016 10:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hamnis/0904673ee014080f05d3f9afbc4eadd6 to your computer and use it in GitHub Desktop.
Save hamnis/0904673ee014080f05d3f9afbc4eadd6 to your computer and use it in GitHub Desktop.
Implementation of rfc7396
import argonaut._, Argonaut._
object MergePatch {
def mergePatch(json: Json, patch: Json): Json = {
val target = json.objectOrEmpty.toMap
if (patch.isObject) {
patch.objectOrEmpty.toList.foldLeft(target) { case (map, (k, v)) =>
if (map.isDefinedAt(k)) {
if (v.isNull) map - k else map + (k -> mergePatch(map(k), v))
} else {
map + (k -> v)
}
}.asJson
} else patch
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment