Skip to content

aoiroaoino/json4s-optics

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

61 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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!

About

Providing optics operation for Json4s by Monocle.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages