{"id":15056581,"url":"https://github.com/lianjiatech/gson-plugin","last_synced_at":"2025-07-16T05:09:27.615Z","repository":{"id":104313308,"uuid":"157722879","full_name":"LianjiaTech/gson-plugin","owner":"LianjiaTech","description":"辅助 Gson 库的 gradle 插件，防止 Json 数据解析类型异常。","archived":false,"fork":false,"pushed_at":"2019-05-13T05:06:57.000Z","size":152,"stargazers_count":151,"open_issues_count":2,"forks_count":14,"subscribers_count":17,"default_branch":"master","last_synced_at":"2025-04-10T04:40:21.263Z","etag":null,"topics":["android","android-library","gradle","gradle-plugin","groovy","gson"],"latest_commit_sha":null,"homepage":"","language":"Groovy","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/LianjiaTech.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-11-15T14:18:29.000Z","updated_at":"2023-12-25T10:41:58.000Z","dependencies_parsed_at":"2023-05-03T15:33:28.901Z","dependency_job_id":null,"html_url":"https://github.com/LianjiaTech/gson-plugin","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/LianjiaTech/gson-plugin","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LianjiaTech%2Fgson-plugin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LianjiaTech%2Fgson-plugin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LianjiaTech%2Fgson-plugin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LianjiaTech%2Fgson-plugin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LianjiaTech","download_url":"https://codeload.github.com/LianjiaTech/gson-plugin/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LianjiaTech%2Fgson-plugin/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265483615,"owners_count":23774240,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","android-library","gradle","gradle-plugin","groovy","gson"],"created_at":"2024-09-24T21:53:39.457Z","updated_at":"2025-07-16T05:09:27.592Z","avatar_url":"https://github.com/LianjiaTech.png","language":"Groovy","funding_links":[],"categories":[],"sub_categories":[],"readme":"# gson-plugin\n强化Android-Json解析的插件，解决Android-Json解析数据类型转换异常，不影响对Gson库的使用\n\n# 诞生背景\nAndroid主要开发语言是Java，属于强数据类型语言，不少公司后台开发采用的是PHP，属于弱数据类型的语言。  \n客户端与服务器在进行数据传输的过程中，常常因为某个字段数据类型不一致，导致客户端gson解析失败，从而导致整个页面的数据均无法展示。\n\n# 功能描述\n1.当某个字段解析失败的时候，跳过该字段继续解析其它字段，保证其它正常数据可以展示出来。  \n2.当某个字段解析失败的时候，通过观察者模式，将异常抛出，开发者在收到异常后可以进行相应的处理（如将异常日志上传到服务器，然后推动服务端RD解决）。  \n3.不影响对Gson库的使用。\n\n# 接入方法\n1.工程根目录加入repositories\n```\nbuildscript {\n    repositories {\n        maven { url 'https://jitpack.io' }\n    }\n}\nallprojects {\n    repositories {\n        maven { url 'https://jitpack.io' }\n    }\n}\n```\n2.工程根目录build.gradle加入ClassPath  \n```\ndependencies {\n classpath 'com.github.LianjiaTech:gson-plugin:2.1.0'\n}\n```\n3.工程app目录build.gradle加入依赖  \n```\napply plugin: 'com.ke.gson.plugin'\n```\n4.可选调用（监听异常json字段，建议收到后上报给服务器）\n```\nReaderTools.setListener(new ReaderTools.JsonSyntaxErrorListener() {\n  @Override\n public void onJsonSyntaxError(String exception, String invokeStack) {\n    //upload error info to server\n Log.e(\"test\", \"json syntax exception: \" + exception);\n Log.e(\"test\", \"json syntax invokeStack: \" + invokeStack);\n }\n});\n```\n5.添加混淆keep\n```\n-keep class com.google.gson.** { *; }\n-keep class com.ke.gson.** { *; }\n```\n# 注意事项\n1.如果也apply了其它plugin插件，请把 apply plugin: 'com.ke.gson.plugin' 加入到其它apply之前  \n原因：gson_plugin只会处理file.name包含gson的jar包，有的插件会将jar文件进行merge，统一输出为一个jar包，导致gson_plugin匹配不到，从而不会对该文件进行处理。  \n\n2.如果引用了SNAPSHOT版本，请不要使用本地缓存  \n工程根目录的build.gradle与app目录的build.gradle都得加入  \n```\nconfigurations.all {\n    resolutionStrategy.cacheChangingModulesFor 0, 'seconds'\n}\n```  \n\n3.编译失败怎么办  \ntaskkill /im java.exe /f  然后clean，重新build  \n\n# 性能对比  \n对如下数据进行2000次循环解析：\n```\npublic class TestBean {\n public String name;\n public int age;\n public String sex;\n public boolean is_success;\n public String[] array;\n public List\u003cString\u003e list;\n public Map\u003cString, String\u003e map;\n public TestBean bean;\n}\n```\n使用原生gson结果：  \n第1次：1374ms，第2次：1430ms，第3次：1429ms，平均：1411ms  \n\n使用gson-plugin结果：  \n第1次：1503ms，第2次：1381ms，第3次：1418ms，平均：1434ms  \n\n结论：  \ngson-plugin比原生gson解析，效率略低（多执行了几行判断逻辑代码），但可忽略不计\n\n# 原理说明\n侵入编译流程，在编译过程中，修改gson库的字节码，修改gson解析相关的方法\n\n# 支持gson库版本\n支持gson库所有版本\n\n# 特殊说明  \n2.1.0之前的版本，对Float、Double、Map数据类型的支持不全面，建议使用2.1.0及以上的版本\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flianjiatech%2Fgson-plugin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flianjiatech%2Fgson-plugin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flianjiatech%2Fgson-plugin/lists"}