protobuf icon indicating copy to clipboard operation
protobuf copied to clipboard

Python: json_format.ParseValue

Open xxgreg opened this issue 2 years ago • 9 comments

What language does this apply to?

Python

Describe the problem you are trying to solve.

I would like to read a python value into a google.protobuf.Value type.

There is a json_format.ParseDict() function, however this only works if the top-level value is a google.protobuf.Struct.

It is already possible to use the unexported methods of the package to solve the problem, as in the example below.

Example of using unexported _Parser type to solve the problem.

def parse_value(value, target_pb):
    parser = json_format._Parser(False, None, 100)
    parser._ConvertValueMessage(value, target_pb, '')
    return value

Also - apologies if there is already a way to do this. If that is the case - treat this as a doc bug ;)

Describe the solution you'd like

One solution is to add a top-level json_format.ParseValue() function analogous to json_format.ParseDict().

Cheers team!

xxgreg avatar Apr 22 '23 05:04 xxgreg

Hmmm it looks like the following works:

v = struct_pb2.Value()
json_format.ParseDict(5, v)
print(v)

So this is a doc bug - not a feature request.

Consider updating the following description to make this behaviour clear.

def ParseDict(js_dict,
              message,
              ignore_unknown_fields=False,
              descriptor_pool=None,
              max_recursion_depth=100):
  """Parses a JSON dictionary representation into a message.

  Args:
    js_dict: Dict representation of a JSON message.
    message: A protocol buffer message to merge into.
    ignore_unknown_fields: If True, do not raise errors for unknown fields.
    descriptor_pool: A Descriptor Pool for resolving types. If None use the
      default.
    max_recursion_depth: max recursion depth of JSON message to be
      deserialized. JSON messages over this depth will fail to be
      deserialized. Default value is 100.

  Returns:
    The same message passed as argument.
  """

xxgreg avatar Apr 22 '23 05:04 xxgreg

Please feel free to contribute any documentation improvements that you think would make this behavior clearer here!

zhangskz avatar Apr 24 '23 15:04 zhangskz

How about the following changes?

If I can get some feedback here, I'd be happy to turn this into a PR.

Add the following to def ParseDict():

In the special case where the message type is google.protobuf.Value, or google.protobuf.ListValue, the js_dict argument is the corresponding python primitive type.

Add the following to def MessageToDict():

In the special case where the message type is google.protobuf.Value, or google.protobuf.ListValue, the returned value is the corresponding python primitive type.

xxgreg avatar Apr 25 '23 00:04 xxgreg

Sorry for the two-year delay in responding. I looked in the existing documentation for coverage of ParseDict and MessageToDict and didn't find any. Where did you imagine these additions would go?

Logofile avatar Mar 20 '25 19:03 Logofile

Just cc @anandolee who might know a place

shaod2 avatar Apr 17 '25 14:04 shaod2

ah maybe we can just elaborate the docstring from https://github.com/protocolbuffers/protobuf/blob/main/python/google/protobuf/json_format.py

and it should be reflected from https://googleapis.dev/python/protobuf/latest/google/protobuf/json_format.html for next releases

shaod2 avatar Apr 17 '25 14:04 shaod2

@shaod2 Can I work on this?

harupy avatar Apr 18 '25 06:04 harupy

@harupy, if you want to work on it, you can send a PR to https://github.com/protocolbuffers/protocolbuffers.github.io. Thanks!

Logofile avatar May 01 '25 16:05 Logofile

We triage inactive PRs and issues in order to make it easier to find active work. If this issue should remain active or becomes active again, please add a comment.

This issue is labeled inactive because the last activity was over 90 days ago. This issue will be closed and archived after 14 additional days without activity.

github-actions[bot] avatar Dec 09 '25 10:12 github-actions[bot]