Navigation | Overlay |
---|---|
t Navigate files | h Toggle hits |
y Change url to tip of branch | m Toggle misses |
b / v Jump to prev/next hit line | p Toggle partial |
z / x Jump to prev/next missed or partial line | 1..9 Toggle flags |
shift + o Open current page in GitHub | a Toggle all on |
/ or ? Show keyboard shortcuts dialog | c Toggle context lines or commits |
1 |
package hammock |
|
2 |
package circe |
|
3 |
|
|
4 |
import cats.implicits._ |
|
5 |
import io.circe.{Encoder => CirceEncoder, Decoder => CirceDecoder} |
|
6 |
import io.circe.parser.{decode => circeDecode} |
|
7 |
import io.circe.syntax._ |
|
8 |
|
|
9 |
class HammockEncoderForCirce[A: CirceEncoder] extends Encoder[A] { |
|
10 |
override def encode(a: A): Entity = |
|
11 | 1 |
Entity.StringEntity(a.asJson.noSpaces, ContentType.`application/json`) |
12 |
}
|
|
13 |
|
|
14 |
class HammockDecoderForCirce[A: CirceDecoder] extends Decoder[A] { |
|
15 |
override def decode(entity: Entity): Either[CodecException, A] = entity match { |
|
16 |
case Entity.StringEntity(str, _) => |
|
17 | 1 |
circeDecode[A](str).left.map(err => CodecException.withMessageAndException(err.getMessage, err)) |
18 |
case _: Entity.ByteArrayEntity => |
|
19 |
CodecException.withMessage("unable to decode a ByteArrayEntity. Only StringEntity is supported").asLeft |
|
20 |
case Entity.EmptyEntity => |
|
21 |
CodecException.withMessage("unable to decode an EmptyEntity. Only StringEntity is supported").asLeft |
|
22 |
}
|
|
23 |
}
|
|
24 |
|
|
25 |
class HammockCodecForCirce[A: CirceEncoder: CirceDecoder] extends Codec[A] { |
|
26 |
override def encode(a: A): Entity = |
|
27 | 1 |
new HammockEncoderForCirce[A].encode(a) |
28 |
override def decode(entity: Entity): Either[CodecException, A] = |
|
29 | 1 |
new HammockDecoderForCirce[A].decode(entity) |
30 |
}
|
Read our documentation on viewing source code .