Skip to content

Commit

Permalink
Merge pull request #24 from webmozart/empty-keys
Browse files Browse the repository at this point in the history
Added support for empty properties (fixes #19)
  • Loading branch information
webmozart authored Aug 10, 2016
2 parents 51bf9f4 + 9ff9408 commit c0696e3
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Changelog
* added support for `$ref` to external schema
* added support for validation against `$schema` property
* added `LocalUriRetriever`
* added support for empty properties before PHP 7.1

* 1.2.2 (2016-01-14)

Expand Down
15 changes: 15 additions & 0 deletions src/JsonEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,21 @@ public function encode($data, $schema = null)
}
}

if (PHP_VERSION_ID < 71000) {
// PHP before 7.1 decodes empty properties as "_empty_". Make
// sure the encoding of these properties works again.
if (is_object($data) && isset($data->{'_empty_'})) {
$data = (array) $data;
}

if (is_array($data) && isset($data['_empty_'])) {
// Maintain key order
$keys = array_keys($data);
$keys[array_search('_empty_', $keys, true)] = '';
$data = array_combine($keys, $data);
}
}

if (PHP_VERSION_ID >= 50500) {
$maxDepth = $this->maxDepth;

Expand Down
48 changes: 48 additions & 0 deletions tests/JsonDecoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,54 @@ public function testDecodeObjectAsArray()
$this->assertEquals(array('name' => 'Bernhard'), $decoded);
}

public function testDecodeEmptyArrayKey()
{
$data = array('' => 'Bernhard');

$this->decoder->setObjectDecoding(JsonDecoder::ASSOC_ARRAY);

$this->assertEquals($data, $this->decoder->decode('{"":"Bernhard"}'));
}

public function testDecodeEmptyProperty()
{
if (version_compare(PHP_VERSION, '7.1.0', '<')) {
$this->markTestSkipped('PHP >= 7.1.0 only');

return;
}

$data = (object) array('' => 'Bernhard');

$this->assertEquals($data, $this->decoder->decode('{"":"Bernhard"}'));
}

public function testDecodeMagicEmptyPropertyAfter71()
{
if (version_compare(PHP_VERSION, '7.1.0', '<')) {
$this->markTestSkipped('PHP >= 7.1.0 only');

return;
}

$data = (object) array('_empty_' => 'Bernhard');

$this->assertEquals($data, $this->decoder->decode('{"_empty_":"Bernhard"}'));
}

public function testDecodeMagicEmptyPropertyBefore71()
{
if (version_compare(PHP_VERSION, '7.1.0', '>=')) {
$this->markTestSkipped('PHP < 7.1.0 only');

return;
}

$data = (object) array('a' => 'b', '_empty_' => 'Bernhard', 'c' => 'd');

$this->assertEquals($data, $this->decoder->decode('{"a":"b","":"Bernhard","c":"d"}'));
}

public function provideInvalidObjectDecoding()
{
return array(
Expand Down
46 changes: 46 additions & 0 deletions tests/JsonEncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,52 @@ public function testEncodeFailsForBinaryValue()
$this->encoder->encode(self::BINARY_INPUT);
}

public function testEncodeEmptyArrayKey()
{
$data = array('' => 'Bernhard');

$this->assertSame('{"":"Bernhard"}', $this->encoder->encode($data));
}

public function testEncodeEmptyProperty()
{
if (version_compare(PHP_VERSION, '7.1.0', '<')) {
$this->markTestSkipped('PHP >= 7.1.0 only');

return;
}

$data = (object) array('' => 'Bernhard');

$this->assertSame('{"":"Bernhard"}', $this->encoder->encode($data));
}

public function testEncodeMagicEmptyPropertyAfter71()
{
if (version_compare(PHP_VERSION, '7.1.0', '<')) {
$this->markTestSkipped('PHP >= 7.1.0 only');

return;
}

$data = (object) array('_empty_' => 'Bernhard');

$this->assertSame('{"_empty_":"Bernhard"}', $this->encoder->encode($data));
}

public function testEncodeMagicEmptyPropertyBefore71()
{
if (version_compare(PHP_VERSION, '7.1.0', '>=')) {
$this->markTestSkipped('PHP < 7.1.0 only');

return;
}

$data = (object) array('a' => 'b', '_empty_' => 'Bernhard', 'c' => 'd');

$this->assertSame('{"a":"b","":"Bernhard","c":"d"}', $this->encoder->encode($data));
}

public function testEncodeArrayAsArray()
{
$data = array('one', 'two');
Expand Down

0 comments on commit c0696e3

Please sign in to comment.