Skip to content

Commit c0fc9b5

Browse files
committed
[JSON] Collection values in JSONMap to store the mapSize
There's no reason to '- 2'/'+ 2'.
1 parent 401229c commit c0fc9b5

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

Sources/SwiftCompilerPluginMessageHandling/JSON/JSONDecoding.swift

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func decodeFromJSON<T: Decodable>(json: UnsafeBufferPointer<UInt8>) throws -> T
5353
For JSON payload such as:
5454

5555
```
56-
{"array": [-1.3, true], "number": 42}
56+
{"foo": [-1.3, true], "barz": 42}
5757
```
5858

5959
will be scanned by 'JSONScanner' into a map like:
@@ -66,29 +66,29 @@ func decodeFromJSON<T: Decodable>(json: UnsafeBufferPointer<UInt8>) throws -> T
6666
<TL> == NULL Marker
6767
map: [
6868
0: <OM>, -- object marker
69-
1: 15, | `- number of *map* elements in this collection
70-
2: <SS>, | --- key 1: 'array'
69+
1: 17, | `- number of *map* elements this object occupies
70+
2: <SS>, | --- key 1: 'foo'
7171
3: <int_ptr>, | | |- pointer in the payload
72-
4: 5, | | `- length
72+
4: 3, | | `- length
7373
5: <AM>, | --- value 1: array
74-
6: 4, | | `- number of *map* elements in the array
74+
6: 6, | | `- number of *map* elements this array occupies
7575
7: <NM>, | | -- arr elm 1: '-1.3'
7676
8: <int_ptr>, | | |
7777
9: 4, | | |
7878
10: <TL>, | | -- arr elm 2: 'true'
79-
11: <SS>, | --- key 2: 'number'
79+
11: <SS>, | --- key 2: 'barz'
8080
12: <int_ptr>, | |
81-
13: 6, | |
81+
13: 4, | |
8282
14: <NM> | --- value 2: '42'
8383
15: <int_ptr>, | |
8484
16: 2, | |
8585
]
8686
```
87-
To decode '<root>.number' value:
87+
To decode '<root>.barz' value:
8888
1. Index 0 indicates it's a object.
89-
2. Parse a key string at index 2, which is not a match for "number"
90-
3. Skip the key's value by finding it's an array, then its 'index(afterValue:)' which is 11
91-
4. Parse a key string at index 11, matching "number"
89+
2. Parse a key string at index 2, which is "foo", not a match for "barz"
90+
3. Skip the key and the value by advancing the index by 'mapSize' of them, 3 and 6.
91+
4. Parse a key string at index 11, matching "barz"
9292
5. Parse a value number at the pointer of index 15, length at index 16
9393
*/
9494

@@ -147,7 +147,7 @@ private struct JSONMapBuilder {
147147
mutating func closeCollection(handle: Int) {
148148
// 'handle': descriptor index.
149149
// 'handle+1': counter index.
150-
mapData[handle + 1] = mapData.count - handle - 2
150+
mapData[handle + 1] = mapData.count - handle
151151
}
152152

153153
func finalize() -> JSONMap {
@@ -381,7 +381,7 @@ private struct JSONMapValue {
381381
case .number, .simpleString, .string:
382382
return 3
383383
case .array, .object:
384-
return 2 &+ data[1]
384+
return data[1]
385385
case nil:
386386
fatalError("invalid value descriptor")
387387
}

0 commit comments

Comments
 (0)