fastjson 和reaml最新版4.1.0使用序列化时出现stack overflow exception

q9rjltbz  于 2021-11-27  发布在  Java
关注(0)|答案(14)|浏览(221)
  • No description provided.*
92vpleto

92vpleto1#

堆栈信息是怎样的?

btqmn9zl

btqmn9zl2#

java.lang.StackOverflowError: stack size 8MB
at java.lang.reflect.Method.invoke(Native Method)
at com.alibaba.fastjson.util.FieldInfo.get(FieldInfo.java:351)
at com.alibaba.fastjson.serializer.FieldSerializer.getPropertyValue(FieldSerializer.java:92)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:352)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
E/AndroidRuntime: at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
E/AndroidRuntime: at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
E/AndroidRuntime: at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:611)
at com.alibaba.fastjson.serializer.FieldSerializer.writeValue(FieldSerializer.java:146)

vulvrdjw

vulvrdjw4#

是不是get方法里有new对象哈

yquaqz18

yquaqz185#

4.1.0的RealmObject里面多了这两个方法,3.7.2没有,所以4.1.0版本序列化的时候会把realm对象序列化出来
/**

  • Returns {@link Realm} instance where this {@link RealmObject} belongs.

  • Youmust notcall {@link Realm#close()} against returned instance.

  • @return {@link Realm} instance where this object belongs to or {@code null} if this object is unmanaged.

  • @throws IllegalStateException if this object is an instance of {@link DynamicRealmObject}

  • or this object was already deleted or the corresponding {@link Realm} was already closed.
    */
    public Realm getRealm() {
    return getRealm(this);
    }

/**
 * returns {@link Realm} instance where the {@code model} belongs.
 * <p>
 * You <b>must not</b> call {@link Realm#close()} against returned instance.
 *
 * @param model an {@link RealmModel} instance other than {@link DynamicRealmObject}.
 * @return {@link Realm} instance where the {@code model} belongs or {@code null} if the {@code model} is unmanaged.
 * @throws IllegalArgumentException if the {@code model} is {@code null}.
 * @throws IllegalStateException if the {@code model}  is an instance of {@link DynamicRealmObject}
 * or this object was already deleted or the corresponding {@link Realm} was already closed.
 */
public static Realm getRealm(RealmModel model) {
    if (model == null) {
        throw new IllegalArgumentException(MSG_NULL_OBJECT);
    }
    if (model instanceof DynamicRealmObject) {
        throw new IllegalStateException(MSG_DYNAMIC_OBJECT);
    }
    if (!(model instanceof RealmObjectProxy)) {
        return null;
    }
    final BaseRealm realm = ((RealmObjectProxy) model).realmGet$proxyState().getRealm$realm();
    realm.checkIfValid();
    if (!RealmObject.isValid(model)) {
        throw new IllegalStateException(MSG_DELETED_OBJECT);
    }

    return (Realm) realm;
}
7dl7o3gd

7dl7o3gd6#

我也发现同样的报错,之前用着正常,添加了一个style字段,然后migration后,就报这个问题了。

有想找其他库替代的想法了

bqujaahr

bqujaahr7#

你看看是不是你有什么get方法被死循环调用了

c8ib6hqw

c8ib6hqw8#

@destinyd@LambdaV5
检查一下是不是get方法中有new 对象?

ijnw1ujt

ijnw1ujt9#

public Realm getRealm() {
return getRealm(this);
}
这个是因为里面的getrealm引用了this,所以又会序列化这个对象,然后就死循环了

vs3odd8k

vs3odd8k10#

@wenshao
get里面没有new对象

我现在处理的办法,是中间再建立一个非继承RealmObject的对象,然后手动转移数据,然后toJSONString 才没有问题

r6hnlfcb

r6hnlfcb11#

@wenshao@LambdaV5@destinyd
唉,fastjson 与 realm 的兼容 真是个大坑,
尝试了各种方法,目前已解决,尚未发现新引入问题

/**实体类转json

* @param obj
* @return
* /

	public static String toJSONString(final Object obj) {
		if (obj instanceof String) {
			return (String) obj;
		}
		try {
			PropertyFilter filter = new PropertyFilter() {
				@Override
				public boolean apply(Object object, String name, Object value) {
					if (object instanceof Realm
							|| object instanceof OsSharedRealm
							|| object instanceof RealmSchema
							|| object instanceof RealmMigration
							|| object instanceof RealmNotifier
							) {
						return false;
					}

					if (name.equalsIgnoreCase("loaded")
							|| name.equalsIgnoreCase("valid")
							|| name.equalsIgnoreCase("managed")
							){
						return false;
					}
					return true;
				}
			};

			return com.alibaba.fastjson.JSON.toJSONString(obj, filter);
		} catch (Exception e) {
			Log.e(TAG, "toJSONString  catch \n" + e.getMessage());
		}

		return null;
	}

考虑把代码推到

自动化接口与文档 ORM 库 APIJSON
Android 快速开发框架 ZBLibrary

里面的 fastjson 封装工具类 JSON.java,
但又不想多引入 Realm 这个库,
毕竟有很多用户不用它。。。

lnxxn5zx

lnxxn5zx12#

打算给 fastjson 发 pull request,把 测试用例 和 解决方案 的相关代码推上去

bnl4lu3b

bnl4lu3b13#

private class NamePropertyFilter implements PropertyPreFilter, ValueFilter {

    @Override
    public boolean apply(JSONSerializer serializer, Object object, String name) {
        return !TextUtils.equals("realm", name);
    }

    @Override
    public Object process(Object object, String name, Object value) {
        return value == null ? "" : value;
    }
}

序列化的时候,传这个对象进去也可以

a9wyjsp7

a9wyjsp714#

@wenshao
已发 Pull Request,加了测试用例和解决代码
#2369

相关问题