简体   繁体   中英

How do I get the class of java.lang.Enum in scala?

I am new to Scala and I am converting some Java code to Scala that uses the Jackson library to handle JSON serialization. I ran into a problem in a Jackson SimpleModule implementation.

In Java, I would do this:

 addSerializer(Enum.class, new LowerEnumSerializer());

I thought it would be as easy as doing this in Scala:

addSerializer(classOf[Enum], new LowerEnumSerializer())

However, my compiler is complaining:

scala: class Enum takes type parameters addSerializer(classOf[Enum], new LowerEnumSerializer())

I am guessing that this is because Java's Enum looks like:

public abstract class Enum<E extends Enum<E>>

Any ideas?

EDIT

I can't use classOf[Enum[_]] because the addSerializer method looks like this:

public <T> SimpleModule addSerializer(Class<? extends T> type, JsonSerializer<T> ser)

EDIT 2 I can't use classOf[Enum[_ <: Enum[_]], I get:

Type mismatch, expected: JsonSerializer[_], actual: Class[Enum[_ <: Enum[_]]

JsonSerializer looks like:

public class LowerEnumSerializer extends StdScalarSerializer<Enum>

怎么样:

classOf[Enum[T] forSome { type T <: Enum[T] }]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM