diff --git a/README.md b/README.md index 2371036..2280e07 100644 --- a/README.md +++ b/README.md @@ -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' } ``` diff --git a/app/build.gradle b/app/build.gradle index 7b04c70..1066fdd 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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' } diff --git a/app/src/androidTest/assets/AbnormalJson.json b/app/src/androidTest/assets/AbnormalJson.json index 65f3995..661a56a 100644 --- a/app/src/androidTest/assets/AbnormalJson.json +++ b/app/src/androidTest/assets/AbnormalJson.json @@ -47,6 +47,7 @@ "d": "哈哈", "e": false }, + "map5" : 1.1, "stringTest1" : null, "stringTest2" : false, "stringTest3" : 123, diff --git a/app/src/androidTest/assets/NormalJson.json b/app/src/androidTest/assets/NormalJson.json index 3522054..c898ca0 100644 --- a/app/src/androidTest/assets/NormalJson.json +++ b/app/src/androidTest/assets/NormalJson.json @@ -42,6 +42,11 @@ }, "map3" : {}, "map4" : null, + "map5" : { + "attitude": 1710126441000, + "type": 1, + "money": 1.2345 + }, "stringTest1" : null, "stringTest2" : "", "stringTest3" : "字符串" diff --git a/app/src/androidTest/java/com/hjq/gson/factory/test/JsonBean.java b/app/src/androidTest/java/com/hjq/gson/factory/test/JsonBean.java index f387259..8c7a35d 100644 --- a/app/src/androidTest/java/com/hjq/gson/factory/test/JsonBean.java +++ b/app/src/androidTest/java/com/hjq/gson/factory/test/JsonBean.java @@ -65,6 +65,7 @@ public final class JsonBean { private Map map2; private Map map3; private Map map4; + private Map map5; private String stringTest1; private String stringTest2; diff --git a/library/build.gradle b/library/build.gradle index 512649f..88cf9a6 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -6,8 +6,8 @@ android { defaultConfig { minSdkVersion 12 - versionCode 950 - versionName "9.5" + versionCode 960 + versionName "9.6" } // 使用 JDK 1.8 diff --git a/library/src/main/java/com/hjq/gson/factory/GsonFactory.java b/library/src/main/java/com/hjq/gson/factory/GsonFactory.java index b48ed4f..c2680d6 100644 --- a/library/src/main/java/com/hjq/gson/factory/GsonFactory.java +++ b/library/src/main/java/com/hjq/gson/factory/GsonFactory.java @@ -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; @@ -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; @@ -44,6 +46,8 @@ public final class GsonFactory { private static final List REFLECTION_ACCESS_FILTERS = new ArrayList<>(); + private static ToNumberStrategy sObjectToNumberStrategy = new AutoToNumberStrategy(); + private static ParseExceptionCallback sParseExceptionCallback; private static volatile Gson sGson; @@ -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())) diff --git a/library/src/main/java/com/hjq/gson/factory/element/CollectionTypeAdapterFactory.java b/library/src/main/java/com/hjq/gson/factory/element/CollectionTypeAdapterFactory.java index 2ec46b4..e7e585a 100644 --- a/library/src/main/java/com/hjq/gson/factory/element/CollectionTypeAdapterFactory.java +++ b/library/src/main/java/com/hjq/gson/factory/element/CollectionTypeAdapterFactory.java @@ -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; diff --git a/library/src/main/java/com/hjq/gson/factory/element/ReflectiveTypeAdapter.java b/library/src/main/java/com/hjq/gson/factory/element/ReflectiveTypeAdapter.java index 6210ab9..87c65d0 100644 --- a/library/src/main/java/com/hjq/gson/factory/element/ReflectiveTypeAdapter.java +++ b/library/src/main/java/com/hjq/gson/factory/element/ReflectiveTypeAdapter.java @@ -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; diff --git a/library/src/main/java/com/hjq/gson/factory/element/ReflectiveTypeAdapterFactory.java b/library/src/main/java/com/hjq/gson/factory/element/ReflectiveTypeAdapterFactory.java index 0ed04a1..cf7ecf7 100644 --- a/library/src/main/java/com/hjq/gson/factory/element/ReflectiveTypeAdapterFactory.java +++ b/library/src/main/java/com/hjq/gson/factory/element/ReflectiveTypeAdapterFactory.java @@ -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; diff --git a/library/src/main/java/com/hjq/gson/factory/other/AutoToNumberStrategy.java b/library/src/main/java/com/hjq/gson/factory/other/AutoToNumberStrategy.java new file mode 100644 index 0000000..bc7f01c --- /dev/null +++ b/library/src/main/java/com/hjq/gson/factory/other/AutoToNumberStrategy.java @@ -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(); + } + } +} \ No newline at end of file diff --git a/library/src/main/java/com/hjq/gson/factory/element/ReflectiveFieldBound.java b/library/src/main/java/com/hjq/gson/factory/other/ReflectiveFieldBound.java similarity index 97% rename from library/src/main/java/com/hjq/gson/factory/element/ReflectiveFieldBound.java rename to library/src/main/java/com/hjq/gson/factory/other/ReflectiveFieldBound.java index 9a50938..dc78ed9 100644 --- a/library/src/main/java/com/hjq/gson/factory/element/ReflectiveFieldBound.java +++ b/library/src/main/java/com/hjq/gson/factory/other/ReflectiveFieldBound.java @@ -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; /** diff --git a/library/src/main/java/com/hjq/gson/factory/element/ReflectiveTypeUtils.java b/library/src/main/java/com/hjq/gson/factory/other/ReflectiveTypeUtils.java similarity index 96% rename from library/src/main/java/com/hjq/gson/factory/element/ReflectiveTypeUtils.java rename to library/src/main/java/com/hjq/gson/factory/other/ReflectiveTypeUtils.java index e341dfb..848d8a8 100644 --- a/library/src/main/java/com/hjq/gson/factory/element/ReflectiveTypeUtils.java +++ b/library/src/main/java/com/hjq/gson/factory/other/ReflectiveTypeUtils.java @@ -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; @@ -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;