Rong Han
Rong Han

Reputation: 53

json4s extract error in scala with map values

I use json4s native,with a json string like this

val myjson = """
{
    "normative":"C",
    "prefixType":{
        "cod":["smallint", "int", "varchar(5)"],
        "des":["varchar", "string"],
        "fec":["timestamp"],
        "hms":["timestamp"],
        "tim":["timestamp"],
        "imp":["decimal","Float", "Double"]
        },
    "fixcolname":{
        "aud_usuario":"varchar(8)",
        "aud_fec":"timestamp",
        "aud_tim":"timestamp"
        },
    "symSep":"_", 
    "maxLength":26
}"""

And a case class

case class colVerify(prefixType: Map[String, Array[String]], fixcolname: Map[String, String], symSep: String, maxLength: Int)

and I want to extract it from the json String

val t = parse(myjson)
implicit val formats = DefaultFormats
val myvfy = t.extract[colVerify]

then got an error like this

Exception in thread "main" org.json4s.package$MappingException: Parsed JSON values do not match with class constructor
args=Map(des -> [Ljava.lang.String;@d7b1517, fec -> [Ljava.lang.String;@16c0663d, tim -> [Ljava.lang.String;@23223dd8, hms -> [Ljava.lang.String;@4ec6a292, imp -> [Ljava.lang.String;@1b40d5f0, cod -> [Ljava.lang.String;@ea4a92b),Map(aud_usuario -> varchar(8), aud_fec -> timestamp, aud_tim -> timestamp),_,26
arg types=scala.collection.immutable.HashMap$HashTrieMap,scala.collection.immutable.Map$Map3,java.lang.String,java.lang.Integer
constructor=public colVerify(scala.collection.mutable.Map,scala.collection.mutable.Map,java.lang.String,int)

Seems like it has problem with the type of Map, but how can I convert it implicitly?

Upvotes: 2

Views: 909

Answers (1)

Harald Gliebe
Harald Gliebe

Reputation: 7564

The problem is that the maps in your case class are mutable maps, is this intentional or did you accidently import collection.mutable.Map?

If you really want the mutable maps, you could implement a custom Serializer as described here: https://github.com/json4s/json4s#serializing-non-supported-types

My first idea to add another constructor with immutable maps in case class doesn't seem to work reliably.

Upvotes: 1

Related Questions