{"action":"closed","issue":{"url":"https://api.github.com/repos/marshmallow-code/marshmallow/issues/533","repository_url":"https://api.github.com/repos/marshmallow-code/marshmallow","labels_url":"https://api.github.com/repos/marshmallow-code/marshmallow/issues/533/labels{/name}","comments_url":"https://api.github.com/repos/marshmallow-code/marshmallow/issues/533/comments","events_url":"https://api.github.com/repos/marshmallow-code/marshmallow/issues/533/events","html_url":"https://github.com/marshmallow-code/marshmallow/issues/533","id":178299057,"number":533,"title":"Bytes field and RawString field","user":{"login":"asmodehn","id":221256,"avatar_url":"https://avatars.githubusercontent.com/u/221256?v=3","gravatar_id":"","url":"https://api.github.com/users/asmodehn","html_url":"https://github.com/asmodehn","followers_url":"https://api.github.com/users/asmodehn/followers","following_url":"https://api.github.com/users/asmodehn/following{/other_user}","gists_url":"https://api.github.com/users/asmodehn/gists{/gist_id}","starred_url":"https://api.github.com/users/asmodehn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/asmodehn/subscriptions","organizations_url":"https://api.github.com/users/asmodehn/orgs","repos_url":"https://api.github.com/users/asmodehn/repos","events_url":"https://api.github.com/users/asmodehn/events{/privacy}","received_events_url":"https://api.github.com/users/asmodehn/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2016-09-21T09:52:33Z","updated_at":"2016-10-05T03:08:34Z","closed_at":"2016-10-05T03:08:34Z","body":"Not sure if these would make sense for marshmallow as a default fields, but while working with marshmallow and existing library doing network serialization/deserialization ([ROS](http://wiki.ros.org/msg), I came to need a field that would do : \r\n\r\nPy2 : str --serialize--> str --deserialize--> str\r\nPy3 : bytes --serialize--> bytes --deserialize--> bytes\r\n\r\nSo something similar to the string field, but for bytes/rawstring...\r\n\r\nI implemented a quick prototype for my usecase like : \r\n\r\n```python\r\ndef ensure_binary_type(val):\r\n    if isinstance(val, marshmallow.compat.text_type):\r\n        val = val.encode('utf-8')\r\n    return marshmallow.compat.binary_type(val)\r\n\r\n\r\nclass RosString(marshmallow.fields.Field):\r\n    \"\"\"A ros string, serializing as ros python field type :\r\n    Python <3.0 implies str, python >3.0 implies bytes\r\n\r\n    If you need unicode serialization, have a look at RosTextString.\r\n\r\n    No marshmallow field class for this, so we're declaring it here.\r\n\r\n    :param kwargs: The same keyword arguments that :class:`Field` receives. required is set to True by default.\r\n    \"\"\"\r\n    default_error_messages = {\r\n        'invalid': 'Not a valid binary string.'\r\n    }\r\n\r\n    def __init__(self, **kwargs):\r\n        super(RosString, self).__init__(required=True, **kwargs)\r\n\r\n    def _serialize(self, value, attr, obj):\r\n        if value is None:\r\n            return None\r\n        return ensure_binary_type(value)\r\n\r\n    def _deserialize(self, value, attr, data):\r\n        if not isinstance(value, marshmallow.compat.basestring):\r\n            self.fail('invalid')\r\n        return ensure_binary_type(value)\r\n\r\n```\r\n\r\nAlso I came to need another field that would serialize as unicode, and deserialize as str/bytes : \r\n\r\nPy2 : str --serialize--> unicode --deserialize--> str\r\nPy3 : bytes --serialize--> str --deserialize--> bytes\r\n\r\nprototype : \r\n```python\r\nclass RosTextString(RosString):\r\n    \"\"\"A ros string, serializing into unicode.\r\n     Python <3.0 means unicode --deserialize--> str, python >3.0 means str --deserialize--> bytes\r\n\r\n    For planning for python3, we serialize this as unicode string\r\n    For using with ROS we deserialize as str / bytes\r\n\r\n    No marshmallow field class for this, so we're declaring it here.\r\n\r\n    :param kwargs: The same keyword arguments that :class:`Field` receives. required is set to True by default.\r\n    \"\"\"\r\n    default_error_messages = {\r\n        'invalid': 'Not a valid text string.'\r\n    }\r\n\r\n    # we got this from marshmallow.fields.String\r\n    def _serialize(self, value, attr, obj):\r\n        if value is None:\r\n            return None\r\n        return marshmallow.utils.ensure_text_type(value)\r\n```\r\n\r\nIf one or both of these can be useful in marshmallow, I can start working on a PR, just let me know.\r\n\r\nCheers !"}}
{"actor":{"display_login":"sloria"}}