我遇到一个问题,我无法让toJson
使用Scala类。它输出一个空的json字符串。 :{}
这些类填充了成员变量。 我尝试了自定义genson类和默认的genson类。两者产生相同的输出。 但是,案例类工作正常。
我做错了吗?
下面的代码和输出我有几个看起来像这样的课程:
package gensontest
import com.owlike.genson.annotation.JsonProperty
class Person (@JsonProperty("name") var name:String ,@JsonProperty("age") var age:Int,@JsonProperty("address") var address: Address) extends Serializable{
}
package gensontest
import com.owlike.genson.annotation.JsonProperty
class Address (@JsonProperty("building") var building:Int, @JsonProperty("city") var city:String) extends Serializable{
}
package gensontest
import com.owlike.genson._
import com.owlike.genson.ext.json4s._
import org.json4s._
import org.json4s.JsonDSL._
import org.json4s.JsonAST._
object CustomGenson {
val customGenson = new ScalaGenson(
new GensonBuilder().useConstructorWithArguments(true).withBundle(ScalaBundle(),Json4SBundle()).create()
)
}
然后是一个包含main()
的对象package gensontest
import CustomGenson.customGenson._
//import com.owlike.genson.defaultGenson._
import org.json4s._
/**
* Hello world!
*
*/
object App{
def main(args: Array[String]) : Unit = {
case class Addresscc(street: String, building: Int)
case class Personcc(name: String, age: Int, address: Option[Address1])
case class Usercc(uId: Int)
var someAddr = new Address(157,"Paris")
var someone:Person = new Person("Eugen",28,someAddr)
val json = toJson(Personcc("foo bar", 28, Some(Addresscc("rue des lapins", 1337))))
val userInstance = new User(2)
val userInstance1 = new Usercc(2)
//check if Person's members are public
println(someone.name)
println(someone.age)
println(someone.address.building)
println(someone.address.city)
val personS = toJson(someone)
val addressS = toJson(someAddr)
val userInstanceS = toJson(userInstance)
val userInstanceSCC = toJson(userInstance1)
println(personS)
println(addressS)
println(userInstanceS)
println(json)
println(userInstanceSCC)
var p1:Person1 = fromJson[Person1](json)
var p:Person = fromJson[Person](personS)
}
}
输出结果为:
/Library/Java/JavaVirtualMachines/jdk1.8.0_141.jdk/Contents/Home/bin/java "-javaagent:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar=64224:/Applications/IntelliJ IDEA CE.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath /Library/Java/JavaVirtualMachines/jdk1.8.0_141.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_141.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_141.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_141.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_141.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_141.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_141.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_141.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_141.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_141.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_141.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_141.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_141.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_141.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_141.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_141.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_141.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_141.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_141.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_141.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_141.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_141.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_141.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_141.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_141.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_141.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_141.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_141.jdk/Contents/Home/lib/tools.jar:/Users/kartiku/IdeaProjects/gensontestscala1/target/classes:/Users/kartiku/.ivy2/cache/org.scala-lang/scala-library/jars/scala-library-2.11.8.jar:/Users/kartiku/.ivy2/cache/org.scala-lang/scala-reflect/jars/scala-reflect-2.11.8.jar:/Users/kartiku/.ivy2/cache/org.scala-lang/scala-library/srcs/scala-library-2.11.8-sources.jar:/Users/kartiku/.ivy2/cache/org.scala-lang/scala-reflect/srcs/scala-reflect-2.11.8-sources.jar:/Users/kartiku/.m2/repository/org/scala-lang/scala-library/2.11.8/scala-library-2.11.8.jar:/Users/kartiku/.m2/repository/org/json4s/json4s-core_2.11/3.5.0/json4s-core_2.11-3.5.0.jar:/Users/kartiku/.m2/repository/org/json4s/json4s-ast_2.11/3.5.0/json4s-ast_2.11-3.5.0.jar:/Users/kartiku/.m2/repository/org/json4s/json4s-scalap_2.11/3.5.0/json4s-scalap_2.11-3.5.0.jar:/Users/kartiku/.m2/repository/com/thoughtworks/paranamer/paranamer/2.8/paranamer-2.8.jar:/Users/kartiku/.m2/repository/org/scala-lang/modules/scala-xml_2.11/1.0.6/scala-xml_2.11-1.0.6.jar:/Users/kartiku/.m2/repository/com/owlike/genson-scala_2.11/1.4/genson-scala_2.11-1.4.jar:/Users/kartiku/.m2/repository/com/owlike/genson/1.4/genson-1.4.jar:/Users/kartiku/.m2/repository/org/scala-lang/scala-reflect/2.11.0/scala-reflect-2.11.0.jar gensontest.App
objc[54099]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_141.jdk/Contents/Home/bin/java and /Library/Java/JavaVirtualMachines/jdk1.8.0_141.jdk/Contents/Home/jre/lib/libinstrument.dylib. One of the two will be used. Which one is undefined.
Eugen
28
157
Paris
{}
{}
{}
{"address":{"building":1337,"street":"rue des lapins"},"age":28,"name":"foo bar"}
{"uId":2}
Process finished with exit code 0
答案 0 :(得分:0)
不确定您是否发布了所有实际代码但是使用最新版本它对我来说工作正常(请注意,你不要 这里需要JsonProperty注释。)
import com.owlike.genson._
import com.owlike.genson.annotation.JsonProperty
class Pers(var f: Int, @JsonProperty("b") var b: Int)
object Test extends App {
val genson = new ScalaGenson(new GensonBuilder().withBundle(ScalaBundle()).create())
println(genson.toJson(new Pers(1, 2)))
}
尝试运行上面的示例。