2014-03-04 16 views
1

我有一個名爲RDV的情況下類:如何保存的情況下類的列表中階

case class Rdv(
    id: Option[Int], 
    nom: String, 
    prénom: String, 
    sexe: Int, 
    telPortable: String, 
    telBureau: String, 
    telPrivé: String, 
    siteRDV: String, 
    typeRDV: String, 
    libelléRDV: String, 
    numRDV: String, 
    étape: String, 
    dateRDV: Long, 
    heureRDVString: String, 
    statut: String, 
    orderId: String) 

,我想保存在磁盤上的這些元素的列表,並重新加載它們。 我試圖用Java類(ObjectOutputStream的,FileOutputStream中,ObjectInputStream的,的FileInputStream),但我在檢索步驟的錯誤:語句

val n2 = ois.readObject().asInstanceOf[List[Rdv]] 

總是得到一個錯誤(ClassNotFound的:RDV),但正確的路徑給出在進口地方。

您知道解決方法來保存這樣的對象嗎? 請提供一小段代碼!

感謝

奧利弗

PS:我有相同的錯誤,同時使用馬歇爾類,如在此代碼:在帶箭頭的線

object Application extends Controller { 

def index = Action { 

//implicit val Rdv2Writes = Json.writes[rdv2] 

def rdvTordv2(rdv: Rdv): rdv2 = new rdv2(
    rdv.nom, 
    rdv.prénom, 
    rdv.dateRDV, 
    rdv.heureRDVString, 
    rdv.telPortable, 
    rdv.telBureau, 
    rdv.telPrivé, 
    rdv.siteRDV, 
    rdv.typeRDV, 
    rdv.libelléRDV, 
    rdv.orderId, 
    rdv.statut) 




val n = variables.manager.liste_locale 
val out = new FileOutputStream("out") 
out.write(Marshal.dump(n)) 
out.close 

val in = new FileInputStream("out") 
val bytes = Stream.continually(in.read).takeWhile(-1 !=).map(_.toByte).toArray 
val bar: List[Rdv] = Marshal.load[List[Rdv]](bytes)  <-------------- 

val n3 = bar.map(rdv => 
    rdvTordv2(rdv)) 
println("n3:" + n3.size) 




Ok(views.html.Application.olivier2(n3)) 

} 

}, 

。 似乎轉換爲List [Rdv]類型遇到問題,但爲什麼?這是一個戲劇!鏈接問題?


OK,有一個與播放一個問題: 我創建了一個新的Scala的項目,此代碼:

object Test1 extends App { 

//pour des fins de test 
case class Person(name:String,age:Int) 
val liste_locale=List(new Person("paul",18)) 

val n = liste_locale 
val out = new FileOutputStream("out") 
out.write(Marshal.dump(n)) 
out.close 

val in = new FileInputStream("out") 
val bytes = Stream.continually(in.read).takeWhile(-1 !=).map(_.toByte).toArray 
val bar: List[Person] = Marshal.load[List[Person]](bytes) 
println(s"bar:size=${bar.size}") 
} 

和顯示效果還是不錯( 「欄:大小= 1」)。

然後,我修改了上面的代碼在劇中的項目,在控制類,比如這個:

object Application extends Controller { 

def index = Action { 

//pour des fins de test 
case class Person(name:String,age:Int) 
val liste_locale=List(new Person("paul",18)) 

val n = liste_locale 
val out = new FileOutputStream("out") 
out.write(Marshal.dump(n)) 
out.close 

val in = new FileInputStream("out") 
val bytes = Stream.continually(in.read).takeWhile(-1 !=).map(_.toByte).toArray 
val bar: List[Person] = Marshal.load[List[Person]](bytes) 
println(s"bar:size=${bar.size}") 


Ok(views.html.Application.olivier2(Nil)) 

} 

} 

,我有一個錯誤說:

play.api.Application$$anon$1: Execution exception[[ClassNotFoundException: controllers.Application$$anonfun$index$1$Person$3]] 

有任何人有答案?

編輯:我認爲錯誤可能來自SBT,所以我修改build.scala像這樣:

import sbt._ 
import Keys._ 
import play.Project._ 

object ApplicationBuild extends Build { 

val appName = "sms_play_2" 
val appVersion = "1.0-SNAPSHOT" 

val appDependencies = Seq(
// Add your project dependencies here, 
jdbc, 
anorm, 
"com.typesafe.slick" % "slick_2.10" % "2.0.0", 
"com.github.nscala-time" %% "nscala-time" % "0.6.0", 
"org.xerial" % "sqlite-jdbc" % "3.7.2", 
"org.quartz-scheduler" % "quartz" % "2.2.1", 
"com.esotericsoftware.kryo" % "kryo" % "2.22", 
"io.argonaut" %% "argonaut" % "6.0.2") 

val mySettings = Seq(
(javaOptions in run) ++= Seq("-Dconfig.file=conf/dev.conf")) 

val playCommonSettings = Seq(

Keys.fork := true) 

val main = play.Project(appName, appVersion, appDependencies).settings(

    Keys.fork in run := true, 
resolvers += Resolver.sonatypeRepo("snapshots")).settings(mySettings: _*) 
.settings(playCommonSettings: _*) 

} 

但沒有成功,錯誤仍然存​​在(類人未找到)

你能幫助我嗎?

+0

你可以序列化成一些可解析的非二進制格式,比如JSON,這通常是更好的選擇(並不總是)。 –

+0

我不相信「給出正確的道路」。路徑必須包含您正在反序列化的類。 –

+0

@ som-snytt:我已經在Marshall的版本中導入了metier.Objets.Rdv !!! help! – lolveley

回答

0

Scala Pickling有合理的勢頭,這種方法有很多優點(很多繁重的工作都是在編譯時完成的)。有一個可插入的序列化機制,支持json等格式。

相關問題