Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OPT_ASSOC for roundtrip serialization of map. #58

Merged
merged 1 commit into from
Aug 20, 2024

Conversation

ranvis
Copy link
Contributor

@ranvis ranvis commented Aug 28, 2015

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

@doc987
Copy link

doc987 commented Aug 4, 2018

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

@m6w6
Copy link
Collaborator

m6w6 commented Dec 18, 2019

Hi! Thanks for your submission.

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

Set OPT_ASSOC to false to pack stdClass instance in map and vice versa.
Set OPT_PHPONLY to false in addition to pack any object into map.
@ranvis
Copy link
Contributor Author

ranvis commented Dec 18, 2019

Done.

@codecov-io
Copy link

codecov-io commented Dec 18, 2019

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%) ⬆️
msgpack_unpack.c 82.76% <100%> (+0.35%) ⬆️
msgpack.c 93.33% <100%> (+0.12%) ⬆️
msgpack_class.c 87.55% <88.88%> (+0.12%) ⬆️

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.

@ranvis
Copy link
Contributor Author

ranvis commented Dec 18, 2019

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"

@taka-oyama
Copy link

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

@m6w6 m6w6 added this to the 2.1 milestone Jun 2, 2023
@m6w6 m6w6 merged commit 6b1845a into msgpack:master Aug 20, 2024
@codecov-commenter
Copy link

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

Attention: Patch coverage is 92.30769% with 2 lines in your changes missing coverage. Please review.

Project coverage is 85.97%. Comparing base (7eb85f2) to head (6b1845a).
Report is 131 commits behind head on master.

Files Patch % Lines
msgpack_class.c 88.88% 2 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            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     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

m6w6 added a commit that referenced this pull request Aug 23, 2024
* Add support for PHP-8.1 Enums
  * merges #173
  * fixes #171
* Add support for APCu
  * merges #162, #93
* Add OPT_ASSOC for roundtrip serialization of map.
  * merges #58
m6w6 added a commit that referenced this pull request Sep 25, 2024
* Add support for PHP-8.1 Enums
  * merges #173
  * fixes #171
* Add support for APCu
  * merges #162, #93
* Add OPT_ASSOC for roundtrip serialization of map.
  * merges #58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants