Skip to content

Latest commit

 

History

History
57 lines (43 loc) · 1.48 KB

README.md

File metadata and controls

57 lines (43 loc) · 1.48 KB

Json4s Optics

Build Status

What is Json4s Optics?

The Json4s Optics is providing optical operation for Json4s by Monocle.

Quick Start

You need the following imports.

// Json4s
import org.json4s.JsonAST._

// Monocle
import monocle._, Monocle._

// This library
import com.github.aoiroaoino.json4s_optics.all._

If you have the following json value.

val json: JValue = JObject(
  "name"      -> JString("aoino"),
  "age"       -> JInt(25),
  "favorites" -> JArray(List(JString("scala"), JString("haskell")))
)

Get age value.

json applyPrism _jObject composeOptional index("age") composePrism _jInt getOption
//=> Some(25)

Capitalize name.

json applyPrism _jObject composeOptional index("name") composePrism _jString modify(_.capitalize)
//=> JObject("name" -> JString("Aoino"), "age" -> ...)

Of cource you can also use alias methods.

json &<-? _jObject ^|-? index("name") ^<-? _jString modify(_.capitalize)
//=> same as above

And easy traverse.

json &<-? _jObject ^|-? index("favorites") ^<-? _jArray ^|->> each ^<-? _jString modify(_.capitalize)
//=> JObject("name" -> ..., "favorites" -> JArray(List(JString("Scala"), JString("Haskell"))))

If you want to more samples, please look here!