Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android版fastjson JSON.toJSONString 传入org.json.JSONObject后只打印{} #3944

Open
franticn opened this issue Nov 11, 2021 · 0 comments

Comments

@franticn
Copy link

org.json.JSONObject req = new org.json.JSONObject();
req.put("id", 1111);
req.put("name", "name11");
System.out.println(com.alibaba.fastjson.JSON.toJSONString(req)); // 输出"{}"

我看master上的SerializeConfig.java中会对org.json.JSONObject做兼容处理,

public class JSONObjectCodec implements ObjectSerializer {
public final static JSONObjectCodec instance = new JSONObjectCodec();
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features)
throws IOException {
SerializeWriter out = serializer.out;
MapSerializer mapSerializer = MapSerializer.instance;
try {
Field mapField = object.getClass().getDeclaredField("map");
if (Modifier.isPrivate(mapField.getModifiers())) {
mapField.setAccessible(true);
}
Object map = mapField.get(object);
mapSerializer.write(serializer, map, fieldName, fieldType, features);
} catch (Exception e) {
out.writeNull();
}
}
}

if (className.equals("org.json.JSONObject")) {
put(clazz, writer = JSONObjectCodec.instance);
return writer;
}

为什么 1.1.71-android 没有相关改动,希望此处更新能同步至Android分支上去https://github.com/alibaba/fastjson/blob/1.1.71.android/src/main/java/com/alibaba/fastjson/serializer/SerializeConfig.java

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant