msgpack-php icon indicating copy to clipboard operation
msgpack-php copied to clipboard

Add OPT_ASSOC for roundtrip serialization of map.

Open ranvis opened this issue 10 years ago • 6 comments

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

ranvis avatar Aug 28 '15 12:08 ranvis

It looks like this was never updated. Is there another way to deserialze to an object instead of an associative array?

doc987 avatar Aug 04 '18 23:08 doc987

Hi! Thanks for your submission.

Please rebase this PR, so we can have a closer look!

m6w6 avatar Dec 18 '19 16:12 m6w6

Done.

ranvis avatar Dec 18 '19 21:12 ranvis

Codecov Report

Merging #58 into master will increase coverage by 0.17%. The diff coverage is 92.3%.

Impacted file tree graph

@@            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 data Powered by Codecov. Last update 7eb85f2...6b1845a. Read the comment docs.

codecov-io avatar Dec 18 '19 21:12 codecov-io

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"

ranvis avatar Dec 18 '19 22:12 ranvis

@m6w6 Is this still considered? I would love to have this feature implemented so the outcome is aligned with json!

taka-oyama avatar Apr 14 '23 08:04 taka-oyama