{"action":"opened","issue":{"url":"https://api.github.com/repos/marshmallow-code/marshmallow/issues/1043","repository_url":"https://api.github.com/repos/marshmallow-code/marshmallow","labels_url":"https://api.github.com/repos/marshmallow-code/marshmallow/issues/1043/labels{/name}","comments_url":"https://api.github.com/repos/marshmallow-code/marshmallow/issues/1043/comments","events_url":"https://api.github.com/repos/marshmallow-code/marshmallow/issues/1043/events","html_url":"https://github.com/marshmallow-code/marshmallow/issues/1043","id":378228749,"node_id":"MDU6SXNzdWUzNzgyMjg3NDk=","number":1043,"title":"Combine  schema definition with object definition","user":{"login":"rainmanwy","id":6995987,"node_id":"MDQ6VXNlcjY5OTU5ODc=","avatar_url":"https://avatars3.githubusercontent.com/u/6995987?v=4","gravatar_id":"","url":"https://api.github.com/users/rainmanwy","html_url":"https://github.com/rainmanwy","followers_url":"https://api.github.com/users/rainmanwy/followers","following_url":"https://api.github.com/users/rainmanwy/following{/other_user}","gists_url":"https://api.github.com/users/rainmanwy/gists{/gist_id}","starred_url":"https://api.github.com/users/rainmanwy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rainmanwy/subscriptions","organizations_url":"https://api.github.com/users/rainmanwy/orgs","repos_url":"https://api.github.com/users/rainmanwy/repos","events_url":"https://api.github.com/users/rainmanwy/events{/privacy}","received_events_url":"https://api.github.com/users/rainmanwy/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-11-07T10:30:22Z","updated_at":"2018-11-07T10:30:22Z","closed_at":null,"author_association":"NONE","body":"This is a great library, i like it:)\r\n\r\nI have tried to find a python library to convert json to object and vise versa(like gson, fastjson in java). And i have tried this library and want to use it.\r\n\r\nOne question is i have to define schema class and object class. For example:\r\nclass User:\r\n    pass\r\n\r\nclass UserSchema(Schema):\r\n    pass\r\n\r\nI have tried to solve it myself, and below is my implementation:\r\n`\r\nclass _MetaClass(SchemaMeta):\r\n\r\n    def __call__(self, **kwargs):\r\n        inst = super(_MetaClass, self).__call__()\r\n        for attr in inst.declared_fields:\r\n            if hasattr(inst.declared_fields[attr], 'many'):\r\n                setattr(inst, attr, [])\r\n            else:\r\n                setattr(inst, attr, None)\r\n        for k, v in kwargs.items():\r\n            if k in inst.declared_fields:\r\n                setattr(inst, k, v)\r\n        return inst\r\n\r\n\r\nclass BaseClass(Schema, metaclass=_MetaClass):\r\n\r\n    @post_load\r\n    def create(self, data):\r\n        return self.__class__(**data)\r\n\r\n    def __repr__(self):\r\n        return str(self.to_json())\r\n\r\n    def to_json(self):\r\n        out = {}\r\n        for key in self.declared_fields:\r\n            v = getattr(self, key)\r\n            if isinstance(v, BaseClass):\r\n                out[key] = str(v)\r\n            elif isinstance(v, (int, float)) or v is None:\r\n                out[key] = v\r\n            else:\r\n                out[key] = str(v)\r\n        return out\r\n`\r\nI am not sure whether there is common solution for that. And with my implementation, i met a problem when use Nest with many\r\n`\r\nclass Student(BaseClass):\r\n    name = fields.String()\r\n    birthday = fields.DateTime()\r\n    age = fields.Integer()\r\n    money = fields.Decimal()\r\n\r\n\r\nclass Class(BaseClass):\r\n    class_name = fields.String()\r\n    students = fields.List(fields.Nested(Student))  # type: List[Student]\r\n    # students = fields.Nested(Student, many=True)  # type: List[Student]\r\n`\r\nwhen using \"students = fields.Nested(Student, many=True)\", lib will report schema verify failed. I am not sure whether i made some mistakes.\r\n\r\nAgain, thank you to develop this greate library.\r\n"}}
{"actor":{"display_login":"rainmanwy"}}