google.protobuf.json_format

Contains routines for printing protocol messages in JSON format.

Simple usage example:

# Create a proto object and serialize it to a json format string. message = my_proto_pb2.MyMessage(foo=’bar’) json_string = json_format.MessageToJson(message)

# Parse a json format string to proto object. message = json_format.Parse(json_string, my_proto_pb2.MyMessage())

exception google.protobuf.json_format.Error

Top-level module error for json_format.

args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

google.protobuf.json_format.MessageToDict(message, including_default_value_fields=False, preserving_proto_field_name=False, use_integers_for_enums=False, descriptor_pool=None, float_precision=None)

Converts protobuf message to a dictionary.

When the dictionary is encoded to JSON, it conforms to proto3 JSON spec.

Parameters:
  • message – The protocol buffers message instance to serialize.

  • including_default_value_fields – If True, singular primitive fields, repeated fields, and map fields will always be serialized. If False, only serialize non-empty fields. Singular message fields and oneof fields are not affected by this option.

  • preserving_proto_field_name – If True, use the original proto field names as defined in the .proto file. If False, convert the field names to lowerCamelCase.

  • use_integers_for_enums – If true, print integers instead of enum names.

  • descriptor_pool – A Descriptor Pool for resolving types. If None use the default.

  • float_precision – If set, use this to specify float field valid digits.

Returns:

A dict representation of the protocol buffer message.

google.protobuf.json_format.MessageToJson(message, including_default_value_fields=False, preserving_proto_field_name=False, indent=2, sort_keys=False, use_integers_for_enums=False, descriptor_pool=None, float_precision=None, ensure_ascii=True)

Converts protobuf message to JSON format.

Parameters:
  • message – The protocol buffers message instance to serialize.

  • including_default_value_fields – If True, singular primitive fields, repeated fields, and map fields will always be serialized. If False, only serialize non-empty fields. Singular message fields and oneof fields are not affected by this option.

  • preserving_proto_field_name – If True, use the original proto field names as defined in the .proto file. If False, convert the field names to lowerCamelCase.

  • indent – The JSON object will be pretty-printed with this indent level. An indent level of 0 or negative will only insert newlines.

  • sort_keys – If True, then the output will be sorted by field names.

  • use_integers_for_enums – If true, print integers instead of enum names.

  • descriptor_pool – A Descriptor Pool for resolving types. If None use the default.

  • float_precision – If set, use this to specify float field valid digits.

  • ensure_ascii – If True, strings with non-ASCII characters are escaped. If False, Unicode strings are returned unchanged.

Returns:

A string containing the JSON formatted protocol buffer message.

google.protobuf.json_format.Parse(text, message, ignore_unknown_fields=False, descriptor_pool=None, max_recursion_depth=100)

Parses a JSON representation of a protocol message into a message.

Parameters:
  • text – Message JSON representation.

  • 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.

Raises::

ParseError: On JSON parsing problems.

google.protobuf.json_format.ParseDict(js_dict, message, ignore_unknown_fields=False, descriptor_pool=None, max_recursion_depth=100)

Parses a JSON dictionary representation into a message.

Parameters:
  • 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.

exception google.protobuf.json_format.ParseError

Thrown in case of parsing error.

args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

exception google.protobuf.json_format.SerializeToJsonError

Thrown if serialization to JSON fails.

args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.