All Downloads are FREE. Search and download functionalities are using the official Maven repository.

tethys.StringEnumJsonReader.scala Maven / Gradle / Ivy

The newest version!
package tethys

import tethys.readers.{FieldName, ReaderError}
import tethys.readers.tokens.TokenIterator

trait StringEnumJsonReader[A] extends JsonReader[A]

object StringEnumJsonReader:
  inline def derived[A <: scala.reflect.Enum]: StringEnumJsonReader[A] =
    new StringEnumJsonReader[A]:
      def read(it: TokenIterator)(implicit fieldName: FieldName): A =
        if it.currentToken().isStringValue then
          val res = it.string()
          it.next()
          try derivation.EnumCompanion.getByName[A](res)
          catch
            case ex: NoSuchElementException =>
              ReaderError.wrongJson(s"Unknown enum name: $res")
        else
          ReaderError.wrongJson(
            s"Expected string value but found: ${it.currentToken()}"
          )




© 2015 - 2024 Weber Informatics LLC | Privacy Policy