2017-10-13 6 views
0

私はtoJsonをScalaクラスで使用することができません。空のjson文字列を出力します。 :{}gensonを使用してスカラークラスをシリアライズできません

クラスにはメンバー変数が設定されています。 私はカスタムの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() 
    ) 


} 

し、メイン収納したオブジェクトを()

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

あなたが実際のコードをすべて投稿したのですが、最新のバージョンではこれまでのところうまく動作していますか(nあなたはここに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))) 
} 

上記の例を実行してみてください。

関連する問題