-
Notifications
You must be signed in to change notification settings - Fork 157
/
JsonCodecs.scala
41 lines (30 loc) · 1.2 KB
/
JsonCodecs.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Copyright (c) 2018-2021 by Rob Norris
// This software is licensed under the MIT License (MIT).
// For more information see LICENSE or https://opensource.org/licenses/MIT
package skunk
package circe.codec
import cats.syntax.all._
import io.circe.{ Json, Encoder => CEncoder, Decoder => CDecoder }
import io.circe.jawn.parse
import skunk.data.Type
trait JsonCodecs {
private def genCodec[A](tpe: Type)(
implicit encode: CEncoder[A],
decode: CDecoder[A]
): Codec[A] =
Codec.simple(
a => encode(a).noSpaces,
s => parse(s).flatMap(decode.decodeJson(_)).leftMap(_.getMessage),
tpe
)
/** Construct a codec for `A`, coded as Json, mapped to the `json` schema type. */
def json[A: CEncoder: CDecoder]: Codec[A] = genCodec[A](Type.json)
/** Construct a codec for `A`, coded as Json, mapped to the `jsonb` schema type. */
def jsonb[A: CEncoder: CDecoder]: Codec[A] = genCodec[A](Type.jsonb)
/** Codec for `Json` values, mapped to the `json` schema type. */
val json: Codec[Json] = json[Json]
/** Codec for `Json` values, mapped to the `jsonb` schema type. */
val jsonb: Codec[Json] = jsonb[Json]
}
object json extends JsonCodecs
object all extends JsonCodecs