Skip to content

Mapping over codecs #1284

@kamilkloch

Description

@kamilkloch

What do you think about baking such an extension into jsoniter? Being able to map over codecs is quite helpful.

extension [A](underlying: JsonValueCodec[A]) {
    def imap[B](f: A => B)(g: B => A): JsonValueCodec[B] = new JsonValueCodec[B] {
      def decodeValue(in: JsonReader, default: B): B = {
        f(underlying.decodeValue(in, underlying.nullValue))
      }

      def encodeValue(x: B, out: JsonWriter): Unit = {
        underlying.encodeValue(g(x), out)
      }


      def nullValue: B = f(underlying.nullValue)
    }
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions