Skip to content
New issue

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

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

Already on GitHub? # to your account

Provide dumps() method #99

Open
krismeister opened this issue Mar 6, 2020 · 2 comments
Open

Provide dumps() method #99

krismeister opened this issue Mar 6, 2020 · 2 comments

Comments

@krismeister
Copy link

krismeister commented Mar 6, 2020

The current docs do not give an example of serialization support for JSON.

I attempted to use Marshmallow's dumps feature

db.MyModel.Schema.dumps(someInstanceOfMyModel)
# throws this error:
TypeError: dumps() missing 1 required positional argument: 'obj' marshmallow

But received an error. I am currently using someInstanceOfMyModel._raw to get the data for JSON output.

Feature Request
Add a expose a dumps method to get a dict.

db.MyModel.dumps(someInstanceOfMyModel)
@ricky-sb
Copy link

ricky-sb commented Mar 6, 2020

Add a expose a dumps method to get a dict.

If you only want a dict, you can use the to_dict() method.

Try this: someInstanceOfMyModel.to_dict().

@Quidge
Copy link

Quidge commented Apr 28, 2020

The obj value that's missing is the instance. You need to use an instantiated Schema:

db.MyModel.Schema().dumps(someInstanceOfMyModel)

I think that the interface of the dumped data from that will vary depending on whether you have marshmallow 2.x or 3.x installed. There was a breaking api change between the two. My project uses marshmallow 2.x so I'm used to doing this to get the dumped data:

data, errors = db.MyModel.Schema().dumps(someInstanceOfMyModel)
# data will the serialized object, errors will be any errors encountered during serialization

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants