Joined June 2012
·
Be9359fff350f9fddef026a00640945b

Carlos Marin

Austin, TX
·
·

Had to simplify, but got it to work this way:

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import scala.reflect.ClassTag
import scala.reflect._

object JsonUtil {
    val jacksonMapper = new ObjectMapper()
    jacksonMapper.registerModule(DefaultScalaModule)
    //mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)

    def toJson(value: Map[Symbol, Any]): String = {
        toJson(value map { case (k,v) => k.name -> v})
    }

    def toJson(value: Any): String = {
        jacksonMapper.writeValueAsString(value)
    }

    def fromJson[T: ClassTag](json: String): T = {
        jacksonMapper.readValue[T](json, classTag[T].runtimeClass.asInstanceOf[Class[T]])
    }
}

And then can parse to a map using:

JsonUtil.fromJson[Map[String, List[Map[String, String]]]](jsonStr1)
for: //{"key": "value"}

JsonUtil.fromJson[Map[String, List[Map[String, String]]]](jsonStr2)
for:
//{"key": [ "subKey": "val" ... ]}

Is there a way to do this without the: ... .experimental.ScalaObjectMapper ?

Tried using ObjectMapper, based on this post, but ran into complications with: mapper.readValue[T](json) :(

Achievements
40 Karma
0 Total ProTip Views