-
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
优化在不指定解析类型的情况下 Gson 默认将数值用 double 类型接收的问题
- Loading branch information
1 parent
53071ce
commit 415e33f
Showing
13 changed files
with
72 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
library/src/main/java/com/hjq/gson/factory/other/AutoToNumberStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
3 changes: 1 addition & 2 deletions
3
...factory/element/ReflectiveFieldBound.java → ...n/factory/other/ReflectiveFieldBound.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters