diff --git a/src/JMS/Serializer/Handler/DateHandler.php b/src/JMS/Serializer/Handler/DateHandler.php index 22505816c..68daa7215 100644 --- a/src/JMS/Serializer/Handler/DateHandler.php +++ b/src/JMS/Serializer/Handler/DateHandler.php @@ -69,6 +69,11 @@ public function serializeDateTime(VisitorInterface $visitor, \DateTime $date, ar if ($visitor instanceof XmlSerializationVisitor && false === $this->xmlCData) { return $visitor->visitSimpleString($date->format($this->getFormat($type)), $type, $context); } + + $format = $this->getFormat($type); + if ('U' === $format) { + return $visitor->visitInteger($date->format($format), $type, $context); + } return $visitor->visitString($date->format($this->getFormat($type)), $type, $context); } diff --git a/tests/JMS/Serializer/Tests/Fixtures/Timestamp.php b/tests/JMS/Serializer/Tests/Fixtures/Timestamp.php new file mode 100755 index 000000000..3facc0413 --- /dev/null +++ b/tests/JMS/Serializer/Tests/Fixtures/Timestamp.php @@ -0,0 +1,34 @@ + + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +namespace JMS\Serializer\Tests\Fixtures; + +use JMS\Serializer\Annotation\Type; + +class Timestamp +{ + /** + * @Type("DateTime<'U'>") + */ + private $timestamp; + + public function __construct($timestamp) + { + $this->timestamp = $timestamp; + } +} diff --git a/tests/JMS/Serializer/Tests/Serializer/BaseSerializationTest.php b/tests/JMS/Serializer/Tests/Serializer/BaseSerializationTest.php index d3d49bbf1..4dc63ef9e 100644 --- a/tests/JMS/Serializer/Tests/Serializer/BaseSerializationTest.php +++ b/tests/JMS/Serializer/Tests/Serializer/BaseSerializationTest.php @@ -29,6 +29,7 @@ use JMS\Serializer\Tests\Fixtures\Garage; use JMS\Serializer\Tests\Fixtures\InlineChildEmpty; use JMS\Serializer\Tests\Fixtures\NamedDateTimeArraysObject; +use JMS\Serializer\Tests\Fixtures\Timestamp; use JMS\Serializer\Tests\Fixtures\Tree; use JMS\Serializer\Tests\Fixtures\VehicleInterfaceGarage; use PhpCollection\Sequence; @@ -365,6 +366,22 @@ public function getDateTime() ); } + /** + * @dataProvider getTimestamp + * @group datetime + */ + public function testTimestamp($key, $value) + { + $this->assertEquals($this->getContent($key), $this->serialize($value)); + } + + public function getTimestamp() + { + return array( + array('timestamp', new Timestamp(new \DateTime('2016-02-11 00:00:00', new \DateTimeZone('UTC')))), + ); + } + public function testDateInterval() { $duration = new \DateInterval('PT45M'); diff --git a/tests/JMS/Serializer/Tests/Serializer/JsonSerializationTest.php b/tests/JMS/Serializer/Tests/Serializer/JsonSerializationTest.php index dee76d326..5e184e7b7 100644 --- a/tests/JMS/Serializer/Tests/Serializer/JsonSerializationTest.php +++ b/tests/JMS/Serializer/Tests/Serializer/JsonSerializationTest.php @@ -89,6 +89,7 @@ protected function getContent($key) $outputs['object_when_null'] = '{"text":"foo"}'; $outputs['object_when_null_and_serialized'] = '{"author":null,"text":"foo"}'; $outputs['date_time'] = '"2011-08-30T00:00:00+0000"'; + $outputs['timestamp'] = '{"timestamp":1455148800}'; $outputs['date_interval'] = '"PT45M"'; $outputs['car'] = '{"km":5,"type":"car"}'; $outputs['car_without_type'] = '{"km":5}'; diff --git a/tests/JMS/Serializer/Tests/Serializer/xml/timestamp.xml b/tests/JMS/Serializer/Tests/Serializer/xml/timestamp.xml new file mode 100755 index 000000000..a9bf077fa --- /dev/null +++ b/tests/JMS/Serializer/Tests/Serializer/xml/timestamp.xml @@ -0,0 +1,4 @@ + + + 1455148800 + diff --git a/tests/JMS/Serializer/Tests/Serializer/yml/timestamp.yml b/tests/JMS/Serializer/Tests/Serializer/yml/timestamp.yml new file mode 100755 index 000000000..b1c38be9d --- /dev/null +++ b/tests/JMS/Serializer/Tests/Serializer/yml/timestamp.yml @@ -0,0 +1 @@ +timestamp: 1455148800