Skip to content

Commit

Permalink
优化在不指定解析类型的情况下 Gson 默认将数值用 double 类型接收的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
getActivity committed Mar 20, 2024
1 parent 53071ce commit 415e33f
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ android {
dependencies {
// Gson 解析容错:https://github.com/getActivity/GsonFactory
implementation 'com.github.getActivity:GsonFactory:9.5'
implementation 'com.github.getActivity:GsonFactory:9.6'
// Json 解析框架:https://github.com/google/gson
implementation 'com.google.code.gson:gson:2.10.1'
// Kotlin 反射库:用于反射 Kotlin data class 类对象
// Kotlin 反射库:用于反射 Kotlin data class 类对象,1.5.10 请修改成当前项目 Kotlin 的版本号
implementation 'org.jetbrains.kotlin:kotlin-reflect:1.5.10'
}
```
Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
applicationId "com.hjq.gson.factory.demo"
minSdkVersion 26
targetSdkVersion 31
versionCode 950
versionName "9.5"
versionCode 960
versionName "9.6"
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
}

Expand Down
1 change: 1 addition & 0 deletions app/src/androidTest/assets/AbnormalJson.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"d": "哈哈",
"e": false
},
"map5" : 1.1,
"stringTest1" : null,
"stringTest2" : false,
"stringTest3" : 123,
Expand Down
5 changes: 5 additions & 0 deletions app/src/androidTest/assets/NormalJson.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@
},
"map3" : {},
"map4" : null,
"map5" : {
"attitude": 1710126441000,
"type": 1,
"money": 1.2345
},
"stringTest1" : null,
"stringTest2" : "",
"stringTest3" : "字符串"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public final class JsonBean {
private Map<String, Integer> map2;
private Map<String, Integer> map3;
private Map<String, Integer> map4;
private Map<String, Object> map5;

private String stringTest1;
private String stringTest2;
Expand Down
4 changes: 2 additions & 2 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ android {

defaultConfig {
minSdkVersion 12
versionCode 950
versionName "9.5"
versionCode 960
versionName "9.6"
}

// 使用 JDK 1.8
Expand Down
16 changes: 15 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 @@ -5,6 +5,7 @@
import com.google.gson.GsonBuilder;
import com.google.gson.InstanceCreator;
import com.google.gson.ReflectionAccessFilter;
import com.google.gson.ToNumberStrategy;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.internal.Excluder;
import com.google.gson.internal.bind.TypeAdapters;
Expand All @@ -21,6 +22,7 @@
import com.hjq.gson.factory.element.CollectionTypeAdapterFactory;
import com.hjq.gson.factory.element.MapTypeAdapterFactory;
import com.hjq.gson.factory.element.ReflectiveTypeAdapterFactory;
import com.hjq.gson.factory.other.AutoToNumberStrategy;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.ArrayList;
Expand All @@ -44,6 +46,8 @@ public final class GsonFactory {

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

private static ToNumberStrategy sObjectToNumberStrategy = new AutoToNumberStrategy();

private static ParseExceptionCallback sParseExceptionCallback;

private static volatile Gson sGson;
Expand Down Expand Up @@ -106,19 +110,29 @@ public static void registerInstanceCreator(Type type, InstanceCreator<?> creator
/**
* 添加反射访问过滤器,同等于 {@link GsonBuilder#addReflectionAccessFilter(ReflectionAccessFilter)}
*/
public void addReflectionAccessFilter(ReflectionAccessFilter filter) {
public static void addReflectionAccessFilter(ReflectionAccessFilter filter) {
if (filter == null) {
return;
}
REFLECTION_ACCESS_FILTERS.add(0, filter);
}

/**
* 设置自动转换数值类型的策略
*/
public static void setObjectToNumberStrategy(ToNumberStrategy objectToNumberStrategy) {
GsonFactory.sObjectToNumberStrategy = objectToNumberStrategy;
}

/**
* 创建 Gson 构建对象
*/
public static GsonBuilder newGsonBuilder() {
GsonBuilder gsonBuilder = new GsonBuilder();
MainConstructor mainConstructor = new MainConstructor(INSTANCE_CREATORS, true, REFLECTION_ACCESS_FILTERS);
if (sObjectToNumberStrategy != null) {
gsonBuilder.setObjectToNumberStrategy(sObjectToNumberStrategy);
}
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.google.gson.internal.ObjectConstructor;
import com.google.gson.reflect.TypeToken;
import com.hjq.gson.factory.constructor.MainConstructor;
import com.hjq.gson.factory.other.ReflectiveTypeUtils;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.Type;
import java.util.Collection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.google.gson.stream.JsonWriter;
import com.hjq.gson.factory.GsonFactory;
import com.hjq.gson.factory.ParseExceptionCallback;
import com.hjq.gson.factory.other.ReflectiveFieldBound;
import java.io.IOException;
import java.util.Map;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import com.google.gson.internal.Excluder;
import com.google.gson.reflect.TypeToken;
import com.hjq.gson.factory.constructor.MainConstructor;
import com.hjq.gson.factory.other.ReflectiveFieldBound;
import com.hjq.gson.factory.other.ReflectiveTypeUtils;
import java.lang.reflect.Field;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.Type;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.hjq.gson.factory.other;

import com.google.gson.ToNumberStrategy;
import com.google.gson.stream.JsonReader;
import java.io.IOException;
import java.math.BigDecimal;

/**
* author : Android 轮子哥
* github : https://github.com/getActivity/GsonFactory
* time : 2024/03/11
* desc : 自动转换数值类型的策略
*/
public class AutoToNumberStrategy implements ToNumberStrategy {

@Override
public Number readNumber(JsonReader in) throws IOException {
// Github issue 地址:https://github.com/getActivity/GsonFactory/issues/40
String numberString = in.nextString();
BigDecimal bigDecimal = new BigDecimal(numberString);
// 判断这个数值是浮点数还是整数
if (bigDecimal.scale() > 0) {
// 如果是浮点数,则用 double 类型装载
return bigDecimal.doubleValue();
}

// 如果是整数,则用 int 类型或者 long 类型装载
if (bigDecimal.compareTo(BigDecimal.valueOf(Integer.MAX_VALUE)) > 0) {
return bigDecimal.longValue();
} else {
return bigDecimal.intValue();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.hjq.gson.factory.element;
package com.hjq.gson.factory.other;

import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;

import java.io.IOException;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.hjq.gson.factory.element;
package com.hjq.gson.factory.other;

import com.google.gson.FieldNamingStrategy;
import com.google.gson.Gson;
Expand All @@ -10,6 +10,10 @@
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import com.hjq.gson.factory.constructor.MainConstructor;
import com.hjq.gson.factory.element.CollectionTypeAdapter;
import com.hjq.gson.factory.element.MapTypeAdapter;
import com.hjq.gson.factory.element.ReflectiveTypeAdapter;
import com.hjq.gson.factory.element.TypeAdapterRuntimeTypeWrapper;
import java.io.IOException;
import java.lang.reflect.Field;
import java.math.BigDecimal;
Expand Down

0 comments on commit 415e33f

Please sign in to comment.