Skip to content

Commit 778103d

Browse files
committed
Fixes #125
1 parent bfbbca0 commit 778103d

File tree

4 files changed

+48
-23
lines changed

4 files changed

+48
-23
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
<br>
99

1010
## master
11-
Nothing yet
11+
### Fixed
12+
- The key of a scalar value after a compound value is lost (#125). Thanks [@smiletoeverybody](https://github.com/smiletoeverybody)
1213

1314
<br>
1415

src/Parser.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,9 @@ private function createGenerator(): Generator
266266
// no break
267267
case ']':
268268
--$currentLevel;
269+
if ($currentLevel < $iteratorLevel) {
270+
$iteratorStruct = null;
271+
}
269272
$inObject = $stack[$currentLevel] == '{';
270273
// no break
271274
default:

test/JsonMachineTest/ItemsTest.php

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -146,25 +146,4 @@ public function testCountViaIteratorCount()
146146

147147
$this->assertSame(3, iterator_count($items));
148148
}
149-
150-
public function testIssue125()
151-
{
152-
$data = '{
153-
"code": "some value",
154-
"key_one": 1,
155-
"key_two": [1]
156-
}';
157-
158-
$pointer = Items::fromString($data, ['pointer' => '/key_one']);
159-
$result = iterator_to_array($pointer);
160-
$this->assertSame([
161-
'key_one' => 1,
162-
], $result);
163-
164-
$pointer = Items::fromString($data, ['pointer' => '/key_two']);
165-
$result = iterator_to_array($pointer);
166-
$this->assertSame([
167-
0 => 1,
168-
], $result);
169-
}
170149
}

test/JsonMachineTest/ParserTest.php

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ public function testGeneratorQuitsAfterFirstFoundCollectionHasBeenFinished()
263263
public function testScalarResult()
264264
{
265265
$result = $this->createParser('{"result":{"items": [1,2,3],"count": 3}}', '/result/count');
266-
$this->assertSame([3], iterator_to_array($result));
266+
$this->assertSame(['count' => 3], iterator_to_array($result));
267267
}
268268

269269
public function testScalarResultInArray()
@@ -656,4 +656,46 @@ public function testGetPositionWorksInsideRecursion()
656656
$this->assertSame(0, $item->getPosition());
657657
}
658658
}
659+
660+
/**
661+
* @dataProvider data_testIssue125
662+
*/
663+
public function testIssue125TheKeyOfAScalarValueAfterACompoundValueIsLost($tokens)
664+
{
665+
$toAssert = [
666+
'keytwo' => 12,
667+
'keythree' => 13,
668+
];
669+
670+
foreach ($toAssert as $key => $value) {
671+
$items = new Parser(
672+
new \ArrayIterator($tokens),
673+
"/$key"
674+
);
675+
$result = iterator_to_array($items);
676+
$this->assertSame([
677+
$key => $value,
678+
], $result);
679+
}
680+
}
681+
682+
public function data_testIssue125()
683+
{
684+
return [
685+
'after array' => [[
686+
'{',
687+
'"keyone"', ':', '[', '11', ']', ',',
688+
'"keytwo"', ':', '12', ',',
689+
'"keythree"', ':', '13',
690+
'}',
691+
]],
692+
'after object' => [[
693+
'{',
694+
'"keyone"', ':', '{', '"key"', ':', '11', '}', ',',
695+
'"keytwo"', ':', '12', ',',
696+
'"keythree"', ':', '13',
697+
'}',
698+
]],
699+
];
700+
}
659701
}

0 commit comments

Comments
 (0)