msgpack / msgpack-php

msgpack.org[PHP]
BSD 3-Clause "New" or "Revised" License
773 stars 119 forks source link

Add serialization and deserialization support for PHP 8.1 Enums. #173

Open seyfahni opened 3 months ago

seyfahni commented 3 months ago

Add support for enum classes. They are serialized with a new serialize type MSGPACK_SERIALIZE_TYPE_ENUM (dynamic id 6) and stored by using the enum class name as key and the enum case name as value.

For example the serialized message pack blob from the added test (generated by msgpack-inspect):

---
- format: "fixmap"
  header: "0x82"
  length: 2
  children:
    - key:
        format: "nil"
        header: "0xc0"
        data: "0xc0"
        value: null
      value:
        format: "fixint"
        header: "0x06"
        data: "0x06"
        value: 6
    - key:
        format: "fixstr"
        header: "0xa8"
        length: 8
        data: "0x54657374456e756d"
        value: "TestEnum"
      value:
        format: "fixstr"
        header: "0xa5"
        length: 5
        data: "0x4f54484552"
        value: "OTHER"

Fixes issue #171.

This is incompatible with enums serialized before this change as they have been serialized as normal classes and thus did not set a numeric serialize type but the class name instead. Thus, old data will continue to fail deserialization in the same way if it does contain a serialized enum.