Add OPT_ASSOC for roundtrip serialization of map.
Since PHP has no real array, unpacking both array and map into array() can cause loss of information.
The patch adds OPT_ASSOC = false option to support roundtrip serialization of map by packing stdClass into map and vice versa like json_encode/decode() does.
This will improve compatibility a bit with data generated by other langauge bindings, while preserving the previous behavior when OPT_ASSOC = true (default).
Related to #18 #45
It looks like this was never updated. Is there another way to deserialze to an object instead of an associative array?
Hi! Thanks for your submission.
Please rebase this PR, so we can have a closer look!
Done.
Codecov Report
Merging #58 into master will increase coverage by
0.17%. The diff coverage is92.3%.
@@ Coverage Diff @@
## master #58 +/- ##
==========================================
+ Coverage 85.79% 85.97% +0.17%
==========================================
Files 8 8
Lines 1345 1369 +24
==========================================
+ Hits 1154 1177 +23
- Misses 191 192 +1
| Impacted Files | Coverage Δ | |
|---|---|---|
| msgpack_pack.c | 94.42% <100%> (+0.04%) |
:arrow_up: |
| msgpack_unpack.c | 82.76% <100%> (+0.35%) |
:arrow_up: |
| msgpack.c | 93.33% <100%> (+0.12%) |
:arrow_up: |
| msgpack_class.c | 87.55% <88.88%> (+0.12%) |
:arrow_up: |
Continue to review full report at Codecov.
Legend - Click here to learn more
Δ = absolute <relative> (impact),ø = not affected,? = missing dataPowered by Codecov. Last update 7eb85f2...6b1845a. Read the comment docs.
usage:
$msgpack = new MessagePack();
$msgpack->setOption(MessagePack::OPT_PHPONLY, false);
$msgpack->setOption(MessagePack::OPT_ASSOC, false);
$data = [0 => 1, 1 => 2, 2 => 3];
var_dump(bin2hex($msgpack->pack($data)));
// string(8) "93010203"
var_dump(bin2hex($msgpack->pack((object)$data)));
// string(20) "83a13001a13102a13203"
@m6w6 Is this still considered? I would love to have this feature implemented so the outcome is aligned with json!