Skip to content

Commit

Permalink
Add JsonCodec for AST (#1116)
Browse files Browse the repository at this point in the history
  • Loading branch information
guersam authored May 15, 2024
1 parent a1c964e commit 8ea1220
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions zio-json/shared/src/main/scala/zio/json/ast/ast.scala
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,8 @@ object Json {

override final def toJsonAST(a: Obj): Either[String, Json] = Right(a)
}

implicit val codec: JsonCodec[Obj] = JsonCodec(encoder, decoder)
}
final case class Arr(elements: Chunk[Json]) extends Json {
def isEmpty: Boolean = elements.isEmpty
Expand Down Expand Up @@ -434,6 +436,8 @@ object Json {

override final def toJsonAST(a: Arr): Either[String, Json] = Right(a)
}

implicit val codec: JsonCodec[Arr] = JsonCodec(encoder, decoder)
}
final case class Bool(value: Boolean) extends Json {
override def asBoolean: Some[Boolean] = Some(value)
Expand All @@ -460,6 +464,8 @@ object Json {

override final def toJsonAST(a: Bool): Either[String, Json] = Right(a)
}

implicit val codec: JsonCodec[Bool] = JsonCodec(encoder, decoder)
}
final case class Str(value: String) extends Json {
override def asString: Some[String] = Some(value)
Expand All @@ -482,6 +488,8 @@ object Json {

override final def toJsonAST(a: Str): Either[String, Json] = Right(a)
}

implicit val codec: JsonCodec[Str] = JsonCodec(encoder, decoder)
}
final case class Num(value: java.math.BigDecimal) extends Json {
override def asNumber: Some[Json.Num] = Some(this)
Expand Down Expand Up @@ -512,6 +520,8 @@ object Json {

override final def toJsonAST(a: Num): Either[String, Num] = Right(a)
}

implicit val codec: JsonCodec[Num] = JsonCodec(encoder, decoder)
}
type Null = Null.type
case object Null extends Json {
Expand All @@ -535,6 +545,8 @@ object Json {
override final def toJsonAST(a: Null.type): Either[String, Json] = Right(a)
}

implicit val codec: JsonCodec[Null.type] = JsonCodec(encoder, decoder)

override def asNull: Some[Unit] = Some(())
}

Expand Down Expand Up @@ -572,5 +584,7 @@ object Json {
override final def toJsonAST(a: Json): Either[String, Json] = Right(a)
}

implicit val codec: JsonCodec[Json] = JsonCodec(encoder, decoder)

def apply(fields: (String, Json)*): Json = Json.Obj(Chunk(fields: _*))
}

0 comments on commit 8ea1220

Please sign in to comment.