-
Notifications
You must be signed in to change notification settings - Fork 329
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
scala 2.10: getting "NoSuchFieldException: MODULE$" while extracting json into case class #22
Comments
i've found a workaround: the case class i defined is actually a nested one (inside class def). if i move it to the top level, the extraction works. |
I am currently experiencing the same issue which is kind of problematic when working in modular scenarios like e.g. import org.json4s._
import org.json4s.native.JsonMethods._
trait PersonModule {
case class Person(name: String, age: Int)
}
object Main extends App with PersonModule {
implicit val formats = DefaultFormats
val json = """{"name":"joe","age":15}"""
val p: Person = parse(json).extract[Person]
} Any suggestions (except moving the case class to the top-level) are highly welcome. |
which version of json4s are you using? I thought I fixed this in 3.2.1 |
I just checked with 3.2.1 and still get the following stack trace:
Thanks for reacting so quickly! |
Oh has 3.2.1 been released? |
@ngocdaothanh yes, but tomorrow there will be a 3.2.2, I'll update the version in the readme then. @afwlehmann In the current snapshot I've added support for case classes defined in a trait. But they do need custom formats. I'll explain why and then how. Why?For classes defined in a trait it's a bit difficult to get to their companion object, which is needed to provide default values. We could punt on those but that brings us to the next problem, the compiler generates an extra field in the constructor of such case classes. The first field in the constructor of those case classes is called How?I've chosen to extend the formats to include a list of companion mappings for those case classes. So you can have formats that belong to your modules and keep the mappings in there. That will then make default values work and provide the much needed trait SharedModule {
case class SharedObj(name: String, visible: Boolean = false)
}
object PingPongGame extends SharedModule
implicit val formats: Formats =
DefaultFormats.withCompanions(classOf[PingPongGame.SharedObj] -> PingPongGame)
val inst = PingPongGame.SharedObj("jeff", visible = true)
val extr = Extraction.decompose(inst)
extr must_== JObject("name" -> JString("jeff"), "visible" -> JBool(true))
extr.extract[PingPongGame.SharedObj] must_== inst The general gist of the implementation is in this commit: 96c5bbc I'll leave the ticket open for you too close if this works for you |
when we add proper support for macros this will be a non-issue, but until then this should work for your use case |
Wonderful! Just checked against 3.3.0-SNAPSHOT and it works like a charm. Would you consider adding your explanation including your example to the FAQ? Thank you very much for your instant help! P.S.: If I could I would close the ticket like you've suggested, but apparently that is only possible for the OP. |
yeah I'll do that tomorrow when I release this, I ran out of time for today :) |
How to parse Json in json4s using scala liborg.json4s json4s-native_2.11 3.5.0 org.json4s json4s-jackson_2.11 3.5.0{ val JSON = parse(json) iam getting result for List(Sony, Yolonda Jones) But iam expected result for like this for (name1 <- names) println(name1) for (msgid <- messagesIds) println(msgid) 56232316996 |
i am getting the following exception while doing the following in scala 2.10:
i did some digging and strangely enough, the static field named "MODULE$" that is always present in class whose name ends with '$' before scala 2.10 is no longer there. json4s seems to rely on it to do the extraction bits and hence the exception. i think the best course of action for 2.10 might be to use the new scala reflection api instead of the java one. however, it there a quick fix that will get me through the day?
The text was updated successfully, but these errors were encountered: