I have searched a lot but not found any answer to my question. Here's what I'm trying to achieve: I can serialize datastructures into a JSON (of my own implementation)
def serializeJSON(data: Any): JSON = {
data match {
// should deal with most regular collections
case t: mutable.Traversable[_] =>
val ar = new JSONArray()
t.foreach(x => ar.eltAdd(serializeJSON(x)))
ar
case t: immutable.Traversable[_] =>
val ar = new JSONArray()
t.foreach(x => ar.eltAdd(serializeJSON(x)))
ar
case p: Product2[_,_] => // especially for Map
val ob = new JSONObject()
ob.set(p._1.toString, serializeJSON(p._2))
ob
case a: Array[_] =>
val ar = new JSONArray()
a.foreach(x => ar.eltAdd(serializeJSON(x)))
ar
// leafs
case null => new JSONNull
case s: String => new JSONString(s)
case x if x.isInstanceOf[Int] | x.isInstanceOf[Long] | x.isInstanceOf[Double] |
x.isInstanceOf[Float] | x.isInstanceOf[Short] => new JSONNumber(x.toString)
case b: Boolean => new JSONBoolean(b)
}
Now I'm trying to deserialize data that were serialized this way. Of course I need to know the type I want to deserialize into, but let's assume that I have it. I need something like:
private def deserializeJSON[T: TypeTag](data: Json): T = ???
Deserializing leafs is pretty straightforward, I think I can manage arrays as well with this question. But I truly have no idea of how to deserialize a JSON into a Map[Int,List[String]]
for example. If someone could give me a hint I would really appreciate that!
Aucun commentaire:
Enregistrer un commentaire