Skip to content

Commit

Permalink
Merge pull request #52 from marcimat/issue_51_datatimeimmutable
Browse files Browse the repository at this point in the history
Fix #51 (Deprecated with DateTimeImmutable on PHP 8.2)
  • Loading branch information
jrbasso authored Jul 13, 2023
2 parents 421dcdd + c685af5 commit ef82b51
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
### Fixed
- Fixed deprecated with DateTimeImmutable deserialization with PHP 8.2

## [3.0.0] - 2020-07-20
### Fixed
- Fixed DateTime & DateTimeImmutable serialization in PHP 7.4+. Thanks @przemyslaw-bogusz
Expand Down
2 changes: 1 addition & 1 deletion src/JsonSerializer/JsonSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ protected function unserializeObject($value)
throw new JsonSerializerException('Unable to find class ' . $className);
}

if ($className === 'DateTime') {
if ($className === 'DateTime' || $className === 'DateTimeImmutable') {
$obj = $this->restoreUsingUnserialize($className, $value);
$this->objectMapping[$this->objectMappingIndex++] = $obj;
return $obj;
Expand Down
4 changes: 4 additions & 0 deletions tests/JsonSerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,10 @@ public function testSerializationOfDateTime()
$date = new \DateTime('2014-06-15 12:00:00', new \DateTimeZone('UTC'));
$obj = $this->serializer->unserialize($this->serializer->serialize($date));
$this->assertSame($date->getTimestamp(), $obj->getTimestamp());

$date = new \DateTimeImmutable('2014-06-15 12:00:00', new \DateTimeZone('UTC'));
$obj = $this->serializer->unserialize($this->serializer->serialize($date));
$this->assertSame($date->getTimestamp(), $obj->getTimestamp());
}

/**
Expand Down

0 comments on commit ef82b51

Please sign in to comment.