Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Provide a to_dict method #151

Closed
busunkim96 opened this issue Oct 15, 2020 · 2 comments · Fixed by #154
Closed

Provide a to_dict method #151

busunkim96 opened this issue Oct 15, 2020 · 2 comments · Fixed by #154
Assignees
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.

Comments

@busunkim96
Copy link
Contributor

I think to_dict would be a clone of to_json that calls MessageToDict instead of MessageToJson.

def to_json(cls, instance, *, use_integers_for_enums=True) -> str:
"""Given a message instance, serialize it to json
Args:
instance: An instance of this message type, or something
compatible (accepted by the type's constructor).
use_integers_for_enums (Optional(bool)): An option that determines whether enum
values should be represented by strings (False) or integers (True).
Default is True.
Returns:
str: The json string representation of the protocol buffer.
"""
return MessageToJson(
cls.pb(instance),
use_integers_for_enums=use_integers_for_enums,
including_default_value_fields=True,
)

Currently if you want to turn an existing message into a dict you have to go through JSON. Prompted by discussion in googleapis/python-memcache#19.

import json

from google.cloud import memcache

# to json
instance = memcache.Instance(name="projects/project/locations/location/instances/instance")
instance_json = memcache.Instance.to_json(instance)
# to dict
instance_dict = json.loads(instance_json)
@busunkim96 busunkim96 added the type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. label Oct 15, 2020
@software-dov
Copy link
Contributor

It should be trivial to do. Is there a rough priority?

@busunkim96
Copy link
Contributor Author

@software-dov This is not blocking anyone so not high priority. :) Just wanted to log it here to get your input.

@busunkim96 busunkim96 added the priority: p2 Moderately-important priority. Fix may not be included in next release. label Oct 16, 2020
gcf-merge-on-green bot pushed a commit that referenced this issue Oct 19, 2020
Parallels the `to_json` method.
Also adds options to ignore unknown fields during deserializtion.

Closes #153 
Closes #151
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants