Skip to content

Commit

Permalink
Remove default_packet for msgpack
Browse files Browse the repository at this point in the history
  • Loading branch information
Fatal1ty committed Nov 27, 2018
1 parent cd220b1 commit a578e1e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
9 changes: 1 addition & 8 deletions mashumaro/serializer/msgpack.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
import datetime

import msgpack

from mashumaro.serializer.base import DataClassDictMixin


def default_packer(o):
if isinstance(o, datetime.datetime):
return o.timestamp()


class DataClassMessagePackMixin(DataClassDictMixin):
def to_msgpack(self):
d = self.to_dict(use_bytes=True)
return msgpack.packb(d, default=default_packer, use_bin_type=True)
return msgpack.packb(d, use_bin_type=True)

@classmethod
def from_msgpack(cls, data: bytes):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name="mashumaro",
version="1.0",
version="1.1",
description="Fast serialization framework on top of dataclasses",
long_description=open('README.md').read(),
long_description_content_type='text/markdown',
Expand Down
10 changes: 10 additions & 0 deletions tests/test_msgpack.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from dataclasses import dataclass
from typing import List
from datetime import datetime

import msgpack

Expand All @@ -20,3 +21,12 @@ class DataClass(DataClassMessagePackMixin):
x: List[int]
dumped = msgpack.packb({'x': [1, 2, 3]})
assert DataClass.from_msgpack(dumped) == DataClass([1, 2, 3])


def test_to_msg_pack_datetime():
@dataclass
class DataClass(DataClassMessagePackMixin):
x: datetime
dt = datetime(2018, 10, 29, 12, 46, 55, 308495)
dumped = msgpack.packb({'x': dt.isoformat()})
assert DataClass(dt).to_msgpack() == dumped

0 comments on commit a578e1e

Please sign in to comment.