Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Option 1 (Meta[Json]):
- trait JsonMeta {
- import io.circe.syntax._
- implicit val JsonMeta: Meta[Json] =
- Meta
- .other[PGobject]("jsonb")
- .xmap[Json](
- a => parse(a.getValue).leftMap[Json](e => throw e).merge,
- a => {
- val o = new PGobject
- o.setType("jsonb")
- o.setValue(a.pretty(Printer.noSpaces.copy(dropNullValues = true)))
- o
- }
- )
- def codecMeta[A: Encoder: Decoder: TypeTag]: Meta[A] =
- Meta[Json].xmap[A](_.as[A].fold[A](throw _, identity), _.asJson)
- }
- // Option 2 (Get/Put):
- import doobie.util.{Get, Put, Meta}
- import org.postgresql.util.PGobject
- import io.circe.parser.parse
- import io.circe.Json
- trait JsonMeta {
- import io.circe.syntax._
- import cats.implicits._
- implicit val jsonMeta: Meta[Json] =
- Meta.Advanced.other[PGobject]("json").timap[Json](
- a => parse(a.getValue).leftMap[Json](e => throw e).merge)(
- a => {
- val o = new PGobject
- o.setType("json")
- o.setValue(a.noSpaces)
- o
- }
- )
- }
- trait SettingMeta extends JsonMeta {
- implicit val settingGet: Get[Setting] = Get[Json].tmap(_.as[Setting].toOption.get)
- implicit val settingPut: Put[Setting] = Put[Json].tcontramap(_.asJson)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement