Last active
July 20, 2016 10:36
-
-
Save hamnis/0904673ee014080f05d3f9afbc4eadd6 to your computer and use it in GitHub Desktop.
Implementation of rfc7396
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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