Skip to content

Commit

Permalink
More efficient (and less allocating on initialization) JSON codec for…
Browse files Browse the repository at this point in the history
… option (#781)
  • Loading branch information
plokhotnyuk authored Jan 11, 2025
1 parent eae0043 commit ff90ce2
Showing 1 changed file with 4 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -641,26 +641,23 @@ object JsonCodec {

private[schema] def option[A](A: ZJsonDecoder[A]): ZJsonDecoder[Option[A]] =
new ZJsonDecoder[Option[A]] {
private[this] val ull: Array[Char] = "ull".toCharArray

def unsafeDecode(trace: List[JsonError], in: RetractReader): Option[A] =
(in.nextNonWhitespace(): @switch) match {
case 'n' =>
Lexer.readChars(trace, in, ull, "null")
if (in.readChar() != 'u' || in.readChar() != 'l' || in.readChar() != 'l') error("expected 'null'", trace)
None
case _ =>
in.retract()
Some(A.unsafeDecode(trace, in))
}

override def unsafeDecodeMissing(trace: List[JsonError]): Option[A] =
None
override def unsafeDecodeMissing(trace: List[JsonError]): Option[A] = None

final override def unsafeFromJsonAST(trace: List[JsonError], json: Json): Option[A] =
json match {
case Json.Null => None
case Json.Obj(Chunk()) => None
case _ => Some(A.unsafeFromJsonAST(trace, json))
case Json.Null => None
case _ => Some(A.unsafeFromJsonAST(trace, json))
}
}

Expand Down

0 comments on commit ff90ce2

Please sign in to comment.