2

I am using Jackson read Yaml in scala 2.11/2.12 val mapper = new ObjectMapper(new YAMLFactory()), I think the Java constructor I am calling is

public <T> T readValue(String content, Class<T> valueType)

this code works

def load(): SomeClass {
  mapper.readValue[SomeClass](configStr, classOf[SomeClass])
}

But I want to change this class to T

mapper.readValue[T](configStr, classOf[T])

error class type required but T found

I searched some and changed it to

def load[T: ClassTag](): T = {
  mapper.readValue[T](configStr, classTag[T].runtimeClass)
}

But it says no construct methods are matched.

1 Answer 1

3

The return type of classTag[T].runtimeClass is a Class[?], you have to use .asInstanceOf (safe in this usage):

mapper.readValue[T](configStr, classTag[T].runtimeClass.asInstanceOf[Class[T]])

Note that if you were using the Jackson Scala module, such methods are already provided to you in order to avoid the boilerplate

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.