Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON4S unknown error

i am trying to get json4s to extract something but i get a "unknown error"

My code looks like:

import org.json4s._
import org.json4s.jackson.JsonMethods._ 
implicit val formats = org.json4s.DefaultFormats

case class Person(name: String, age: Int)
val json = """{"name":"joe","age":15}"""

print(parse(json).extract[Person])

Error Path:

org.json4s.MappingException: unknown error
    at org.json4s.Extraction$.extract(Extraction.scala:50)
    at org.json4s.ExtractableJsonAstNode.extract(ExtractableJsonAstNode.scala:21)
    at Main$$anon$1.<init>(test.scala:8)
    at Main$.main(test.scala:1)
    at Main.main(test.scala)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at scala.tools.nsc.util.ScalaClassLoader$$anonfun$run$1.apply(ScalaClassLoader.scala:78)
    at scala.tools.nsc.util.ScalaClassLoader$class.asContext(ScalaClassLoader.scala:24)
    at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.asContext(ScalaClassLoader.scala:88)
    at scala.tools.nsc.util.ScalaClassLoader$class.run(ScalaClassLoader.scala:78)
    at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.run(ScalaClassLoader.scala:101)
    at scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:33)
    at scala.tools.nsc.ObjectRunner$.runAndCatch(ObjectRunner.scala:40)
    at scala.tools.nsc.ScriptRunner.scala$tools$nsc$ScriptRunner$$runCompiled(ScriptRunner.scala:171)
    at scala.tools.nsc.ScriptRunner$$anonfun$runScript$1.apply(ScriptRunner.scala:188)
    at scala.tools.nsc.ScriptRunner$$anonfun$runScript$1.apply(ScriptRunner.scala:188)
    at scala.tools.nsc.ScriptRunner$$anonfun$withCompiledScript$1.apply$mcZ$sp(ScriptRunner.scala:157)
    at scala.tools.nsc.ScriptRunner$$anonfun$withCompiledScript$1.apply(ScriptRunner.scala:131)
    at scala.tools.nsc.ScriptRunner$$anonfun$withCompiledScript$1.apply(ScriptRunner.scala:131)
    at scala.tools.nsc.util.package$.waitingForThreads(package.scala:26)
    at scala.tools.nsc.ScriptRunner.withCompiledScript(ScriptRunner.scala:130)
    at scala.tools.nsc.ScriptRunner.runScript(ScriptRunner.scala:188)
    at scala.tools.nsc.ScriptRunner.runScriptAndCatch(ScriptRunner.scala:201)
    at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:58)
    at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:80)
    at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:89)
    at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.lang.NoSuchFieldException: MODULE$
    at java.lang.Class.getField(Class.java:1520)
    at org.json4s.Meta$$anonfun$mappingOf$1$$anonfun$8.apply(Meta.scala:208)
    at org.json4s.Meta$$anonfun$mappingOf$1.apply(Meta.scala:207)
    at org.json4s.Meta$$anonfun$mappingOf$1.apply(Meta.scala:195)
    at org.json4s.Meta$Memo.memoize(Meta.scala:240)
    at org.json4s.Meta$.mappingOf(Meta.scala:195)
    at org.json4s.Extraction$.mkMapping$1(Extraction.scala:207)
    at org.json4s.Extraction$.org$json4s$Extraction$$extract0(Extraction.scala:214)
    at org.json4s.Extraction$.extract(Extraction.scala:47)
    ... 34 more

I run SCALA 2.9.2 with json4s 2.9.2:3.1.0 with the jackson parser 2.1.1

before i went to "json4s" i tryed jackson directly and got not even the the provided examples to work with extraction so i figure something is off here but not rly a clue where to start looking.

Thanks in advance for help :)

like image 682
Oliver Zachau Avatar asked Feb 02 '13 12:02

Oliver Zachau


1 Answers

In fact there's a difference where you place your case class code. Consider two examples:

class SomeServlet {
 def someMethod(json: String) = parse(json).extract[Person]
 case class Person(name: String, age: Int)
}

In this case class Person will have an implicit field with type SomeServlet (like Java non-static inner class) and thus won't correspond to given JSON. That's why you get an exception. Now look at the second example:

class SomeServlet {
 def someMethod(json: String) = parse(json).extract[Person]
}

case class Person(name: String, age: Int)

In the second example class Person is completely independent and thus has no implicit field. So it should work. At least works for me.

like image 157
vania-pooh Avatar answered Sep 20 '22 20:09

vania-pooh