美文网首页
scala fastjson 报错 fastjson 巨大的坑

scala fastjson 报错 fastjson 巨大的坑

作者: 邵红晓 | 来源:发表于2019-08-06 12:37 被阅读0次

报错代码 scala 1

  val map = new util.HashMap[String,Int]()
  map.put("tom",23)
  map.put("rose",30)
  map.put("jary",35)
  val map2 = new util.HashMap[String,Int]()
  map2.put("tom",23)
  map2.put("rose",30)
  map2.put("jary",35)
  val list = new util.ArrayList[util.Map[String,Int]]()
  list.add(map)
  list.add(map2)
  //正常代码
  val jsonStr = JSON.toJSONString(list,SerializerFeature.DisableCircularReferenceDetect)
 //异常代码
  val jsonStr = JSON.toJSONString(list)

scala1 代码报错异常,编译不通过

Error:(23, 22) ambiguous reference to overloaded definition,
both method toJSONString in class JSON of type (x$1: Any, x$2: com.alibaba.fastjson.serializer.SerializerFeature*)String
and  method toJSONString in class JSON of type (x$1: Any)String
match argument types (java.util.ArrayList[java.util.Map[String,Int]])
  val jsonStr = JSON.toJSONString(list)

报错代码 scala 2

  val map = Map("tom"->23,"rose"->30,"jary"->35)
  val map2 = Map("tom"->23,"rose"->30,"jary"->35)
  val arr = new ArrayBuffer[Map[String,Int]]()
  arr.append(map)
  arr.append(map2)
  val jsonStr = JSON.toJSONString(arr,SerializerFeature.DisableCircularReferenceDetect)
  println(jsonStr)
结果:是不是很诡异
{"empty":false,"traversableAgain":true}

/**
  * scala自带json
  * 只能序列化map对象
  */
case class Demo(name:String,age:Int)
object FastJsonDemo extends App{
  val map = Map("tom"->23,"rose"->30,"jary"->35)
  val result=JSONObject.apply(map)
  println(result)
}
#输出结果: {"tom" : 23, "rose" : 30, "jary" : 35}

问题:

错误 java代码1 重复引用

        Map<String,Integer> map = new HashMap<>();
        map.put("tom",23);
        map.put("rose",30);
        map.put("jary",35);

        List<Map<String,Integer>> list = new ArrayList<>();
        list.add(map);
        list.add(map);
        String jsonStr1= JSON.toJSONString(list);
        String jsonStr2 = JSON.toJSONString(list, SerializerFeature.DisableCircularReferenceDetect);
        System.out.println(jsonStr1);
        System.out.println(jsonStr2);
结果:是不是很诡异"$ref":"$[0]"}
jsonStr1 = [{"tom":23,"rose":30,"jary":35},{"$ref":"$[0]"}]
jsonStr2 = [{"tom":23,"rose":30,"jary":35},{"tom":23,"rose":30,"jary":35}]

java代码编译不报错,但是打印内容不对{"ref":"[0]"}]

错误java代码2 循环引用

@Data
class  A{
    private B b;
}
@Data
class B{
private A a;
}

    A a = new A();
        B b = new B();
        a.setB(b);
        b.setA(a);
        Map<String, Object> dataMap = new HashedMap<>();
        dataMap.put("a", a);
        dataMap.put("b", b);
        System.out.println(JSON.toJSONString(dataMap));

fastjson 对象重复和循环引用问题

  • 介绍:
  • FastJson提供了SerializerFeature.DisableCircularReferenceDetect这个序列化选项,用来关闭引用检测。
  • 关闭引用检测后,重复引用对象时就不会被$ref代替,但是在循环引用时也会导致异常。

调用
JSON.toJSONString()
JSONObject().toString()
打印出的字符串回出现$符号,从而导致程序异常,这真是一个巨大的坑,卧槽

总结

使用scala开发过程建议直接使用fastjson对象,JSONObject,JSONArray,使用过程中不要调用toString方法,非得调用,加上SerializerFeature.DisableCircularReferenceDetect该代码
遇到JSONArray,还得遍历它,建议使用如下方式,不然就会有坑出现
val jsonArray = JSON.parseArray(data_list,classOf[JSONObject])

val jsonArray = JSON.parseArray(data_list,classOf[JSONObject])
            import collection.JavaConverters._
            jsonArray.asScala.foreach(json=>{
              val distinct_id = json.getString("name")
              val uid = if (distinct_id.length() < 12) {
              uid
            })

相关文章

网友评论

      本文标题:scala fastjson 报错 fastjson 巨大的坑

      本文链接:https://www.haomeiwen.com/subject/tnyidctx.html