Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8108923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:15:38+00:00 2026-06-06T01:15:38+00:00

Following on from: Scala classOf for type parameter I’ve tried implementing it but seem

  • 0

Following on from:

Scala classOf for type parameter

I’ve tried implementing it but seem to get some weird generics problem, actually I’ve muddled my way through a couple of them now, but this is as close to correct I can get it…

I’m using the Scala – Jackson JSON bindings (excellent lib by the way, much easier than SJson)

def genparseResult[T: ClassManifest](t: T,s:String):Either[Tuple2[JsonParseException,String],T] = {
  try{
    val res = jsonSerializer.readValue(s,  classManifest[T].erasure)
    Right(res)
  }
  catch{
    case jpe:JsonParseException => Left((jpe,s))
  }
}

Anyhow, the code above is generating the following compile error:

type mismatch; found : res.type (with underlying type Any) required: T

I’m confused as hell. Should the code above be able to work?

Update
following input from tenshi, I post the completed class

import com.fasterxml.jackson.core.JsonParseException
object DatasiftJsonMapper {
  import java.util.Date
  import com.fasterxml.jackson.databind.{ Module, ObjectMapper }
  import com.fasterxml.jackson.module.scala.DefaultScalaModule

  val jsonSerializer = {
    val m = new ObjectMapper()
    m.registerModule(DefaultScalaModule)
    m
  }

  def parseDSResult(s: String): Either[Tuple2[JsonParseException, String], DatasiftResult] = {
    genparseResult(classOf[DatasiftResult], s)
  }

  def parseQRegRequest(s: String): Either[Tuple2[JsonParseException, String], QRegRequest] = {
    genparseResult(classOf[QRegRequest], s)
  }

  def genparseResult[T: ClassManifest](t: Class[T], s: String): Either[Tuple2[JsonParseException, String], T] = {
    try {
      val res = jsonSerializer.readValue(s, classManifest[T].erasure).asInstanceOf[T]
      Right(res)
    } catch {
      case jpe: JsonParseException => Left((jpe, s))
    }
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T01:15:39+00:00Added an answer on June 6, 2026 at 1:15 am

    As far as I remember, classManifest[T].erasure returns Class[_] instead of Class[T], so the result of jsonSerializer.readValue(...) would be or type Any. You can try to cast parsing result:

    val res = jsonSerializer.readValue(s,  classManifest[T].erasure).asInstanceOf[T]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using the following code: sendMail(From(Props.get(email)), Subject(Test Email), To(email@address), PlainMailBodyType(test email body)); And in Boot.scala:
Is the following From header incorect? // To send HTML mail, the Content-type header
Sometimes I get the following from SQL Server 2005 when executing a stored procedure:
Why can't I access 'B' in the following from 'A' but can from the
I have to translate the following code from Java to Scala: EDIT: added if-statements
Following code is from my REPL: scala> words.zipWithIndex.filter((x:java.lang.String,index:Int)=>index%2==0) <console>:9: error: type mismatch; found :
Idle pondering from a Scala learner perhaps, but ... in my tinkerings I've written
Following on from another question I asked, Scala 2.8 breakout , I wanted to
The following example is from the book 'Programming in Scala'. Given a class 'Rational'
The following example from A Tour of Scala shows how implicit can be used

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.