Mahdi
Mahdi

Reputation: 1852

Support for Scala Enumeration by net.liftweb.json

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.

Upvotes: 0

Views: 365

Answers (2)

Radu Linu
Radu Linu

Reputation: 1261

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

Upvotes: 0

Shadowlands
Shadowlands

Reputation: 15074

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.

Upvotes: 3

Related Questions