JsZipper is a new tool allowing much more complex & powerful manipulations of Json structures for Play2/Json Scala API (not a part of Play2 core for now)
JsZipper is inspired by the Zipper concept introduced by Gérard Huet in 1997.
The Zipper allows to update immutable traversable structures in an efficient way. Json is an immutable AST so it fits well. FYI, the Zipper behaves like a loupe that walks through each node of the AST (left/right/up/down) while keeping aware of the nodes on its left, its right and its upper. The interesting idea behind the loupe is that when it targets a node, it can modify and even delete the focused node. The analogy to the pants zipper is quite good too because when it goes down the tree, it behaves as if it was opening the tree to be able to drive the loupe through all nodes and when it goes up, it closes back the tree… I won’t tell more here, it would be too long.
JsZipper is a specific interpretation of Zipper concept for Play/Json API based on :
Scala Streams to go through / update / construct Json AST nodes in a lazy way
Monadic aspects to provide funnier ways of manipulating the Json AST (plz see below)
Please note, JsZipper is not an end in itself but a tool useful to provide new API to manipulate Json.
Let’s go to samples because it explains everything.
Let’s use Future as our Monad because it’s… coooool to do things in the future ;)
Imagine you call several services returning Future[JsValue] and you want to build/update a JsObject from it.
Until now, if you wanted to do that with Play2/Json, it was quite tricky and required some code.
It’s still draft so it can be improved but if you like it, don’t hesitate to comment and if people like it, it could become a part of Play Framework itself