New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extracting json with case class gives error org.json4s.package$MappingException: Can't find ScalaSig for class com.example.app.FBGraphAPI$User$3 #125

Open
lovesh opened this Issue May 7, 2014 · 9 comments

Comments

5 participants
@lovesh

lovesh commented May 7, 2014

I am using case classes to extract json. I have a method that has code like this

def method(args: Map[String, String]): Map[String, Any] = {
   ......
   val json = parse(response.body)
   case class User(id: String, name: String, first_name: Option[String], last_name: Option[String],         gender: Option[String], link: Option[String], locale: Option[String], timezone: Option[Double], birthday: Option[String])
   val extractedUser = json.extract[User]
   ......
}

Now when i execute this method i get the error
org.json4s.package$MappingException: Can't find ScalaSig for class com.example.app.FBGraphAPI$User$3 at org.json4s.reflect.ScalaSigReader$.findClass(ScalaSigReader.scala:42) at org.json4s.reflect.ScalaSigReader$.readConstructor(ScalaSigReader.scala:26) at org.json4s.reflect.Reflector$ClassDescriptorBuilder.ctorParamType(Reflector.scala:108) at org.json4s.reflect.Reflector$ClassDescriptorBuilder$$anonfun$6.apply(Reflector.scala:98) at org.json4s.reflect.Reflector$ClassDescriptorBuilder$$anonfun$6.apply(Reflector.scala:95) at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244) at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244) at scala.collection.immutable.List.foreach(List.scala:318) at scala.collection.TraversableLike$class.map(TraversableLike.scala:244) at scala.collection.AbstractTraversable.map(Traversable.scala:105) at org.json4s.reflect.Reflector$ClassDescriptorBuilder.ctorParamType(Reflector.scala:95) at org.json4s.reflect.Reflector$ClassDescriptorBuilder$$anonfun$constructorsAndCompanion$3$$anonfun$13.apply(Reflector.scala:149) at org.json4s.reflect.Reflector$ClassDescriptorBuilder$$anonfun$constructorsAndCompanion$3$$anonfun$13.apply(Reflector.scala:122) at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244) at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244) at scala.collection.mutable.ResizableArray$class.foreach(ResizableArray.scala:59) at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:47) at scala.collection.TraversableLike$class.map(TraversableLike.scala:244) at scala.collection.AbstractTraversable.map(Traversable.scala:105) at org.json4s.reflect.Reflector$ClassDescriptorBuilder$$anonfun$constructorsAndCompanion$3.apply(Reflector.scala:122) at org.json4s.reflect.Reflector$ClassDescriptorBuilder$$anonfun$constructorsAndCompanion$3.apply(Reflector.scala:116) at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244) at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244) at scala.collection.IndexedSeqOptimized$class.foreach(IndexedSeqOptimized.scala:33) at scala.collection.mutable.WrappedArray.foreach(WrappedArray.scala:34) at scala.collection.TraversableLike$class.map(TraversableLike.scala:244) at scala.collection.AbstractTraversable.map(Traversable.scala:105) at org.json4s.reflect.Reflector$ClassDescriptorBuilder.constructorsAndCompanion(Reflector.scala:116) at org.json4s.reflect.Reflector$ClassDescriptorBuilder.result(Reflector.scala:157) at org.json4s.reflect.Reflector$.createDescriptor(Reflector.scala:50) at org.json4s.reflect.Reflector$$anonfun$describe$1.apply(Reflector.scala:44) at org.json4s.reflect.Reflector$$anonfun$describe$1.apply(Reflector.scala:44) at org.json4s.reflect.package$Memo.apply(package.scala:39) at org.json4s.reflect.Reflector$.describe(Reflector.scala:44) at org.json4s.Extraction$.extract(Extraction.scala:304) at org.json4s.Extraction$.extract(Extraction.scala:42) at org.json4s.ExtractableJsonAstNode.extract(ExtractableJsonAstNode.scala:21) at com.example.app.FBGraphAPI$.getUserDetails(FBGraphAPI.scala:24) at .<init>(<console>:13) at .<clinit>(<console>) at .<init>(<console>:7) at .<clinit>(<console>) at $print(<console>) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:734) at scala.tools.nsc.interpreter.IMain$Request.loadAndRun(IMain.scala:983) at scala.tools.nsc.interpreter.IMain.loadAndRunReq$1(IMain.scala:573) at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:604) at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:568) at scala.tools.nsc.interpreter.ILoop.reallyInterpret$1(ILoop.scala:756) at scala.tools.nsc.interpreter.ILoop.interpretStartingWith(ILoop.scala:801) at scala.tools.nsc.interpreter.ILoop.command(ILoop.scala:713) at scala.tools.nsc.interpreter.ILoop.processLine$1(ILoop.scala:577) at scala.tools.nsc.interpreter.ILoop.innerLoop$1(ILoop.scala:584) at scala.tools.nsc.interpreter.ILoop.loop(ILoop.scala:587) at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply$mcZ$sp(ILoop.scala:878) at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:833) at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:833) at scala.tools.nsc.util.ScalaClassLoader$.savingContextLoader(ScalaClassLoader.scala:135) at scala.tools.nsc.interpreter.ILoop.process(ILoop.scala:833) at scala.tools.nsc.interpreter.ILoop.main(ILoop.scala:900) at xsbt.ConsoleInterface.run(ConsoleInterface.scala:69) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at sbt.compiler.AnalyzingCompiler.call(AnalyzingCompiler.scala:102) at sbt.compiler.AnalyzingCompiler.console(AnalyzingCompiler.scala:77) at sbt.Console.sbt$Console$$console0$1(Console.scala:23) at sbt.Console$$anonfun$apply$2$$anonfun$apply$1.apply$mcV$sp(Console.scala:24) at sbt.TrapExit$.sbt$TrapExit$$executeMain$1(TrapExit.scala:33) at sbt.TrapExit$$anon$1.run(TrapExit.scala:42)

Whats weird is when i go to the REPL and execute each line of the above method individually, the code works. What can be the difference between executing the code line by line in REPL vs executing it as a method?

@casualjim

This comment has been minimized.

Member

casualjim commented May 7, 2014

which scala version are you using and which version of json4s?
How does your build.sbt look ?

@lovesh

This comment has been minimized.

lovesh commented May 7, 2014

ScalaVersion = "2.10.3"
json4s-jackson = "3.2.6"

I dont have a build.sbt but i have a build.scala in my project folder and in build.scala's libraryDependencies i have the above

@casualjim

This comment has been minimized.

Member

casualjim commented May 7, 2014

can you show me the entire project? Or is it possible to post a reproduction somewhere?
Is the case class being defined in the method?

@lovesh

This comment has been minimized.

lovesh commented May 7, 2014

Yes the case class is defined in the method. See the 3 lines of code above. The 2nd line defines the case class.

@casualjim

This comment has been minimized.

Member

casualjim commented May 7, 2014

That is not supported in json4s. Only top level objects or objects defined
direclty in an object are supported


Met vriendelijke groeten - Best regards - Salutations
Ivan Porto Carrero - Mob: +1 (415) 323-8084
Web: http://flanders.co.nz
Twitter: http://twitter.com/casualjim

On Wed, May 7, 2014 at 12:58 PM, Lovesh Harchandani <
notifications@github.com> wrote:

Yes the case class is defined in the method. See the 3 lines of code
above. The 2nd line defines the case class.


Reply to this email directly or view it on GitHubhttps://github.com//issues/125#issuecomment-42475476
.

@lovesh

This comment has been minimized.

lovesh commented May 7, 2014

Thanks. It worked. I moved my case class definition outside the method and it worked. I am new to scala. Can you explain why case class cannot be defined inside a method for json4s only? Because its valid in scala. Thanks again.

@mdedetrich

This comment has been minimized.

mdedetrich commented Jul 16, 2014

@lovesh, its an issue with reflection. If you try to do reflection for a case class that is not an inner case class, then json4s needs to get the context of that class (via means of reflection) plus the manifest of the class itself

On the other hand, if the case class is top level, it doesn't require any context, only a manifest. Thats not to say its impossible to make it work for case classes that aren't inner objects, its just that the job is a lot harder

@PrashantParab01

This comment has been minimized.

PrashantParab01 commented Jan 25, 2017

object TestFields {

case class ProJson(eventProperties:Map[String,Any])

def main(args: Array[String]) {

  val json = parse("""
                  { "key":"01234",
                    "eventProperties":{
                      "unknownProperty1":"value",
                      "unknownProperty2":"value",
                      "unknownProperty3":"value"
                    }
                  }
                  """)
      implicit val formats = DefaultFormats
      val eventPropertiesMap= json.extract[ProJson]

}
Can we make eventPropertiesMap as a Json Object ?

@johnwonder

This comment has been minimized.

johnwonder commented Apr 12, 2017

study it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment