Skip to content

Commit

Permalink
兼容 Gson 2.9.1 版本
Browse files Browse the repository at this point in the history
  • Loading branch information
getActivity committed Aug 29, 2022
1 parent 4244094 commit 940d884
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 17 deletions.
23 changes: 13 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ android {
dependencies {
// Gson 解析容错:https://github.com/getActivity/GsonFactory
implementation 'com.github.getActivity:GsonFactory:6.2'
implementation 'com.github.getActivity:GsonFactory:6.3'
// Json 解析框架:https://github.com/google/gson
implementation 'com.google.code.gson:gson:2.9.0'
implementation 'com.google.code.gson:gson:2.9.1'
}
```

Expand Down Expand Up @@ -73,6 +73,9 @@ GsonFactory.registerTypeAdapterFactory(TypeAdapterFactory factory);
// 注册构造函数创建器
GsonFactory.registerInstanceCreator(Type type, InstanceCreator<?> creator);

// 添加反射访问过滤器
GsonFactory.addReflectionAccessFilter(ReflectionAccessFilter filter);

// 设置 Json 解析容错监听
GsonFactory.setJsonCallback(new JsonCallback() {

Expand Down Expand Up @@ -115,13 +118,13 @@ GsonFactory.setJsonCallback(new JsonCallback() {

* **基本涵盖 99.99% 的开发场景**,可以运行 Demo 中的**单元测试**用例来查看效果:

| 数据类型 | 容错的范围 | 数据示例 |
| :----: | :------: | :-----: |
| bean | 集合、字符串、布尔值、数值 | `[]``""``false``0` |
| 集合 | bean、字符串、布尔值、数值 | `{}``""``false``0` |
| 字符串 | bean、集合、布尔值、数值 | `{}``[]``false``0` |
| 布尔值 | bean、集合、字符串、数值 | `{}``[]``""``0` |
| 数值 | bean、集合、字符串、布尔值 | `{}``[]``""``false` |
| 数据类型 | 容错的范围 | 数据示例 |
| :-----: | :--------------------: | :-----------------------: |
| bean | 集合、字符串、布尔值、数值 | `[]``""``false``0` |
| 集合 | bean、字符串、布尔值、数值 | `{}``""``false``0` |
| 字符串 | bean、集合、布尔值、数值 | `{}``[]``false``0` |
| 布尔值 | bean、集合、字符串、数值 | `{}``[]``""``0` |
| 数值 | bean、集合、字符串、布尔值 | `{}``[]``""``false` |

* 大家可能觉得 Gson 解析容错没什么,那是因为我们对 Gson 解析失败的场景没有了解过:

Expand All @@ -138,7 +141,7 @@ GsonFactory.setJsonCallback(new JsonCallback() {
* 如果客户端定义的是整数,但后台返回浮点数,框架就对数值进行`取整`并赋值给字段

* 如果客户端定义布尔值,但是后台返回整数,框架则将`非 0 的数值则赋值为 true,否则为 false`

#### 常见疑问解答

* Retrofit + RxJava 怎么替换?
Expand Down
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "com.hjq.gson.factory.demo"
minSdkVersion 14
targetSdkVersion 31
versionCode 62
versionName "6.2"
versionCode 63
versionName "6.3"
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
}

Expand Down Expand Up @@ -67,7 +67,7 @@ dependencies {

// Json 解析框架:https://github.com/google/gson
// noinspection GradleDependency
implementation 'com.google.code.gson:gson:2.9.0'
implementation 'com.google.code.gson:gson:2.9.1'

// Bugly 异常捕捉:https://bugly.qq.com/docs/user-guide/instruction-manual-android/?v=20190418140644
implementation 'com.tencent.bugly:crashreport:4.0.4'
Expand Down
6 changes: 3 additions & 3 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ android {

defaultConfig {
minSdkVersion 11
versionCode 62
versionName "6.2"
versionCode 63
versionName "6.3"
}

// 使用 JDK 1.8
Expand Down Expand Up @@ -34,7 +34,7 @@ afterEvaluate {
dependencies {
// Json 解析框架:https://github.com/google/gson
// noinspection GradleDependency
implementation 'com.google.code.gson:gson:2.9.0'
implementation 'com.google.code.gson:gson:2.9.1'
}

// 防止编码问题
Expand Down
15 changes: 14 additions & 1 deletion library/src/main/java/com/hjq/gson/factory/GsonFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.InstanceCreator;
import com.google.gson.ReflectionAccessFilter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.internal.ConstructorConstructor;
import com.google.gson.internal.Excluder;
Expand Down Expand Up @@ -43,6 +44,8 @@ public final class GsonFactory {

private static final List<TypeAdapterFactory> TYPE_ADAPTER_FACTORIES = new ArrayList<>();

private static final List<ReflectionAccessFilter> REFLECTION_ACCESS_FILTERS = new ArrayList<>();

private static JsonCallback sJsonCallback;

private static volatile Gson sGson;
Expand Down Expand Up @@ -96,12 +99,22 @@ public static void registerInstanceCreator(Type type, InstanceCreator<?> creator
INSTANCE_CREATORS.put(type, creator);
}

/**
* 添加反射访问过滤器,同等于 {@link GsonBuilder#addReflectionAccessFilter(ReflectionAccessFilter)}
*/
public void addReflectionAccessFilter(ReflectionAccessFilter filter) {
if (filter == null) {
return;
}
REFLECTION_ACCESS_FILTERS.add(0, filter);
}

/**
* 创建 Gson 构建对象
*/
public static GsonBuilder newGsonBuilder() {
GsonBuilder gsonBuilder = new GsonBuilder();
ConstructorConstructor constructor = new ConstructorConstructor(INSTANCE_CREATORS, true);
ConstructorConstructor constructor = new ConstructorConstructor(INSTANCE_CREATORS, true, REFLECTION_ACCESS_FILTERS);
gsonBuilder.registerTypeAdapterFactory(TypeAdapters.newFactory(String.class, new StringTypeAdapter()))
.registerTypeAdapterFactory(TypeAdapters.newFactory(boolean.class, Boolean.class, new BooleanTypeAdapter()))
.registerTypeAdapterFactory(TypeAdapters.newFactory(int.class, Integer.class, new IntegerTypeAdapter()))
Expand Down

0 comments on commit 940d884

Please sign in to comment.