0

I am using the liftweb JSON converter and got it working, by including the dependency in build.sbt like this:

"net.liftweb" %% "lift-json" % "2.6.2"

This all works before I added Enumerations. I can see here that Enumerations are supported, and you should do something like this:

// Scala enums
implicit val formats = net.liftweb.json.DefaultFormats + new EnumSerializer(MyEnum)

But the problem is in my environment the net.liftweb.json.ext package is not recognized. This is the package where EnumSerializer lives.

Mahdi
  • 1,778
  • 1
  • 21
  • 35

2 Answers2

3

There is a separate extensions lib that you would need to include. Adding an extra line something like:

"net.liftweb" %% "lift-json-ext" % "2.6.2"

should do the trick.

Shadowlands
  • 14,994
  • 4
  • 45
  • 43
0

I had an enumeration that was created by the gRPC proto and in that case the EnumSerializer didn't work for me. In that case, I created a custom serializer and worked awesome.

case object GrpcTimeUnitSerializer extends CustomSerializer[TimeUnit] (format => (
  {
    case JString(tu) => TimeUnit.fromName(tu.toUpperCase).get
    case JNull       => throw new GrpcServiceException(Status.INTERNAL.withDescription("Not allowed null value for the type TimeUnit."))
  },
  {
    case tu: TimeUnit => JString(tu.toString)
  }
))

And here is the DefaultFormats definition:

implicit val formats: Formats = DefaultFormats + GrpcTimeUnitSerializer
Radu Linu
  • 1,143
  • 13
  • 29