Skip to content

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())

Error

Bases: Exception

Top-level module error for json_format.

Source code in client/ayon_nuke/vendor/google/protobuf/json_format.py
78
79
class Error(Exception):
  """Top-level module error for json_format."""

ParseError

Bases: Error

Thrown in case of parsing error.

Source code in client/ayon_nuke/vendor/google/protobuf/json_format.py
86
87
class ParseError(Error):
  """Thrown in case of parsing error."""

SerializeToJsonError

Bases: Error

Thrown if serialization to JSON fails.

Source code in client/ayon_nuke/vendor/google/protobuf/json_format.py
82
83
class SerializeToJsonError(Error):
  """Thrown if serialization to JSON fails."""

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:

Name Type Description Default
message

The protocol buffers message instance to serialize.

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

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

False
use_integers_for_enums

If true, print integers instead of enum names.

False
descriptor_pool

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

None
float_precision

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

None

Returns:

Type Description

A dict representation of the protocol buffer message.

Source code in client/ayon_nuke/vendor/google/protobuf/json_format.py
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
def 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.

  Args:
    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.
  """
  printer = _Printer(
      including_default_value_fields,
      preserving_proto_field_name,
      use_integers_for_enums,
      descriptor_pool,
      float_precision=float_precision)
  # pylint: disable=protected-access
  return printer._MessageToJsonObject(message)

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:

Name Type Description Default
message

The protocol buffers message instance to serialize.

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

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

False
indent

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

2
sort_keys

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

False
use_integers_for_enums

If true, print integers instead of enum names.

False
descriptor_pool

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

None
float_precision

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

None
ensure_ascii

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

True

Returns:

Type Description

A string containing the JSON formatted protocol buffer message.

Source code in client/ayon_nuke/vendor/google/protobuf/json_format.py
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
def 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.

  Args:
    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.
  """
  printer = _Printer(
      including_default_value_fields,
      preserving_proto_field_name,
      use_integers_for_enums,
      descriptor_pool,
      float_precision=float_precision)
  return printer.ToJsonString(message, indent, sort_keys, ensure_ascii)

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:

Name Type Description Default
text

Message JSON representation.

required
message

A protocol buffer message to merge into.

required
ignore_unknown_fields

If True, do not raise errors for unknown fields.

False
descriptor_pool

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

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

100

Returns:

Type Description

The same message passed as argument.

Raises:: ParseError: On JSON parsing problems.

Source code in client/ayon_nuke/vendor/google/protobuf/json_format.py
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
def 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.

  Args:
    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.
  """
  if not isinstance(text, str):
    text = text.decode('utf-8')
  try:
    js = json.loads(text, object_pairs_hook=_DuplicateChecker)
  except ValueError as e:
    raise ParseError('Failed to load JSON: {0}.'.format(str(e)))
  return ParseDict(js, message, ignore_unknown_fields, descriptor_pool,
                   max_recursion_depth)

ParseDict(js_dict, message, ignore_unknown_fields=False, descriptor_pool=None, max_recursion_depth=100)

Parses a JSON dictionary representation into a message.

Parameters:

Name Type Description Default
js_dict

Dict representation of a JSON message.

required
message

A protocol buffer message to merge into.

required
ignore_unknown_fields

If True, do not raise errors for unknown fields.

False
descriptor_pool

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

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

100

Returns:

Type Description

The same message passed as argument.

Source code in client/ayon_nuke/vendor/google/protobuf/json_format.py
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
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.
  """
  parser = _Parser(ignore_unknown_fields, descriptor_pool, max_recursion_depth)
  parser.ConvertMessage(js_dict, message, '')
  return message