0

Given this code example:

import com.mongodb.util.JSON
import com.mongodb.casbah.Imports._
val json = """{"date" : { "$date" : 1327064009959 }}"""
val doc = JSON.parse(json)

I get this error: java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.String

What can I do to get this parsed correctly in Scala with Casbah?

Piotr Zolnierek
  • 1,030
  • 1
  • 10
  • 19

2 Answers2

0

There is a solution, which I don't like too much, thou:

import com.mongodb.util.JSON
import com.mongodb.casbah.Imports._
import scala.util.matching.Regex

val json = """{"date" : { "$date" : 1327064009959 }}"""
val doc = JSON.parse(json)
var regex = new Regex("""\{ "\$date" : (\d+) \}""", "date")
val fixed = regex replaceAllIn (json, m => "\"" + (new DateTime(m.group("date").toLong)) + "\"" )
val doc = JSON.parse(fixed).asInstanceOf[DBObject]
Piotr Zolnierek
  • 1,030
  • 1
  • 10
  • 19
-1

Check this typo, this is the valid JSON you should pass

var json = '
    {
        "date": {
            "$date": 1327064009959
        }
    }';
om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
Sam Arul Raj T
  • 1,752
  • 17
  • 23
  • Are you sure you are not messing up javascript with scala? Cause I sure it wont even compile in scala, cause ticks are used by characters, not strings: `val ok = 'x'` and this will throw an error: `val notOk = 'notOk'`. Hence your solution is meaningless. – om-nom-nom Mar 18 '12 at 11:30