Skip to content

Commit 2c5046a

Browse files
committed
refactor unkeyeddecodingcontainer
1 parent f241404 commit 2c5046a

File tree

1 file changed

+38
-203
lines changed

1 file changed

+38
-203
lines changed

Sources/ReerJSON/JSONDecoderImpl.swift

Lines changed: 38 additions & 203 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,12 +1236,11 @@ private final class PreTransformKeyedDecodingContainer<K: CodingKey>: KeyedDecod
12361236
private struct JSONUnkeyedDecodingContainer: UnkeyedDecodingContainer {
12371237
let impl: JSONDecoderImpl
12381238
var arrayPointer: UnsafeMutablePointer<yyjson_val>?
1239-
var peekedValue: JSON?
12401239
var count: Int?
12411240

12421241
private var arrayIterator: yyjson_arr_iter
12431242

1244-
var isAtEnd: Bool { arrayIterator.idx >= arrayIterator.max }
1243+
var isAtEnd: Bool { currentIndex >= (count ?? 0) }
12451244
var currentIndex = 0
12461245

12471246
init(impl: JSONDecoderImpl, codingPathNode: CodingPathNode) throws {
@@ -1275,52 +1274,32 @@ private struct JSONUnkeyedDecodingContainer: UnkeyedDecodingContainer {
12751274
var currentCodingPath: [CodingKey] {
12761275
codingPathNode.path(byAppendingIndex: currentIndex)
12771276
}
1278-
1279-
private mutating func advanceToNextValue() {
1280-
currentIndex += 1
1281-
peekedValue = nil
1282-
}
1283-
1277+
12841278
@inline(__always)
1285-
private mutating func peekNextValueIfPresent<T>(ofType type: T.Type) -> JSON? {
1286-
if let value = peekedValue {
1287-
return value
1288-
}
1289-
1290-
guard let elementPtr = yyjson_arr_iter_next(&arrayIterator) else {
1291-
return nil
1279+
private mutating func valueFromIterator<T>(ofType type: T.Type) throws -> JSON {
1280+
if !isAtEnd {
1281+
return JSON(pointer: arrayIterator.cur)
12921282
}
12931283

1294-
let nextValue = JSON(pointer: elementPtr)
1295-
peekedValue = nextValue
1296-
return nextValue
1284+
var path = self.codingPath
1285+
path.append(_CodingKey(index: currentIndex))
1286+
throw DecodingError.valueNotFound(
1287+
type,
1288+
.init(
1289+
codingPath: path,
1290+
debugDescription: "Cannot get next value -- unkeyed container is at end.",
1291+
underlyingError: nil
1292+
)
1293+
)
12971294
}
12981295

1299-
@inline(__always)
1300-
private mutating func peekNextValue<T>(ofType type: T.Type) throws -> JSON {
1301-
guard let nextValue = peekNextValueIfPresent(ofType: type) else {
1302-
var message = "Unkeyed container is at end."
1303-
if T.self == JSONUnkeyedDecodingContainer.self {
1304-
message = "Cannot get nested unkeyed container -- unkeyed container is at end."
1305-
}
1306-
if T.self == Decoder.self {
1307-
message = "Cannot get superDecoder() -- unkeyed container is at end."
1308-
}
1309-
1310-
var path = self.codingPath
1311-
path.append(_CodingKey(index: self.currentIndex))
1312-
1313-
throw DecodingError.valueNotFound(
1314-
type,
1315-
.init(codingPath: path,
1316-
debugDescription: message,
1317-
underlyingError: nil))
1318-
}
1319-
return nextValue
1296+
private mutating func advanceToNextValue() {
1297+
currentIndex += 1
1298+
yyjson_arr_iter_next(&arrayIterator)
13201299
}
13211300

13221301
mutating func decodeNil() throws -> Bool {
1323-
let value = try self.peekNextValue(ofType: Never.self)
1302+
let value = try valueFromIterator(ofType: Never.self)
13241303
if value.isNull {
13251304
advanceToNextValue()
13261305
return true
@@ -1332,249 +1311,105 @@ private struct JSONUnkeyedDecodingContainer: UnkeyedDecodingContainer {
13321311
}
13331312

13341313
mutating func decode(_ type: Bool.Type) throws -> Bool {
1335-
let value = try self.peekNextValue(ofType: Bool.self)
1314+
let value = try valueFromIterator(ofType: Bool.self)
13361315
guard let bool = value.bool else {
13371316
throw impl.createTypeMismatchError(type: type, for: codingPath, value: value)
13381317
}
13391318
advanceToNextValue()
13401319
return bool
13411320
}
13421321

1343-
mutating func decodeIfPresent(_ type: Bool.Type) throws -> Bool? {
1344-
guard let value = peekNextValueIfPresent(ofType: Bool.self), !value.isNull else {
1345-
advanceToNextValue()
1346-
return nil
1347-
}
1348-
guard let bool = value.bool else {
1349-
throw impl.createTypeMismatchError(type: type, for: currentCodingPath, value: value)
1350-
}
1351-
1352-
advanceToNextValue()
1353-
return bool
1354-
}
1355-
13561322
mutating func decode(_ type: String.Type) throws -> String {
1357-
let value = try self.peekNextValue(ofType: String.self)
1358-
guard let string = value.string else {
1359-
throw impl.createTypeMismatchError(type: type, for: currentCodingPath, value: value)
1360-
}
1361-
advanceToNextValue()
1362-
return string
1363-
}
1364-
1365-
mutating func decodeIfPresent(_ type: String.Type) throws -> String? {
1366-
guard let value = peekNextValueIfPresent(ofType: String.self), !value.isNull else {
1367-
advanceToNextValue()
1368-
return nil
1369-
}
1323+
let value = try valueFromIterator(ofType: String.self)
13701324
guard let string = value.string else {
13711325
throw impl.createTypeMismatchError(type: type, for: currentCodingPath, value: value)
13721326
}
1373-
13741327
advanceToNextValue()
13751328
return string
13761329
}
13771330

13781331
mutating func decode(_: Double.Type) throws -> Double {
1379-
let value = try peekNextValue(ofType: Double.self)
1380-
let result = try impl.unboxFloatingPoint(from: value, as: Double.self, for: codingPathNode, _CodingKey(index: currentIndex))
1381-
advanceToNextValue()
1382-
return result
1383-
}
1384-
1385-
mutating func decodeIfPresent(_ type: Double.Type) throws -> Double? {
1386-
guard let value = peekNextValueIfPresent(ofType: Double.self), !value.isNull else {
1387-
advanceToNextValue()
1388-
return nil
1389-
}
1332+
let value = try valueFromIterator(ofType: Double.self)
13901333
let result = try impl.unboxFloatingPoint(from: value, as: Double.self, for: codingPathNode, _CodingKey(index: currentIndex))
13911334
advanceToNextValue()
13921335
return result
13931336
}
13941337

1395-
mutating func decode(_: Float.Type) throws -> Float {
1396-
let value = try peekNextValue(ofType: Float.self)
1397-
let result = try impl.unboxFloatingPoint(from: value, as: Float.self, for: codingPathNode, _CodingKey(index: currentIndex))
1398-
advanceToNextValue()
1399-
return result
1400-
}
1401-
1402-
mutating func decodeIfPresent(_ type: Float.Type) throws -> Float? {
1403-
guard let value = peekNextValueIfPresent(ofType: Float.self), !value.isNull else {
1404-
advanceToNextValue()
1405-
return nil
1406-
}
1407-
let result = try impl.unboxFloatingPoint(from: value, as: Float.self, for: codingPathNode, _CodingKey(index: currentIndex))
1408-
advanceToNextValue()
1409-
return result
1410-
}
1411-
1412-
14131338
mutating func decode(_: Int.Type) throws -> Int {
1414-
let value = try peekNextValue(ofType: Int.self)
1415-
return try decodeInteger(value)
1416-
}
1417-
1418-
mutating func decodeIfPresent(_: Int.Type) throws -> Int? {
1419-
guard let value = peekNextValueIfPresent(ofType: Int.self), !value.isNull else {
1420-
advanceToNextValue()
1421-
return nil
1422-
}
1339+
let value = try valueFromIterator(ofType: Int.self)
14231340
return try decodeInteger(value)
14241341
}
14251342

14261343
mutating func decode(_: Int8.Type) throws -> Int8 {
1427-
let value = try peekNextValue(ofType: Int8.self)
1428-
return try decodeInteger(value)
1429-
}
1430-
1431-
mutating func decodeIfPresent(_: Int8.Type) throws -> Int8? {
1432-
guard let value = peekNextValueIfPresent(ofType: Int8.self), !value.isNull else {
1433-
advanceToNextValue()
1434-
return nil
1435-
}
1344+
let value = try valueFromIterator(ofType: Int8.self)
14361345
return try decodeInteger(value)
14371346
}
14381347

14391348
mutating func decode(_: Int16.Type) throws -> Int16 {
1440-
let value = try peekNextValue(ofType: Int16.self)
1441-
return try decodeInteger(value)
1442-
}
1443-
1444-
mutating func decodeIfPresent(_: Int16.Type) throws -> Int16? {
1445-
guard let value = peekNextValueIfPresent(ofType: Int16.self), !value.isNull else {
1446-
advanceToNextValue()
1447-
return nil
1448-
}
1349+
let value = try valueFromIterator(ofType: Int16.self)
14491350
return try decodeInteger(value)
14501351
}
14511352

14521353
mutating func decode(_: Int32.Type) throws -> Int32 {
1453-
let value = try peekNextValue(ofType: Int32.self)
1454-
return try decodeInteger(value)
1455-
}
1456-
1457-
mutating func decodeIfPresent(_: Int32.Type) throws -> Int32? {
1458-
guard let value = peekNextValueIfPresent(ofType: Int32.self), !value.isNull else {
1459-
advanceToNextValue()
1460-
return nil
1461-
}
1354+
let value = try valueFromIterator(ofType: Int32.self)
14621355
return try decodeInteger(value)
14631356
}
14641357

14651358
mutating func decode(_: Int64.Type) throws -> Int64 {
1466-
let value = try peekNextValue(ofType: Int64.self)
1359+
let value = try valueFromIterator(ofType: Int64.self)
14671360
return try decodeInteger(value)
14681361
}
14691362

14701363
#if compiler(>=6.0)
14711364
@available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, visionOS 2.0, *)
14721365
mutating func decode(_: Int128.Type) throws -> Int128 {
1473-
let value = try peekNextValue(ofType: Int128.self)
1366+
let value = try valueFromIterator(ofType: Int128.self)
14741367
return try decodeInteger(value)
14751368
}
14761369
#endif
14771370

1478-
mutating func decodeIfPresent(_: Int64.Type) throws -> Int64? {
1479-
guard let value = peekNextValueIfPresent(ofType: Int64.self), !value.isNull else {
1480-
advanceToNextValue()
1481-
return nil
1482-
}
1483-
return try decodeInteger(value)
1484-
}
1485-
14861371
mutating func decode(_: UInt.Type) throws -> UInt {
1487-
let value = try peekNextValue(ofType: UInt.self)
1488-
return try decodeInteger(value)
1489-
}
1490-
1491-
mutating func decodeIfPresent(_: UInt.Type) throws -> UInt? {
1492-
guard let value = peekNextValueIfPresent(ofType: UInt.self), !value.isNull else {
1493-
advanceToNextValue()
1494-
return nil
1495-
}
1372+
let value = try valueFromIterator(ofType: UInt.self)
14961373
return try decodeInteger(value)
14971374
}
14981375

14991376
mutating func decode(_: UInt8.Type) throws -> UInt8 {
1500-
let value = try peekNextValue(ofType: UInt.self)
1501-
return try decodeInteger(value)
1502-
}
1503-
1504-
mutating func decodeIfPresent(_: UInt8.Type) throws -> UInt8? {
1505-
guard let value = peekNextValueIfPresent(ofType: UInt.self), !value.isNull else {
1506-
advanceToNextValue()
1507-
return nil
1508-
}
1377+
let value = try valueFromIterator(ofType: UInt.self)
15091378
return try decodeInteger(value)
15101379
}
15111380

15121381
mutating func decode(_: UInt16.Type) throws -> UInt16 {
1513-
let value = try peekNextValue(ofType: UInt.self)
1514-
return try decodeInteger(value)
1515-
}
1516-
1517-
mutating func decodeIfPresent(_: UInt16.Type) throws -> UInt16? {
1518-
guard let value = peekNextValueIfPresent(ofType: UInt.self), !value.isNull else {
1519-
advanceToNextValue()
1520-
return nil
1521-
}
1382+
let value = try valueFromIterator(ofType: UInt.self)
15221383
return try decodeInteger(value)
15231384
}
15241385

15251386
mutating func decode(_: UInt32.Type) throws -> UInt32 {
1526-
let value = try peekNextValue(ofType: UInt.self)
1527-
return try decodeInteger(value)
1528-
}
1529-
1530-
mutating func decodeIfPresent(_: UInt32.Type) throws -> UInt32? {
1531-
guard let value = peekNextValueIfPresent(ofType: UInt.self), !value.isNull else {
1532-
advanceToNextValue()
1533-
return nil
1534-
}
1387+
let value = try valueFromIterator(ofType: UInt.self)
15351388
return try decodeInteger(value)
15361389
}
15371390

15381391
mutating func decode(_: UInt64.Type) throws -> UInt64 {
1539-
let value = try peekNextValue(ofType: UInt.self)
1392+
let value = try valueFromIterator(ofType: UInt.self)
15401393
return try decodeInteger(value)
15411394
}
15421395

15431396
#if compiler(>=6.0)
15441397
@available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, visionOS 2.0, *)
15451398
mutating func decode(_: UInt128.Type) throws -> UInt128 {
1546-
let value = try peekNextValue(ofType: UInt.self)
1399+
let value = try valueFromIterator(ofType: UInt.self)
15471400
return try decodeInteger(value)
15481401
}
15491402
#endif
15501403

1551-
mutating func decodeIfPresent(_: UInt64.Type) throws -> UInt64? {
1552-
guard let value = peekNextValueIfPresent(ofType: UInt.self), !value.isNull else {
1553-
advanceToNextValue()
1554-
return nil
1555-
}
1556-
return try decodeInteger(value)
1557-
}
1558-
15591404
mutating func decode<T: Decodable>(_ type: T.Type) throws -> T {
1560-
let value = try self.peekNextValue(ofType: type)
1561-
let result = try impl.unbox(value, as: type, for: codingPathNode, currentIndexKey)
1562-
advanceToNextValue()
1563-
return result
1564-
}
1565-
1566-
mutating func decodeIfPresent<T: Decodable>(_ type: T.Type) throws -> T? {
1567-
guard let value = self.peekNextValueIfPresent(ofType: type), !value.isNull else {
1568-
advanceToNextValue()
1569-
return nil
1570-
}
1405+
let value = try valueFromIterator(ofType: type)
15711406
let result = try impl.unbox(value, as: type, for: codingPathNode, currentIndexKey)
15721407
advanceToNextValue()
15731408
return result
15741409
}
15751410

15761411
mutating func nestedContainer<NestedKey: CodingKey>(keyedBy type: NestedKey.Type) throws -> KeyedDecodingContainer<NestedKey> {
1577-
let value = try self.peekNextValue(ofType: KeyedDecodingContainer<NestedKey>.self)
1412+
let value = try valueFromIterator(ofType: KeyedDecodingContainer<NestedKey>.self)
15781413
let container = try impl.with(value: value, path: codingPathNode.appending(index: currentIndex)) {
15791414
try impl.container(keyedBy: type)
15801415
}
@@ -1583,7 +1418,7 @@ private struct JSONUnkeyedDecodingContainer: UnkeyedDecodingContainer {
15831418
}
15841419

15851420
mutating func nestedUnkeyedContainer() throws -> UnkeyedDecodingContainer {
1586-
let value = try self.peekNextValue(ofType: UnkeyedDecodingContainer.self)
1421+
let value = try valueFromIterator(ofType: UnkeyedDecodingContainer.self)
15871422
let container = try impl.with(value: value, path: codingPathNode.appending(index: currentIndex)) {
15881423
try impl.unkeyedContainer()
15891424
}
@@ -1592,7 +1427,7 @@ private struct JSONUnkeyedDecodingContainer: UnkeyedDecodingContainer {
15921427
}
15931428

15941429
mutating func superDecoder() throws -> Decoder {
1595-
let value = try self.peekNextValue(ofType: Decoder.self)
1430+
let value = try valueFromIterator(ofType: Decoder.self)
15961431
let decoder = JSONDecoderImpl(
15971432
json: value,
15981433
userInfo: impl.userInfo,

0 commit comments

Comments
 (0)