alibaba / fastjson

FASTJSON 2.0.x has been released, faster and more secure, recommend you upgrade.
https://github.com/alibaba/fastjson2/wiki/fastjson_1_upgrade_cn
Apache License 2.0
25.69k stars 6.51k forks source link

序列化scala的immutable.Map异常 #2161

Open onelogistics opened 5 years ago

onelogistics commented 5 years ago

使用fastjson序列化map,代码如下

val map=Map("test1" -> "aa")
val result=com.alibaba.fastjson.JSON.toJSONString(map,Array.empty[SerializerFeature]:_*)
println(result)

输出结果为: {"empty":false,"traversableAgain":true}

onelogistics commented 5 years ago

使用scala.util.parsing.json可以得到预期输出

 val map=Map("test1" -> "aa")
  val result=scala.util.parsing.json.JSONObject.apply(map)
println(result)

输出结果为: {"test1" : "aa"} 想请教下fastjson序列化异常的原因

huangyunbo commented 5 years ago

@Robert114 I have the same problem. How can I solve it?

onelogistics commented 5 years ago

i didn't solve it. fastjson is designed for Java, maybe it has some special usage on scala

plokhotnyuk commented 5 years ago

@junjun-yang @huangyunbo you can use jsoniter-scala for that, having the same or better efficiency in parsing and serialization but with support of Scala case classes and collections, until Scala support will land for fastjson.

onelogistics commented 5 years ago

okay,thank you for your.recommendation

echohlne commented 4 years ago

异常的原因如下: 假设val map = Map("key" -> 1) map的className为: scala.collection.immutable.Map$Map1 scala在获取当前对象匹配的ObjectSerializer时, 会根据当前类的className跟已知对象类型(比如Integer, Map, Array等) 比较, 判断传入的对象类型是否跟已知类型相同, 或者是否是对应的子类等。 Scala中的Map类型在匹配完所有已知类型后都没有发现对应的ObjectSerializer类, 最终使用了默认的JavaBeanSerializer(com\alibaba\fastjson\serializer\SerializeConfig.class$getObjectWriter), 导致错误

yangyongyongyong commented 2 years ago

没有解决办法吗. 就用fastjson的话