Flexjson 中的 ConcurrentModificationException

ConcurrentModificationException in Flexjson

序列化对象时有时会出现此错误

flexjson.JSONException: Error trying to deepSerialize
        at flexjson.transformer.ObjectTransformer.transform(ObjectTransformer.java:61)
        at flexjson.transformer.TransformerWrapper.transform(TransformerWrapper.java:22)
        at flexjson.transformer.ObjectTransformer.transform(ObjectTransformer.java:49)
        at flexjson.transformer.TransformerWrapper.transform(TransformerWrapper.java:22)
        at flexjson.JSONContext.transform(JSONContext.java:73)
        at flexjson.JSONSerializer.serialize(JSONSerializer.java:377)
        at flexjson.JSONSerializer.deepSerialize(JSONSerializer.java:301

...
Caused by: java.util.ConcurrentModificationException
        at java.base/java.util.ArrayList$Itr.checkForComodification(ArrayList.java:1042)
        at java.base/java.util.ArrayList$Itr.next(ArrayList.java:996)
        at flexjson.JSONContext.matches(JSONContext.java:434)

有了这个String sessionJson = serializer.exclude("*.class").deepSerialize(response);

谁能告诉我 ConcurrentModificationException 是什么意思?

可能在两个线程之间共享 JSONSerializer 的同一个实例,并且与序列化一起,还有其他 calls/invocations。检查对 includes/excludes、use、transforms 等的调用,它们应该在第一个线程调用 serialize.

之前

其他选项是使用CopyOnWriteArrayList,例如:

serializer = new flexjson.JSONSerializer();
List pathExpressions = (List)BeanUtil.getDeclaredProperty(serializer, "pathExpressions");
pathExpressions = new CopyOnWriteArrayList(pathExpressions);
BeanUtil.setDeclaredProperty(serializer, "pathExpressions", pathExpressions);