Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1 | # Protocol Buffers - Google's data interchange format |
| 2 | # Copyright 2008 Google Inc. All rights reserved. |
| 3 | # https://developers.google.com/protocol-buffers/ |
| 4 | # |
| 5 | # Redistribution and use in source and binary forms, with or without |
| 6 | # modification, are permitted provided that the following conditions are |
| 7 | # met: |
| 8 | # |
| 9 | # * Redistributions of source code must retain the above copyright |
| 10 | # notice, this list of conditions and the following disclaimer. |
| 11 | # * Redistributions in binary form must reproduce the above |
| 12 | # copyright notice, this list of conditions and the following disclaimer |
| 13 | # in the documentation and/or other materials provided with the |
| 14 | # distribution. |
| 15 | # * Neither the name of Google Inc. nor the names of its |
| 16 | # contributors may be used to endorse or promote products derived from |
| 17 | # this software without specific prior written permission. |
| 18 | # |
| 19 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | |
| 31 | """Contains routines for printing protocol messages in JSON format. |
| 32 | |
| 33 | Simple usage example: |
| 34 | |
| 35 | # Create a proto object and serialize it to a json format string. |
| 36 | message = my_proto_pb2.MyMessage(foo='bar') |
| 37 | json_string = json_format.MessageToJson(message) |
| 38 | |
| 39 | # Parse a json format string to proto object. |
| 40 | message = json_format.Parse(json_string, my_proto_pb2.MyMessage()) |
| 41 | """ |
| 42 | |
| 43 | __author__ = 'jieluo@google.com (Jie Luo)' |
| 44 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 45 | # pylint: disable=g-statement-before-imports,g-import-not-at-top |
| 46 | try: |
| 47 | from collections import OrderedDict |
| 48 | except ImportError: |
| 49 | from ordereddict import OrderedDict # PY26 |
| 50 | # pylint: enable=g-statement-before-imports,g-import-not-at-top |
| 51 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 52 | import base64 |
| 53 | import json |
| 54 | import math |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 55 | |
| 56 | from operator import methodcaller |
| 57 | |
| 58 | import re |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 59 | import sys |
| 60 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 61 | import six |
| 62 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 63 | from google.protobuf import descriptor |
| 64 | from google.protobuf import symbol_database |
| 65 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 66 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 67 | _TIMESTAMPFOMAT = '%Y-%m-%dT%H:%M:%S' |
| 68 | _INT_TYPES = frozenset([descriptor.FieldDescriptor.CPPTYPE_INT32, |
| 69 | descriptor.FieldDescriptor.CPPTYPE_UINT32, |
| 70 | descriptor.FieldDescriptor.CPPTYPE_INT64, |
| 71 | descriptor.FieldDescriptor.CPPTYPE_UINT64]) |
| 72 | _INT64_TYPES = frozenset([descriptor.FieldDescriptor.CPPTYPE_INT64, |
| 73 | descriptor.FieldDescriptor.CPPTYPE_UINT64]) |
| 74 | _FLOAT_TYPES = frozenset([descriptor.FieldDescriptor.CPPTYPE_FLOAT, |
| 75 | descriptor.FieldDescriptor.CPPTYPE_DOUBLE]) |
| 76 | _INFINITY = 'Infinity' |
| 77 | _NEG_INFINITY = '-Infinity' |
| 78 | _NAN = 'NaN' |
| 79 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 80 | _UNPAIRED_SURROGATE_PATTERN = re.compile(six.u( |
| 81 | r'[\ud800-\udbff](?![\udc00-\udfff])|(?<![\ud800-\udbff])[\udc00-\udfff]' |
| 82 | )) |
| 83 | |
| 84 | _VALID_EXTENSION_NAME = re.compile(r'\[[a-zA-Z0-9\._]*\]$') |
| 85 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 86 | |
| 87 | class Error(Exception): |
| 88 | """Top-level module error for json_format.""" |
| 89 | |
| 90 | |
| 91 | class SerializeToJsonError(Error): |
| 92 | """Thrown if serialization to JSON fails.""" |
| 93 | |
| 94 | |
| 95 | class ParseError(Error): |
| 96 | """Thrown in case of parsing error.""" |
| 97 | |
| 98 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 99 | def MessageToJson(message, |
| 100 | including_default_value_fields=False, |
| 101 | preserving_proto_field_name=False, |
| 102 | indent=2, |
| 103 | sort_keys=False, |
| 104 | use_integers_for_enums=False): |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 105 | """Converts protobuf message to JSON format. |
| 106 | |
| 107 | Args: |
| 108 | message: The protocol buffers message instance to serialize. |
| 109 | including_default_value_fields: If True, singular primitive fields, |
| 110 | repeated fields, and map fields will always be serialized. If |
| 111 | False, only serialize non-empty fields. Singular message fields |
| 112 | and oneof fields are not affected by this option. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 113 | preserving_proto_field_name: If True, use the original proto field |
| 114 | names as defined in the .proto file. If False, convert the field |
| 115 | names to lowerCamelCase. |
| 116 | indent: The JSON object will be pretty-printed with this indent level. |
| 117 | An indent level of 0 or negative will only insert newlines. |
| 118 | sort_keys: If True, then the output will be sorted by field names. |
| 119 | use_integers_for_enums: If true, print integers instead of enum names. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 120 | |
| 121 | Returns: |
| 122 | A string containing the JSON formatted protocol buffer message. |
| 123 | """ |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 124 | printer = _Printer(including_default_value_fields, |
| 125 | preserving_proto_field_name, |
| 126 | use_integers_for_enums) |
| 127 | return printer.ToJsonString(message, indent, sort_keys) |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 128 | |
| 129 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 130 | def MessageToDict(message, |
| 131 | including_default_value_fields=False, |
| 132 | preserving_proto_field_name=False, |
| 133 | use_integers_for_enums=False): |
| 134 | """Converts protobuf message to a dictionary. |
| 135 | |
| 136 | When the dictionary is encoded to JSON, it conforms to proto3 JSON spec. |
| 137 | |
| 138 | Args: |
| 139 | message: The protocol buffers message instance to serialize. |
| 140 | including_default_value_fields: If True, singular primitive fields, |
| 141 | repeated fields, and map fields will always be serialized. If |
| 142 | False, only serialize non-empty fields. Singular message fields |
| 143 | and oneof fields are not affected by this option. |
| 144 | preserving_proto_field_name: If True, use the original proto field |
| 145 | names as defined in the .proto file. If False, convert the field |
| 146 | names to lowerCamelCase. |
| 147 | use_integers_for_enums: If true, print integers instead of enum names. |
| 148 | |
| 149 | Returns: |
| 150 | A dict representation of the protocol buffer message. |
| 151 | """ |
| 152 | printer = _Printer(including_default_value_fields, |
| 153 | preserving_proto_field_name, |
| 154 | use_integers_for_enums) |
| 155 | # pylint: disable=protected-access |
| 156 | return printer._MessageToJsonObject(message) |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 157 | |
| 158 | |
| 159 | def _IsMapEntry(field): |
| 160 | return (field.type == descriptor.FieldDescriptor.TYPE_MESSAGE and |
| 161 | field.message_type.has_options and |
| 162 | field.message_type.GetOptions().map_entry) |
| 163 | |
| 164 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 165 | class _Printer(object): |
| 166 | """JSON format printer for protocol message.""" |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 167 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 168 | def __init__(self, |
| 169 | including_default_value_fields=False, |
| 170 | preserving_proto_field_name=False, |
| 171 | use_integers_for_enums=False): |
| 172 | self.including_default_value_fields = including_default_value_fields |
| 173 | self.preserving_proto_field_name = preserving_proto_field_name |
| 174 | self.use_integers_for_enums = use_integers_for_enums |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 175 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 176 | def ToJsonString(self, message, indent, sort_keys): |
| 177 | js = self._MessageToJsonObject(message) |
| 178 | return json.dumps(js, indent=indent, sort_keys=sort_keys) |
| 179 | |
| 180 | def _MessageToJsonObject(self, message): |
| 181 | """Converts message to an object according to Proto3 JSON Specification.""" |
| 182 | message_descriptor = message.DESCRIPTOR |
| 183 | full_name = message_descriptor.full_name |
| 184 | if _IsWrapperMessage(message_descriptor): |
| 185 | return self._WrapperMessageToJsonObject(message) |
| 186 | if full_name in _WKTJSONMETHODS: |
| 187 | return methodcaller(_WKTJSONMETHODS[full_name][0], message)(self) |
| 188 | js = {} |
| 189 | return self._RegularMessageToJsonObject(message, js) |
| 190 | |
| 191 | def _RegularMessageToJsonObject(self, message, js): |
| 192 | """Converts normal message according to Proto3 JSON Specification.""" |
| 193 | fields = message.ListFields() |
| 194 | |
| 195 | try: |
| 196 | for field, value in fields: |
| 197 | if self.preserving_proto_field_name: |
| 198 | name = field.name |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 199 | else: |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 200 | name = field.json_name |
| 201 | if _IsMapEntry(field): |
| 202 | # Convert a map field. |
| 203 | v_field = field.message_type.fields_by_name['value'] |
| 204 | js_map = {} |
| 205 | for key in value: |
| 206 | if isinstance(key, bool): |
| 207 | if key: |
| 208 | recorded_key = 'true' |
| 209 | else: |
| 210 | recorded_key = 'false' |
| 211 | else: |
| 212 | recorded_key = key |
| 213 | js_map[recorded_key] = self._FieldToJsonObject( |
| 214 | v_field, value[key]) |
| 215 | js[name] = js_map |
| 216 | elif field.label == descriptor.FieldDescriptor.LABEL_REPEATED: |
| 217 | # Convert a repeated field. |
| 218 | js[name] = [self._FieldToJsonObject(field, k) |
| 219 | for k in value] |
| 220 | elif field.is_extension: |
| 221 | f = field |
| 222 | if (f.containing_type.GetOptions().message_set_wire_format and |
| 223 | f.type == descriptor.FieldDescriptor.TYPE_MESSAGE and |
| 224 | f.label == descriptor.FieldDescriptor.LABEL_OPTIONAL): |
| 225 | f = f.message_type |
| 226 | name = '[%s.%s]' % (f.full_name, name) |
| 227 | js[name] = self._FieldToJsonObject(field, value) |
| 228 | else: |
| 229 | js[name] = self._FieldToJsonObject(field, value) |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 230 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 231 | # Serialize default value if including_default_value_fields is True. |
| 232 | if self.including_default_value_fields: |
| 233 | message_descriptor = message.DESCRIPTOR |
| 234 | for field in message_descriptor.fields: |
| 235 | # Singular message fields and oneof fields will not be affected. |
| 236 | if ((field.label != descriptor.FieldDescriptor.LABEL_REPEATED and |
| 237 | field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_MESSAGE) or |
| 238 | field.containing_oneof): |
| 239 | continue |
| 240 | if self.preserving_proto_field_name: |
| 241 | name = field.name |
| 242 | else: |
| 243 | name = field.json_name |
| 244 | if name in js: |
| 245 | # Skip the field which has been serailized already. |
| 246 | continue |
| 247 | if _IsMapEntry(field): |
| 248 | js[name] = {} |
| 249 | elif field.label == descriptor.FieldDescriptor.LABEL_REPEATED: |
| 250 | js[name] = [] |
| 251 | else: |
| 252 | js[name] = self._FieldToJsonObject(field, field.default_value) |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 253 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 254 | except ValueError as e: |
| 255 | raise SerializeToJsonError( |
| 256 | 'Failed to serialize {0} field: {1}.'.format(field.name, e)) |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 257 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 258 | return js |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 259 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 260 | def _FieldToJsonObject(self, field, value): |
| 261 | """Converts field value according to Proto3 JSON Specification.""" |
| 262 | if field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_MESSAGE: |
| 263 | return self._MessageToJsonObject(value) |
| 264 | elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_ENUM: |
| 265 | if self.use_integers_for_enums: |
| 266 | return value |
| 267 | enum_value = field.enum_type.values_by_number.get(value, None) |
| 268 | if enum_value is not None: |
| 269 | return enum_value.name |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 270 | else: |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 271 | if field.file.syntax == 'proto3': |
| 272 | return value |
| 273 | raise SerializeToJsonError('Enum field contains an integer value ' |
| 274 | 'which can not mapped to an enum value.') |
| 275 | elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_STRING: |
| 276 | if field.type == descriptor.FieldDescriptor.TYPE_BYTES: |
| 277 | # Use base64 Data encoding for bytes |
| 278 | return base64.b64encode(value).decode('utf-8') |
| 279 | else: |
| 280 | return value |
| 281 | elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_BOOL: |
| 282 | return bool(value) |
| 283 | elif field.cpp_type in _INT64_TYPES: |
| 284 | return str(value) |
| 285 | elif field.cpp_type in _FLOAT_TYPES: |
| 286 | if math.isinf(value): |
| 287 | if value < 0.0: |
| 288 | return _NEG_INFINITY |
| 289 | else: |
| 290 | return _INFINITY |
| 291 | if math.isnan(value): |
| 292 | return _NAN |
| 293 | return value |
| 294 | |
| 295 | def _AnyMessageToJsonObject(self, message): |
| 296 | """Converts Any message according to Proto3 JSON Specification.""" |
| 297 | if not message.ListFields(): |
| 298 | return {} |
| 299 | # Must print @type first, use OrderedDict instead of {} |
| 300 | js = OrderedDict() |
| 301 | type_url = message.type_url |
| 302 | js['@type'] = type_url |
| 303 | sub_message = _CreateMessageFromTypeUrl(type_url) |
| 304 | sub_message.ParseFromString(message.value) |
| 305 | message_descriptor = sub_message.DESCRIPTOR |
| 306 | full_name = message_descriptor.full_name |
| 307 | if _IsWrapperMessage(message_descriptor): |
| 308 | js['value'] = self._WrapperMessageToJsonObject(sub_message) |
| 309 | return js |
| 310 | if full_name in _WKTJSONMETHODS: |
| 311 | js['value'] = methodcaller(_WKTJSONMETHODS[full_name][0], |
| 312 | sub_message)(self) |
| 313 | return js |
| 314 | return self._RegularMessageToJsonObject(sub_message, js) |
| 315 | |
| 316 | def _GenericMessageToJsonObject(self, message): |
| 317 | """Converts message according to Proto3 JSON Specification.""" |
| 318 | # Duration, Timestamp and FieldMask have ToJsonString method to do the |
| 319 | # convert. Users can also call the method directly. |
| 320 | return message.ToJsonString() |
| 321 | |
| 322 | def _ValueMessageToJsonObject(self, message): |
| 323 | """Converts Value message according to Proto3 JSON Specification.""" |
| 324 | which = message.WhichOneof('kind') |
| 325 | # If the Value message is not set treat as null_value when serialize |
| 326 | # to JSON. The parse back result will be different from original message. |
| 327 | if which is None or which == 'null_value': |
| 328 | return None |
| 329 | if which == 'list_value': |
| 330 | return self._ListValueMessageToJsonObject(message.list_value) |
| 331 | if which == 'struct_value': |
| 332 | value = message.struct_value |
| 333 | else: |
| 334 | value = getattr(message, which) |
| 335 | oneof_descriptor = message.DESCRIPTOR.fields_by_name[which] |
| 336 | return self._FieldToJsonObject(oneof_descriptor, value) |
| 337 | |
| 338 | def _ListValueMessageToJsonObject(self, message): |
| 339 | """Converts ListValue message according to Proto3 JSON Specification.""" |
| 340 | return [self._ValueMessageToJsonObject(value) |
| 341 | for value in message.values] |
| 342 | |
| 343 | def _StructMessageToJsonObject(self, message): |
| 344 | """Converts Struct message according to Proto3 JSON Specification.""" |
| 345 | fields = message.fields |
| 346 | ret = {} |
| 347 | for key in fields: |
| 348 | ret[key] = self._ValueMessageToJsonObject(fields[key]) |
| 349 | return ret |
| 350 | |
| 351 | def _WrapperMessageToJsonObject(self, message): |
| 352 | return self._FieldToJsonObject( |
| 353 | message.DESCRIPTOR.fields_by_name['value'], message.value) |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 354 | |
| 355 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 356 | def _IsWrapperMessage(message_descriptor): |
| 357 | return message_descriptor.file.name == 'google/protobuf/wrappers.proto' |
| 358 | |
| 359 | |
| 360 | def _DuplicateChecker(js): |
| 361 | result = {} |
| 362 | for name, value in js: |
| 363 | if name in result: |
| 364 | raise ParseError('Failed to load JSON: duplicate key {0}.'.format(name)) |
| 365 | result[name] = value |
| 366 | return result |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 367 | |
| 368 | |
| 369 | def _CreateMessageFromTypeUrl(type_url): |
| 370 | # TODO(jieluo): Should add a way that users can register the type resolver |
| 371 | # instead of the default one. |
| 372 | db = symbol_database.Default() |
| 373 | type_name = type_url.split('/')[-1] |
| 374 | try: |
| 375 | message_descriptor = db.pool.FindMessageTypeByName(type_name) |
| 376 | except KeyError: |
| 377 | raise TypeError( |
| 378 | 'Can not find message descriptor by type_url: {0}.'.format(type_url)) |
| 379 | message_class = db.GetPrototype(message_descriptor) |
| 380 | return message_class() |
| 381 | |
| 382 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 383 | def Parse(text, message, ignore_unknown_fields=False): |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 384 | """Parses a JSON representation of a protocol message into a message. |
| 385 | |
| 386 | Args: |
| 387 | text: Message JSON representation. |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 388 | message: A protocol buffer message to merge into. |
| 389 | ignore_unknown_fields: If True, do not raise errors for unknown fields. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 390 | |
| 391 | Returns: |
| 392 | The same message passed as argument. |
| 393 | |
| 394 | Raises:: |
| 395 | ParseError: On JSON parsing problems. |
| 396 | """ |
| 397 | if not isinstance(text, six.text_type): text = text.decode('utf-8') |
| 398 | try: |
| 399 | if sys.version_info < (2, 7): |
| 400 | # object_pair_hook is not supported before python2.7 |
| 401 | js = json.loads(text) |
| 402 | else: |
| 403 | js = json.loads(text, object_pairs_hook=_DuplicateChecker) |
| 404 | except ValueError as e: |
| 405 | raise ParseError('Failed to load JSON: {0}.'.format(str(e))) |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 406 | return ParseDict(js, message, ignore_unknown_fields) |
| 407 | |
| 408 | |
| 409 | def ParseDict(js_dict, message, ignore_unknown_fields=False): |
| 410 | """Parses a JSON dictionary representation into a message. |
| 411 | |
| 412 | Args: |
| 413 | js_dict: Dict representation of a JSON message. |
| 414 | message: A protocol buffer message to merge into. |
| 415 | ignore_unknown_fields: If True, do not raise errors for unknown fields. |
| 416 | |
| 417 | Returns: |
| 418 | The same message passed as argument. |
| 419 | """ |
| 420 | parser = _Parser(ignore_unknown_fields) |
| 421 | parser.ConvertMessage(js_dict, message) |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 422 | return message |
| 423 | |
| 424 | |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 425 | _INT_OR_FLOAT = six.integer_types + (float,) |
| 426 | |
| 427 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 428 | class _Parser(object): |
| 429 | """JSON format parser for protocol message.""" |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 430 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 431 | def __init__(self, |
| 432 | ignore_unknown_fields): |
| 433 | self.ignore_unknown_fields = ignore_unknown_fields |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 434 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 435 | def ConvertMessage(self, value, message): |
| 436 | """Convert a JSON object into a message. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 437 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 438 | Args: |
| 439 | value: A JSON object. |
| 440 | message: A WKT or regular protocol message to record the data. |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 441 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 442 | Raises: |
| 443 | ParseError: In case of convert problems. |
| 444 | """ |
| 445 | message_descriptor = message.DESCRIPTOR |
| 446 | full_name = message_descriptor.full_name |
| 447 | if _IsWrapperMessage(message_descriptor): |
| 448 | self._ConvertWrapperMessage(value, message) |
| 449 | elif full_name in _WKTJSONMETHODS: |
| 450 | methodcaller(_WKTJSONMETHODS[full_name][1], value, message)(self) |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 451 | else: |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 452 | self._ConvertFieldValuePair(value, message) |
| 453 | |
| 454 | def _ConvertFieldValuePair(self, js, message): |
| 455 | """Convert field value pairs into regular message. |
| 456 | |
| 457 | Args: |
| 458 | js: A JSON object to convert the field value pairs. |
| 459 | message: A regular protocol message to record the data. |
| 460 | |
| 461 | Raises: |
| 462 | ParseError: In case of problems converting. |
| 463 | """ |
| 464 | names = [] |
| 465 | message_descriptor = message.DESCRIPTOR |
| 466 | fields_by_json_name = dict((f.json_name, f) |
| 467 | for f in message_descriptor.fields) |
| 468 | for name in js: |
| 469 | try: |
| 470 | field = fields_by_json_name.get(name, None) |
| 471 | if not field: |
| 472 | field = message_descriptor.fields_by_name.get(name, None) |
| 473 | if not field and _VALID_EXTENSION_NAME.match(name): |
| 474 | if not message_descriptor.is_extendable: |
| 475 | raise ParseError('Message type {0} does not have extensions'.format( |
| 476 | message_descriptor.full_name)) |
| 477 | identifier = name[1:-1] # strip [] brackets |
| 478 | identifier = '.'.join(identifier.split('.')[:-1]) |
| 479 | # pylint: disable=protected-access |
| 480 | field = message.Extensions._FindExtensionByName(identifier) |
| 481 | # pylint: enable=protected-access |
| 482 | if not field: |
| 483 | if self.ignore_unknown_fields: |
| 484 | continue |
| 485 | raise ParseError( |
| 486 | ('Message type "{0}" has no field named "{1}".\n' |
| 487 | ' Available Fields(except extensions): {2}').format( |
| 488 | message_descriptor.full_name, name, |
| 489 | message_descriptor.fields)) |
| 490 | if name in names: |
| 491 | raise ParseError('Message type "{0}" should not have multiple ' |
| 492 | '"{1}" fields.'.format( |
| 493 | message.DESCRIPTOR.full_name, name)) |
| 494 | names.append(name) |
| 495 | # Check no other oneof field is parsed. |
| 496 | if field.containing_oneof is not None: |
| 497 | oneof_name = field.containing_oneof.name |
| 498 | if oneof_name in names: |
| 499 | raise ParseError('Message type "{0}" should not have multiple ' |
| 500 | '"{1}" oneof fields.'.format( |
| 501 | message.DESCRIPTOR.full_name, oneof_name)) |
| 502 | names.append(oneof_name) |
| 503 | |
| 504 | value = js[name] |
| 505 | if value is None: |
| 506 | if (field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_MESSAGE |
| 507 | and field.message_type.full_name == 'google.protobuf.Value'): |
| 508 | sub_message = getattr(message, field.name) |
| 509 | sub_message.null_value = 0 |
| 510 | else: |
| 511 | message.ClearField(field.name) |
| 512 | continue |
| 513 | |
| 514 | # Parse field value. |
| 515 | if _IsMapEntry(field): |
| 516 | message.ClearField(field.name) |
| 517 | self._ConvertMapFieldValue(value, message, field) |
| 518 | elif field.label == descriptor.FieldDescriptor.LABEL_REPEATED: |
| 519 | message.ClearField(field.name) |
| 520 | if not isinstance(value, list): |
| 521 | raise ParseError('repeated field {0} must be in [] which is ' |
| 522 | '{1}.'.format(name, value)) |
| 523 | if field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_MESSAGE: |
| 524 | # Repeated message field. |
| 525 | for item in value: |
| 526 | sub_message = getattr(message, field.name).add() |
| 527 | # None is a null_value in Value. |
| 528 | if (item is None and |
| 529 | sub_message.DESCRIPTOR.full_name != 'google.protobuf.Value'): |
| 530 | raise ParseError('null is not allowed to be used as an element' |
| 531 | ' in a repeated field.') |
| 532 | self.ConvertMessage(item, sub_message) |
| 533 | else: |
| 534 | # Repeated scalar field. |
| 535 | for item in value: |
| 536 | if item is None: |
| 537 | raise ParseError('null is not allowed to be used as an element' |
| 538 | ' in a repeated field.') |
| 539 | getattr(message, field.name).append( |
| 540 | _ConvertScalarFieldValue(item, field)) |
| 541 | elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_MESSAGE: |
| 542 | if field.is_extension: |
| 543 | sub_message = message.Extensions[field] |
| 544 | else: |
| 545 | sub_message = getattr(message, field.name) |
| 546 | sub_message.SetInParent() |
| 547 | self.ConvertMessage(value, sub_message) |
| 548 | else: |
| 549 | setattr(message, field.name, _ConvertScalarFieldValue(value, field)) |
| 550 | except ParseError as e: |
| 551 | if field and field.containing_oneof is None: |
| 552 | raise ParseError('Failed to parse {0} field: {1}'.format(name, e)) |
| 553 | else: |
| 554 | raise ParseError(str(e)) |
| 555 | except ValueError as e: |
| 556 | raise ParseError('Failed to parse {0} field: {1}.'.format(name, e)) |
| 557 | except TypeError as e: |
| 558 | raise ParseError('Failed to parse {0} field: {1}.'.format(name, e)) |
| 559 | |
| 560 | def _ConvertAnyMessage(self, value, message): |
| 561 | """Convert a JSON representation into Any message.""" |
| 562 | if isinstance(value, dict) and not value: |
| 563 | return |
| 564 | try: |
| 565 | type_url = value['@type'] |
| 566 | except KeyError: |
| 567 | raise ParseError('@type is missing when parsing any message.') |
| 568 | |
| 569 | sub_message = _CreateMessageFromTypeUrl(type_url) |
| 570 | message_descriptor = sub_message.DESCRIPTOR |
| 571 | full_name = message_descriptor.full_name |
| 572 | if _IsWrapperMessage(message_descriptor): |
| 573 | self._ConvertWrapperMessage(value['value'], sub_message) |
| 574 | elif full_name in _WKTJSONMETHODS: |
| 575 | methodcaller( |
| 576 | _WKTJSONMETHODS[full_name][1], value['value'], sub_message)(self) |
| 577 | else: |
| 578 | del value['@type'] |
| 579 | self._ConvertFieldValuePair(value, sub_message) |
| 580 | # Sets Any message |
| 581 | message.value = sub_message.SerializeToString() |
| 582 | message.type_url = type_url |
| 583 | |
| 584 | def _ConvertGenericMessage(self, value, message): |
| 585 | """Convert a JSON representation into message with FromJsonString.""" |
| 586 | # Duration, Timestamp, FieldMask have a FromJsonString method to do the |
| 587 | # conversion. Users can also call the method directly. |
| 588 | message.FromJsonString(value) |
| 589 | |
| 590 | def _ConvertValueMessage(self, value, message): |
| 591 | """Convert a JSON representation into Value message.""" |
| 592 | if isinstance(value, dict): |
| 593 | self._ConvertStructMessage(value, message.struct_value) |
| 594 | elif isinstance(value, list): |
| 595 | self. _ConvertListValueMessage(value, message.list_value) |
| 596 | elif value is None: |
| 597 | message.null_value = 0 |
| 598 | elif isinstance(value, bool): |
| 599 | message.bool_value = value |
| 600 | elif isinstance(value, six.string_types): |
| 601 | message.string_value = value |
| 602 | elif isinstance(value, _INT_OR_FLOAT): |
| 603 | message.number_value = value |
| 604 | else: |
| 605 | raise ParseError('Unexpected type for Value message.') |
| 606 | |
| 607 | def _ConvertListValueMessage(self, value, message): |
| 608 | """Convert a JSON representation into ListValue message.""" |
| 609 | if not isinstance(value, list): |
| 610 | raise ParseError( |
| 611 | 'ListValue must be in [] which is {0}.'.format(value)) |
| 612 | message.ClearField('values') |
| 613 | for item in value: |
| 614 | self._ConvertValueMessage(item, message.values.add()) |
| 615 | |
| 616 | def _ConvertStructMessage(self, value, message): |
| 617 | """Convert a JSON representation into Struct message.""" |
| 618 | if not isinstance(value, dict): |
| 619 | raise ParseError( |
| 620 | 'Struct must be in a dict which is {0}.'.format(value)) |
| 621 | for key in value: |
| 622 | self._ConvertValueMessage(value[key], message.fields[key]) |
| 623 | return |
| 624 | |
| 625 | def _ConvertWrapperMessage(self, value, message): |
| 626 | """Convert a JSON representation into Wrapper message.""" |
| 627 | field = message.DESCRIPTOR.fields_by_name['value'] |
| 628 | setattr(message, 'value', _ConvertScalarFieldValue(value, field)) |
| 629 | |
| 630 | def _ConvertMapFieldValue(self, value, message, field): |
| 631 | """Convert map field value for a message map field. |
| 632 | |
| 633 | Args: |
| 634 | value: A JSON object to convert the map field value. |
| 635 | message: A protocol message to record the converted data. |
| 636 | field: The descriptor of the map field to be converted. |
| 637 | |
| 638 | Raises: |
| 639 | ParseError: In case of convert problems. |
| 640 | """ |
| 641 | if not isinstance(value, dict): |
| 642 | raise ParseError( |
| 643 | 'Map field {0} must be in a dict which is {1}.'.format( |
| 644 | field.name, value)) |
| 645 | key_field = field.message_type.fields_by_name['key'] |
| 646 | value_field = field.message_type.fields_by_name['value'] |
| 647 | for key in value: |
| 648 | key_value = _ConvertScalarFieldValue(key, key_field, True) |
| 649 | if value_field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_MESSAGE: |
| 650 | self.ConvertMessage(value[key], getattr( |
| 651 | message, field.name)[key_value]) |
| 652 | else: |
| 653 | getattr(message, field.name)[key_value] = _ConvertScalarFieldValue( |
| 654 | value[key], value_field) |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 655 | |
| 656 | |
| 657 | def _ConvertScalarFieldValue(value, field, require_str=False): |
| 658 | """Convert a single scalar field value. |
| 659 | |
| 660 | Args: |
| 661 | value: A scalar value to convert the scalar field value. |
| 662 | field: The descriptor of the field to convert. |
| 663 | require_str: If True, the field value must be a str. |
| 664 | |
| 665 | Returns: |
| 666 | The converted scalar field value |
| 667 | |
| 668 | Raises: |
| 669 | ParseError: In case of convert problems. |
| 670 | """ |
| 671 | if field.cpp_type in _INT_TYPES: |
| 672 | return _ConvertInteger(value) |
| 673 | elif field.cpp_type in _FLOAT_TYPES: |
| 674 | return _ConvertFloat(value) |
| 675 | elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_BOOL: |
| 676 | return _ConvertBool(value, require_str) |
| 677 | elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_STRING: |
| 678 | if field.type == descriptor.FieldDescriptor.TYPE_BYTES: |
| 679 | return base64.b64decode(value) |
| 680 | else: |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 681 | # Checking for unpaired surrogates appears to be unreliable, |
| 682 | # depending on the specific Python version, so we check manually. |
| 683 | if _UNPAIRED_SURROGATE_PATTERN.search(value): |
| 684 | raise ParseError('Unpaired surrogate') |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 685 | return value |
| 686 | elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_ENUM: |
| 687 | # Convert an enum value. |
| 688 | enum_value = field.enum_type.values_by_name.get(value, None) |
| 689 | if enum_value is None: |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 690 | try: |
| 691 | number = int(value) |
| 692 | enum_value = field.enum_type.values_by_number.get(number, None) |
| 693 | except ValueError: |
| 694 | raise ParseError('Invalid enum value {0} for enum type {1}.'.format( |
| 695 | value, field.enum_type.full_name)) |
| 696 | if enum_value is None: |
| 697 | if field.file.syntax == 'proto3': |
| 698 | # Proto3 accepts unknown enums. |
| 699 | return number |
| 700 | raise ParseError('Invalid enum value {0} for enum type {1}.'.format( |
| 701 | value, field.enum_type.full_name)) |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 702 | return enum_value.number |
| 703 | |
| 704 | |
| 705 | def _ConvertInteger(value): |
| 706 | """Convert an integer. |
| 707 | |
| 708 | Args: |
| 709 | value: A scalar value to convert. |
| 710 | |
| 711 | Returns: |
| 712 | The integer value. |
| 713 | |
| 714 | Raises: |
| 715 | ParseError: If an integer couldn't be consumed. |
| 716 | """ |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 717 | if isinstance(value, float) and not value.is_integer(): |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 718 | raise ParseError('Couldn\'t parse integer: {0}.'.format(value)) |
| 719 | |
| 720 | if isinstance(value, six.text_type) and value.find(' ') != -1: |
| 721 | raise ParseError('Couldn\'t parse integer: "{0}".'.format(value)) |
| 722 | |
| 723 | return int(value) |
| 724 | |
| 725 | |
| 726 | def _ConvertFloat(value): |
| 727 | """Convert an floating point number.""" |
| 728 | if value == 'nan': |
| 729 | raise ParseError('Couldn\'t parse float "nan", use "NaN" instead.') |
| 730 | try: |
| 731 | # Assume Python compatible syntax. |
| 732 | return float(value) |
| 733 | except ValueError: |
| 734 | # Check alternative spellings. |
| 735 | if value == _NEG_INFINITY: |
| 736 | return float('-inf') |
| 737 | elif value == _INFINITY: |
| 738 | return float('inf') |
| 739 | elif value == _NAN: |
| 740 | return float('nan') |
| 741 | else: |
| 742 | raise ParseError('Couldn\'t parse float: {0}.'.format(value)) |
| 743 | |
| 744 | |
| 745 | def _ConvertBool(value, require_str): |
| 746 | """Convert a boolean value. |
| 747 | |
| 748 | Args: |
| 749 | value: A scalar value to convert. |
| 750 | require_str: If True, value must be a str. |
| 751 | |
| 752 | Returns: |
| 753 | The bool parsed. |
| 754 | |
| 755 | Raises: |
| 756 | ParseError: If a boolean value couldn't be consumed. |
| 757 | """ |
| 758 | if require_str: |
| 759 | if value == 'true': |
| 760 | return True |
| 761 | elif value == 'false': |
| 762 | return False |
| 763 | else: |
| 764 | raise ParseError('Expected "true" or "false", not {0}.'.format(value)) |
| 765 | |
| 766 | if not isinstance(value, bool): |
| 767 | raise ParseError('Expected true or false without quotes.') |
| 768 | return value |
| 769 | |
| 770 | _WKTJSONMETHODS = { |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 771 | 'google.protobuf.Any': ['_AnyMessageToJsonObject', |
| 772 | '_ConvertAnyMessage'], |
| 773 | 'google.protobuf.Duration': ['_GenericMessageToJsonObject', |
| 774 | '_ConvertGenericMessage'], |
| 775 | 'google.protobuf.FieldMask': ['_GenericMessageToJsonObject', |
| 776 | '_ConvertGenericMessage'], |
| 777 | 'google.protobuf.ListValue': ['_ListValueMessageToJsonObject', |
| 778 | '_ConvertListValueMessage'], |
| 779 | 'google.protobuf.Struct': ['_StructMessageToJsonObject', |
| 780 | '_ConvertStructMessage'], |
| 781 | 'google.protobuf.Timestamp': ['_GenericMessageToJsonObject', |
| 782 | '_ConvertGenericMessage'], |
| 783 | 'google.protobuf.Value': ['_ValueMessageToJsonObject', |
| 784 | '_ConvertValueMessage'] |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 785 | } |