在处理我们的项目时,我想知道一些事情.Google的GSON API是否使用您要反序列化的JSON中的构造函数?例如:
我有一个JSON字符串,我想转换为Employee对象.Employee对象有一个构造函数,它对参数应用一些检查(例如,它的ID> 0).我们使用下面的代码来反序列化JSON.但是在将JSON反序列化为Employee时,是否甚至调用了此构造函数?
链接到GSON:https://github.com/google/gson
编辑: 所以在尝试断点后,我发现构造函数没有被调用.有人知道一种方法来调用它吗?
/**
* The GSON class to help you create and de-serialize the JSON objects.
*/
Gson gson = new Gson();
/**
* Convert JSON to an object.
* @param json The JSON to convert.
* @param cls The class to convert to.
* @return The converted JSON to object.
*/
public Object jsonToObject(String json, Class<?> cls) {
return gson.fromJson(json, cls);
}
Run Code Online (Sandbox Code Playgroud)