Comments (2)
Looks like this is still outstanding. I tried sending a serialized Date
object from Javascript, and it was deserialized to the object shown below. I was hoping to get a DateTime
object instead.
MessagePack\Ext Object
(
[type] => -1
[data] => some binary data
)
I wasn't able to get a type transformer to work either. The unpack
method in the compressed example below fails.
class DateTimeTransformer implements \MessagePack\TypeTransformer\Unpackable
{
private $type;
public function __construct(int $type)
{
$this->type = $type;
}
public function getType() : int
{
return $this->type;
}
public function unpack(\MessagePack\BufferUnpacker $unpacker, int $extLength)
{
//how does one unpack a timestamp?
return new \DateTimeImmutable($unpacker->unpack???());
}
}
$transformer = new DateTimeTransformer(-1);
$unpacker = new \MessagePack\BufferUnpacker();
$unpacker->registerTransformer($transformer);
$data = $unpacker->reset($packed)->unpack();
from msgpack.php.
@doc987 I took msgpack-lite library as an example from your other issue (msgpack/msgpack-php#127). As you noticed, it has native support for Date
objects (ext type 0x0D). So, to be able to deserialize these dates into PHP DateTime(Immutable)
objects you can create the following transformer:
namespace App\MessagePack;
use MessagePack\BufferUnpacker;
use MessagePack\TypeTransformer\Unpackable;
final class MsgpackLiteJsDateTransformer implements Unpackable
{
public function getType() : int
{
return 0x0d;
}
public function unpack(BufferUnpacker $unpacker, int $extLength) : \DateTimeImmutable
{
return \DateTimeImmutable::createFromFormat('U.u', $unpacker->unpackFloat() / 1000);
}
}
Test:
namespace App\MessagePack;
use MessagePack\BufferUnpacker;
// msgpack.encode(new Date('2018-08-07T22:21:45.580Z')) = c7090dcb42765167b52ec000
$packedJsDate = hex2bin('c7090dcb42765167b52ec000');
$unpacker = new BufferUnpacker($packedJsDate);
$unpacker->registerTransformer(new MsgpackLiteJsDateTransformer());
var_dump($unpacker->unpack());
/*
class DateTimeImmutable#4 (3) {
public $date =>
string(26) "2018-08-07 22:21:45.580000"
public $timezone_type =>
int(1)
public $timezone =>
string(6) "+00:00"
}
*/
from msgpack.php.
Related Issues (14)
- Use generators for unpackArray/unpackMap HOT 4
- Add packer flags to disable utf8/bin and array/map auto-detection
- Add an ability to pack to single-precision floats
- tryUnpack - how do you know when to return while streaming HOT 2
- Bump minimum PHP version requirement to 7.x
- Optimize packing map keys
- BufferUnpacker with Type transformers not decoding correctly HOT 4
- Do you know the folks doing the PHP C module? HOT 4
- Installing without composer HOT 1
- PHP 8 support HOT 5
- Use array_is_list() for detecting array/map HOT 2
- Consider making PackOptions::FORCE_STR the default option
- Inline BufferUnpacker::ensureLength() and compare perf
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from msgpack.php.