Skip to content

Correct json serialization of Enum object instead of just {} #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
}
},
"require": {
"php": ">=5.3"
"php": ">=5.4"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a BC break, the PR cannot be merged as is.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that as you can see in comment. You may consider to use that changeset in new major version or some other branch.
I just wanted to let you know that json problem exists and there is simple solution

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand, thank you for providing the patch.

There is no new BC-breaking version planned since this is just a minor thing and a PHP implementation is in the works for 7.1, so I'll be closing this. Users can still implement this in sub-classes, but I don't see a big value since it doesn't support deserialization.

},
"require-dev": {
"phpunit/phpunit": "4.*",
Expand Down
15 changes: 14 additions & 1 deletion src/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @author Daniel Costa <[email protected]>
* @author Mirosław Filip <[email protected]>
*/
abstract class Enum
abstract class Enum implements \JsonSerializable
{
/**
* Enum value
Expand Down Expand Up @@ -73,6 +73,19 @@ public function __toString()
return (string)$this->value;
}

/**
* (PHP 5 &gt;= 5.4.0)<br/>
* Specify data which should be serialized to JSON
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
* @return mixed data which can be serialized by <b>json_encode</b>,
* which is a value of any type other than a resource.
*/
public function jsonSerialize()
{
return $this->getValue();
}


/**
* Returns the names (keys) of all constants in the Enum class
*
Expand Down
15 changes: 15 additions & 0 deletions tests/EnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,21 @@ public function testToString($expected, $enumObject)
$this->assertSame($expected, (string) $enumObject);
}

public function testJsonEncode()
{
$value = new EnumFixture(EnumFixture::FOO);
$encoded = json_encode($value);
$this->assertEquals($encoded, '"'.EnumFixture::FOO.'"');

$value = new EnumFixture(EnumFixture::BAR);
$encoded = json_encode($value);
$this->assertEquals($encoded, '"'.EnumFixture::BAR.'"');

$value = new EnumFixture(EnumFixture::NUMBER);
$encoded = json_encode($value);
$this->assertEquals($encoded, EnumFixture::NUMBER);
}

public function toStringProvider() {
return array(
array(EnumFixture::FOO, new EnumFixture(EnumFixture::FOO)),
Expand Down