Skip to content

Commit 8418068

Browse files
committed
科学计数法优化
1 parent 184e673 commit 8418068

2 files changed

Lines changed: 8 additions & 2 deletions

File tree

Sources/SmartCodable/Core/JSONValue/JSONValue.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ extension JSONValue {
127127
extension NSNumber {
128128
static func fromJSONNumber(_ string: String) -> NSNumber? {
129129
let decIndex = string.firstIndex(of: ".")
130-
let expIndex = string.firstIndex(of: "e")
130+
// JSON 规范允许大写 E 作为科学计数法标记,这里需要一并识别
131+
let expIndex = string.firstIndex(where: { $0 == "e" || $0 == "E" })
131132
let isInteger = decIndex == nil && expIndex == nil
132133
let isNegative = string.utf8[string.utf8.startIndex] == UInt8(ascii: "-")
133134
let digitCount = string[string.startIndex..<(expIndex ?? string.endIndex)].count

Sources/SmartCodable/Core/PropertyWrapper/SmartAny/SmartAnyImpl.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,10 +260,15 @@ extension JSONDecoderImpl {
260260
}
261261
case .number(let number):
262262
if number.contains(".") { // 浮点数
263-
if number.contains("e") { // 检查字符串中是否包含字符 e,这表示数字可能以科学计数法表示
263+
// JSON 规范(RFC 8259)允许 e / E 两种写法,这里需要大小写都兼容
264+
if number.contains("e") || number.contains("E") { // 科学计数法
264265
if let temp = container.decodeIfPresent(Decimal.self) as? NSNumber {
265266
return .number(temp)
266267
}
268+
// Decimal 无法覆盖次正规数(如 ±E-324)等极小值,回退到 Double 兜底
269+
if let temp = container.decodeIfPresent(Double.self) as? NSNumber {
270+
return .number(temp)
271+
}
267272
} else {
268273
if let temp = container.decodeIfPresent(Double.self) as? NSNumber {
269274
return .number(temp)

0 commit comments

Comments
 (0)