Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom JsonCoproductCodec #7

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/main/scala/argonaut/custom/CustomGenericDecodeJsons.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ object CustomGenericDecodeJsons {
tail <- c.as(tailDecode.value)
} yield field[K](head) :: tail
}

implicit def customCConsAsJsObjectDecodeJson[K <: Symbol, H, T <: Coproduct](implicit
key: Witness.Aux[K],
headDecode: Lazy[DecodeJson[H]],
tailDecode: Lazy[DecodeJson[T]],
cc: JsonCoproductCodec
): DecodeJson[FieldType[K, H] :+: T] =
DecodeJson { c =>
val inl: Option[DecodeResult[FieldType[K, H] :+: T]] =
cc.attemptDecode(key.value.name, headDecode.value, c.focus)
.map(_.map(field[K](_)).map(Inl(_)))
inl.getOrElse(c.as(tailDecode.value).map(Inr(_)))
}
}

trait CustomGenericDecodeJsons {
Expand All @@ -38,9 +51,10 @@ trait CustomGenericDecodeJsons {
implicit def cconsDecodeJson[K <: Symbol, H, T <: Coproduct](implicit
key: Witness.Aux[K],
headDecode: Lazy[DecodeJson[H]],
tailDecode: Lazy[DecodeJson[T]]
tailDecode: Lazy[DecodeJson[T]],
cc: JsonCoproductCodec
): DecodeJson[FieldType[K, H] :+: T] =
GenericDecodeJsons.cconsAsJsObjectDecodeJson(key, headDecode, tailDecode)
CustomGenericDecodeJsons.customCConsAsJsObjectDecodeJson(key, headDecode, tailDecode, cc)

implicit def instanceDecodeJson[F, G](implicit
gen: LabelledGeneric.Aux[F, G],
Expand Down
16 changes: 14 additions & 2 deletions src/main/scala/argonaut/custom/CustomGenericEncodeJsons.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ object CustomGenericEncodeJsons {
EncodeJson { case h :: t =>
pc.encodeField(key.value.name, headEncode.value.encode(h), tailEncode.value.encode(t))
}

implicit def customCConsAsJsObjectEncodeJson[K <: Symbol, H, T <: Coproduct](implicit
key: Witness.Aux[K],
headEncode: Lazy[EncodeJson[H]],
tailEncode: Lazy[EncodeJson[T]],
cc: JsonCoproductCodec
): EncodeJson[FieldType[K, H] :+: T] =
EncodeJson {
case Inl(h) => cc.encode(key.value.name, headEncode.value.encode(h))
case Inr(t) => tailEncode.value.encode(t)
}
}

trait CustomGenericEncodeJsons {
Expand All @@ -33,9 +44,10 @@ trait CustomGenericEncodeJsons {
implicit def cconsEncodeJson[K <: Symbol, H, T <: Coproduct](implicit
key: Witness.Aux[K],
headEncode: Lazy[EncodeJson[H]],
tailEncode: Lazy[EncodeJson[T]]
tailEncode: Lazy[EncodeJson[T]],
cc: JsonCoproductCodec
): EncodeJson[FieldType[K, H] :+: T] =
GenericEncodeJsons.cconsJsObjectEncodeJson(key, headEncode, tailEncode)
CustomGenericEncodeJsons.customCConsAsJsObjectEncodeJson(key, headEncode, tailEncode, cc)

implicit def instanceEncodeJson[F, G](implicit
gen: LabelledGeneric.Aux[F, G],
Expand Down
18 changes: 18 additions & 0 deletions src/main/scala/argonaut/custom/JsonCoproductCodec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package argonaut.custom

import argonaut.{DecodeJson, Json, DecodeResult}

trait JsonCoproductCodec {
def encode(typeName: String, json: Json): Json
def attemptDecode[T](typeName: String, decodeJson: DecodeJson[T], json: Json): Option[DecodeResult[T]]
}

object JsonCoproductCodec {
implicit val defaultJsonCoproductCodec =
new JsonCoproductCodec {
def attemptDecode[T](typeName: String, decodeJson: DecodeJson[T], json: Json): Option[DecodeResult[T]] =
json.field(typeName).map(decodeJson.decodeJson)
def encode(typeName: String, json: Json): Json =
Json.obj(typeName -> json)
}
}