我正在使用Scala - 我使用JacksMapper它工作正常, 这里是代码
import java.io.FileOutputStream
import java.io.ObjectOutputStream
import java.io.FileInputStream
import java.io.ObjectInputStream
import scala.util.Marshal
import java.io.ByteArrayOutputStream
import java.io.PrintWriter
import scala.util.parsing.json.JSONObject
import scala.util.parsing.json.JSON
import scala.util.parsing.json.JSONArray
import java.util.concurrent.atomic.AtomicReference
import scala.collection.mutable._
//import com.lambdaworks.jacks.JacksMapper
import java.io.BufferedReader
import java.io.FileReader
import net.liftweb.json._
import net.liftweb.json.JsonDSL._
import net.liftweb.json.JsonAST._
import net.liftweb.json.Extraction._
import net.liftweb.json.Printer._
//import com.codahale.jerkson.Json._
object jsonTest extends Serializable{
def main(args: Array[String]): Unit = {
var i = 1
val map = new HashMap[String, Any]() with scala.collection.mutable.SynchronizedMap[String, Any]
while(i < 10000)
{
var in_list = List[Any]()
in_list :+= "dummy"
in_list :+= "dummy"
in_list :+= "dummy"
val in_map = HashMap[String,Any]()
in_map("dummy"+i) = in_list
var out_list = List[Any]()
out_list :+= "cat1"
out_list :+= "hash1"
out_list :+= 100
out_list :+= (System.currentTimeMillis()/1000).toInt
out_list :+= in_map
map("dummy"+i) = out_list
i = i + 1
}
val json = JacksMapper.writeValueAsString[scala.collection.immutable.Map[String,Any]](map.toMap)
Some(new PrintWriter("foo.txt")).foreach{p => p.write(json); p.close}
val t1 = System.currentTimeMillis()
val br : BufferedReader = new BufferedReader(new FileReader("foo.txt"));
val sb:StringBuilder = new StringBuilder();
var line = br.readLine();
while (line != null) {
sb.append (line);
sb.append("\n");
line = br.readLine();
}
val content = sb.toString();
br.close()
println(content.length())
val obj = JacksMapper.readValue[scala.collection.immutable.Map[String,Any]](content)
val obj = scala.collection.immutable.Map[String,Any]
println(obj("dummy3"))
println(System.currentTimeMillis() - t1)
}
}
但我正在尝试使用Lift_Json进行scala。 请告诉我如何使用Lift_Json标签更改上述代码 我得到了Lift_Json的Jar文件,并使用简单的解析,如compact(render(json))。
答案 0 :(得分:2)
这样的事情应该能够完成你想要做的事情。它将生成您的测试Map
,然后将其序列化为JSON
字符串并将其写入文件。下一步将重新读取并将其提取到您可以读取的地图 - 如上例所示。
import net.liftweb.json._
import java.io._
implicit val formats = net.liftweb.json.DefaultFormats
val map = {
val mb = new scala.collection.mutable.HashMap[String, Any]()
(1 to 10000).foreach { i =>
val in_list = "dummy" :: "dummy" :: "dummy" :: Nil
val in_map = Map("dummy%s".format(i) -> in_list)
mb += "dummy%s".format(i) -> List("cat1", "hash1", 100, (System.currentTimeMillis()/1000).toInt, in_map)
}
mb.toMap
}
val json = Extraction.decompose(map)
val jsonStrOut = Printer.pretty(JsonAST.render(json))
val fileName = "foo.txt"
val fw = new FileWriter(fileName)
fw.write(jsonStrOut)
fw.close()
val jsonStrIn = scala.io.Source.fromFile(fileName).mkString
val obj = parse(jsonStrIn).asInstanceOf[JObject].values
println(obj("dummy3"))
虽然这是可行的,但我强烈建议利用case classes
和其他Scala结构来帮助进行序列化和反序列化。 Lift非常善于处理这些对象。除非有理由在大地图中包含所有内容,否则我认为从长远来看,你会为自己省去很多麻烦。