json_format.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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. """Contains routines for printing protocol messages in JSON format.
  31. Simple usage example:
  32. # Create a proto object and serialize it to a json format string.
  33. message = my_proto_pb2.MyMessage(foo='bar')
  34. json_string = json_format.MessageToJson(message)
  35. # Parse a json format string to proto object.
  36. message = json_format.Parse(json_string, my_proto_pb2.MyMessage())
  37. """
  38. __author__ = 'jieluo@google.com (Jie Luo)'
  39. try:
  40. from collections import OrderedDict
  41. except ImportError:
  42. from ordereddict import OrderedDict #PY26
  43. import base64
  44. import json
  45. import math
  46. import re
  47. import six
  48. import sys
  49. from operator import methodcaller
  50. from google.protobuf import descriptor
  51. from google.protobuf import symbol_database
  52. _TIMESTAMPFOMAT = '%Y-%m-%dT%H:%M:%S'
  53. _INT_TYPES = frozenset([descriptor.FieldDescriptor.CPPTYPE_INT32,
  54. descriptor.FieldDescriptor.CPPTYPE_UINT32,
  55. descriptor.FieldDescriptor.CPPTYPE_INT64,
  56. descriptor.FieldDescriptor.CPPTYPE_UINT64])
  57. _INT64_TYPES = frozenset([descriptor.FieldDescriptor.CPPTYPE_INT64,
  58. descriptor.FieldDescriptor.CPPTYPE_UINT64])
  59. _FLOAT_TYPES = frozenset([descriptor.FieldDescriptor.CPPTYPE_FLOAT,
  60. descriptor.FieldDescriptor.CPPTYPE_DOUBLE])
  61. _INFINITY = 'Infinity'
  62. _NEG_INFINITY = '-Infinity'
  63. _NAN = 'NaN'
  64. _UNPAIRED_SURROGATE_PATTERN = re.compile(six.u(
  65. r'[\ud800-\udbff](?![\udc00-\udfff])|(?<![\ud800-\udbff])[\udc00-\udfff]'
  66. ))
  67. class Error(Exception):
  68. """Top-level module error for json_format."""
  69. class SerializeToJsonError(Error):
  70. """Thrown if serialization to JSON fails."""
  71. class ParseError(Error):
  72. """Thrown in case of parsing error."""
  73. def MessageToJson(message,
  74. including_default_value_fields=False,
  75. preserving_proto_field_name=False):
  76. """Converts protobuf message to JSON format.
  77. Args:
  78. message: The protocol buffers message instance to serialize.
  79. including_default_value_fields: If True, singular primitive fields,
  80. repeated fields, and map fields will always be serialized. If
  81. False, only serialize non-empty fields. Singular message fields
  82. and oneof fields are not affected by this option.
  83. preserving_proto_field_name: If True, use the original proto field
  84. names as defined in the .proto file. If False, convert the field
  85. names to lowerCamelCase.
  86. Returns:
  87. A string containing the JSON formatted protocol buffer message.
  88. """
  89. printer = _Printer(including_default_value_fields,
  90. preserving_proto_field_name)
  91. return printer.ToJsonString(message)
  92. def MessageToDict(message,
  93. including_default_value_fields=False,
  94. preserving_proto_field_name=False):
  95. """Converts protobuf message to a JSON dictionary.
  96. Args:
  97. message: The protocol buffers message instance to serialize.
  98. including_default_value_fields: If True, singular primitive fields,
  99. repeated fields, and map fields will always be serialized. If
  100. False, only serialize non-empty fields. Singular message fields
  101. and oneof fields are not affected by this option.
  102. preserving_proto_field_name: If True, use the original proto field
  103. names as defined in the .proto file. If False, convert the field
  104. names to lowerCamelCase.
  105. Returns:
  106. A dict representation of the JSON formatted protocol buffer message.
  107. """
  108. printer = _Printer(including_default_value_fields,
  109. preserving_proto_field_name)
  110. # pylint: disable=protected-access
  111. return printer._MessageToJsonObject(message)
  112. def _IsMapEntry(field):
  113. return (field.type == descriptor.FieldDescriptor.TYPE_MESSAGE and
  114. field.message_type.has_options and
  115. field.message_type.GetOptions().map_entry)
  116. class _Printer(object):
  117. """JSON format printer for protocol message."""
  118. def __init__(self,
  119. including_default_value_fields=False,
  120. preserving_proto_field_name=False):
  121. self.including_default_value_fields = including_default_value_fields
  122. self.preserving_proto_field_name = preserving_proto_field_name
  123. def ToJsonString(self, message):
  124. js = self._MessageToJsonObject(message)
  125. return json.dumps(js, indent=2)
  126. def _MessageToJsonObject(self, message):
  127. """Converts message to an object according to Proto3 JSON Specification."""
  128. message_descriptor = message.DESCRIPTOR
  129. full_name = message_descriptor.full_name
  130. if _IsWrapperMessage(message_descriptor):
  131. return self._WrapperMessageToJsonObject(message)
  132. if full_name in _WKTJSONMETHODS:
  133. return methodcaller(_WKTJSONMETHODS[full_name][0], message)(self)
  134. js = {}
  135. return self._RegularMessageToJsonObject(message, js)
  136. def _RegularMessageToJsonObject(self, message, js):
  137. """Converts normal message according to Proto3 JSON Specification."""
  138. fields = message.ListFields()
  139. try:
  140. for field, value in fields:
  141. if self.preserving_proto_field_name:
  142. name = field.name
  143. else:
  144. name = field.json_name
  145. if _IsMapEntry(field):
  146. # Convert a map field.
  147. v_field = field.message_type.fields_by_name['value']
  148. js_map = {}
  149. for key in value:
  150. if isinstance(key, bool):
  151. if key:
  152. recorded_key = 'true'
  153. else:
  154. recorded_key = 'false'
  155. else:
  156. recorded_key = key
  157. js_map[recorded_key] = self._FieldToJsonObject(
  158. v_field, value[key])
  159. js[name] = js_map
  160. elif field.label == descriptor.FieldDescriptor.LABEL_REPEATED:
  161. # Convert a repeated field.
  162. js[name] = [self._FieldToJsonObject(field, k)
  163. for k in value]
  164. else:
  165. js[name] = self._FieldToJsonObject(field, value)
  166. # Serialize default value if including_default_value_fields is True.
  167. if self.including_default_value_fields:
  168. message_descriptor = message.DESCRIPTOR
  169. for field in message_descriptor.fields:
  170. # Singular message fields and oneof fields will not be affected.
  171. if ((field.label != descriptor.FieldDescriptor.LABEL_REPEATED and
  172. field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_MESSAGE) or
  173. field.containing_oneof):
  174. continue
  175. if self.preserving_proto_field_name:
  176. name = field.name
  177. else:
  178. name = field.json_name
  179. if name in js:
  180. # Skip the field which has been serailized already.
  181. continue
  182. if _IsMapEntry(field):
  183. js[name] = {}
  184. elif field.label == descriptor.FieldDescriptor.LABEL_REPEATED:
  185. js[name] = []
  186. else:
  187. js[name] = self._FieldToJsonObject(field, field.default_value)
  188. except ValueError as e:
  189. raise SerializeToJsonError(
  190. 'Failed to serialize {0} field: {1}.'.format(field.name, e))
  191. return js
  192. def _FieldToJsonObject(self, field, value):
  193. """Converts field value according to Proto3 JSON Specification."""
  194. if field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_MESSAGE:
  195. return self._MessageToJsonObject(value)
  196. elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_ENUM:
  197. enum_value = field.enum_type.values_by_number.get(value, None)
  198. if enum_value is not None:
  199. return enum_value.name
  200. else:
  201. raise SerializeToJsonError('Enum field contains an integer value '
  202. 'which can not mapped to an enum value.')
  203. elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_STRING:
  204. if field.type == descriptor.FieldDescriptor.TYPE_BYTES:
  205. # Use base64 Data encoding for bytes
  206. return base64.b64encode(value).decode('utf-8')
  207. else:
  208. return value
  209. elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_BOOL:
  210. return bool(value)
  211. elif field.cpp_type in _INT64_TYPES:
  212. return str(value)
  213. elif field.cpp_type in _FLOAT_TYPES:
  214. if math.isinf(value):
  215. if value < 0.0:
  216. return _NEG_INFINITY
  217. else:
  218. return _INFINITY
  219. if math.isnan(value):
  220. return _NAN
  221. return value
  222. def _AnyMessageToJsonObject(self, message):
  223. """Converts Any message according to Proto3 JSON Specification."""
  224. if not message.ListFields():
  225. return {}
  226. # Must print @type first, use OrderedDict instead of {}
  227. js = OrderedDict()
  228. type_url = message.type_url
  229. js['@type'] = type_url
  230. sub_message = _CreateMessageFromTypeUrl(type_url)
  231. sub_message.ParseFromString(message.value)
  232. message_descriptor = sub_message.DESCRIPTOR
  233. full_name = message_descriptor.full_name
  234. if _IsWrapperMessage(message_descriptor):
  235. js['value'] = self._WrapperMessageToJsonObject(sub_message)
  236. return js
  237. if full_name in _WKTJSONMETHODS:
  238. js['value'] = methodcaller(_WKTJSONMETHODS[full_name][0],
  239. sub_message)(self)
  240. return js
  241. return self._RegularMessageToJsonObject(sub_message, js)
  242. def _GenericMessageToJsonObject(self, message):
  243. """Converts message according to Proto3 JSON Specification."""
  244. # Duration, Timestamp and FieldMask have ToJsonString method to do the
  245. # convert. Users can also call the method directly.
  246. return message.ToJsonString()
  247. def _ValueMessageToJsonObject(self, message):
  248. """Converts Value message according to Proto3 JSON Specification."""
  249. which = message.WhichOneof('kind')
  250. # If the Value message is not set treat as null_value when serialize
  251. # to JSON. The parse back result will be different from original message.
  252. if which is None or which == 'null_value':
  253. return None
  254. if which == 'list_value':
  255. return self._ListValueMessageToJsonObject(message.list_value)
  256. if which == 'struct_value':
  257. value = message.struct_value
  258. else:
  259. value = getattr(message, which)
  260. oneof_descriptor = message.DESCRIPTOR.fields_by_name[which]
  261. return self._FieldToJsonObject(oneof_descriptor, value)
  262. def _ListValueMessageToJsonObject(self, message):
  263. """Converts ListValue message according to Proto3 JSON Specification."""
  264. return [self._ValueMessageToJsonObject(value)
  265. for value in message.values]
  266. def _StructMessageToJsonObject(self, message):
  267. """Converts Struct message according to Proto3 JSON Specification."""
  268. fields = message.fields
  269. ret = {}
  270. for key in fields:
  271. ret[key] = self._ValueMessageToJsonObject(fields[key])
  272. return ret
  273. def _WrapperMessageToJsonObject(self, message):
  274. return self._FieldToJsonObject(
  275. message.DESCRIPTOR.fields_by_name['value'], message.value)
  276. def _IsWrapperMessage(message_descriptor):
  277. return message_descriptor.file.name == 'google/protobuf/wrappers.proto'
  278. def _DuplicateChecker(js):
  279. result = {}
  280. for name, value in js:
  281. if name in result:
  282. raise ParseError('Failed to load JSON: duplicate key {0}.'.format(name))
  283. result[name] = value
  284. return result
  285. def _CreateMessageFromTypeUrl(type_url):
  286. # TODO(jieluo): Should add a way that users can register the type resolver
  287. # instead of the default one.
  288. db = symbol_database.Default()
  289. type_name = type_url.split('/')[-1]
  290. try:
  291. message_descriptor = db.pool.FindMessageTypeByName(type_name)
  292. except KeyError:
  293. raise TypeError(
  294. 'Can not find message descriptor by type_url: {0}.'.format(type_url))
  295. message_class = db.GetPrototype(message_descriptor)
  296. return message_class()
  297. def Parse(text, message, ignore_unknown_fields=False):
  298. """Parses a JSON representation of a protocol message into a message.
  299. Args:
  300. text: Message JSON representation.
  301. message: A protocol buffer message to merge into.
  302. ignore_unknown_fields: If True, do not raise errors for unknown fields.
  303. Returns:
  304. The same message passed as argument.
  305. Raises::
  306. ParseError: On JSON parsing problems.
  307. """
  308. if not isinstance(text, six.text_type): text = text.decode('utf-8')
  309. try:
  310. if sys.version_info < (2, 7):
  311. # object_pair_hook is not supported before python2.7
  312. js = json.loads(text)
  313. else:
  314. js = json.loads(text, object_pairs_hook=_DuplicateChecker)
  315. except ValueError as e:
  316. raise ParseError('Failed to load JSON: {0}.'.format(str(e)))
  317. return ParseDict(js, message, ignore_unknown_fields)
  318. def ParseDict(js_dict, message, ignore_unknown_fields=False):
  319. """Parses a JSON dictionary representation into a message.
  320. Args:
  321. js_dict: Dict representation of a JSON message.
  322. message: A protocol buffer message to merge into.
  323. ignore_unknown_fields: If True, do not raise errors for unknown fields.
  324. Returns:
  325. The same message passed as argument.
  326. """
  327. parser = _Parser(ignore_unknown_fields)
  328. parser.ConvertMessage(js_dict, message)
  329. return message
  330. _INT_OR_FLOAT = six.integer_types + (float,)
  331. class _Parser(object):
  332. """JSON format parser for protocol message."""
  333. def __init__(self,
  334. ignore_unknown_fields):
  335. self.ignore_unknown_fields = ignore_unknown_fields
  336. def ConvertMessage(self, value, message):
  337. """Convert a JSON object into a message.
  338. Args:
  339. value: A JSON object.
  340. message: A WKT or regular protocol message to record the data.
  341. Raises:
  342. ParseError: In case of convert problems.
  343. """
  344. message_descriptor = message.DESCRIPTOR
  345. full_name = message_descriptor.full_name
  346. if _IsWrapperMessage(message_descriptor):
  347. self._ConvertWrapperMessage(value, message)
  348. elif full_name in _WKTJSONMETHODS:
  349. methodcaller(_WKTJSONMETHODS[full_name][1], value, message)(self)
  350. else:
  351. self._ConvertFieldValuePair(value, message)
  352. def _ConvertFieldValuePair(self, js, message):
  353. """Convert field value pairs into regular message.
  354. Args:
  355. js: A JSON object to convert the field value pairs.
  356. message: A regular protocol message to record the data.
  357. Raises:
  358. ParseError: In case of problems converting.
  359. """
  360. names = []
  361. message_descriptor = message.DESCRIPTOR
  362. fields_by_json_name = dict((f.json_name, f)
  363. for f in message_descriptor.fields)
  364. for name in js:
  365. try:
  366. field = fields_by_json_name.get(name, None)
  367. if not field:
  368. field = message_descriptor.fields_by_name.get(name, None)
  369. if not field:
  370. if self.ignore_unknown_fields:
  371. continue
  372. raise ParseError(
  373. 'Message type "{0}" has no field named "{1}".'.format(
  374. message_descriptor.full_name, name))
  375. if name in names:
  376. raise ParseError('Message type "{0}" should not have multiple '
  377. '"{1}" fields.'.format(
  378. message.DESCRIPTOR.full_name, name))
  379. names.append(name)
  380. # Check no other oneof field is parsed.
  381. if field.containing_oneof is not None:
  382. oneof_name = field.containing_oneof.name
  383. if oneof_name in names:
  384. raise ParseError('Message type "{0}" should not have multiple '
  385. '"{1}" oneof fields.'.format(
  386. message.DESCRIPTOR.full_name, oneof_name))
  387. names.append(oneof_name)
  388. value = js[name]
  389. if value is None:
  390. if (field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_MESSAGE
  391. and field.message_type.full_name == 'google.protobuf.Value'):
  392. sub_message = getattr(message, field.name)
  393. sub_message.null_value = 0
  394. else:
  395. message.ClearField(field.name)
  396. continue
  397. # Parse field value.
  398. if _IsMapEntry(field):
  399. message.ClearField(field.name)
  400. self._ConvertMapFieldValue(value, message, field)
  401. elif field.label == descriptor.FieldDescriptor.LABEL_REPEATED:
  402. message.ClearField(field.name)
  403. if not isinstance(value, list):
  404. raise ParseError('repeated field {0} must be in [] which is '
  405. '{1}.'.format(name, value))
  406. if field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_MESSAGE:
  407. # Repeated message field.
  408. for item in value:
  409. sub_message = getattr(message, field.name).add()
  410. # None is a null_value in Value.
  411. if (item is None and
  412. sub_message.DESCRIPTOR.full_name != 'google.protobuf.Value'):
  413. raise ParseError('null is not allowed to be used as an element'
  414. ' in a repeated field.')
  415. self.ConvertMessage(item, sub_message)
  416. else:
  417. # Repeated scalar field.
  418. for item in value:
  419. if item is None:
  420. raise ParseError('null is not allowed to be used as an element'
  421. ' in a repeated field.')
  422. getattr(message, field.name).append(
  423. _ConvertScalarFieldValue(item, field))
  424. elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_MESSAGE:
  425. sub_message = getattr(message, field.name)
  426. sub_message.SetInParent()
  427. self.ConvertMessage(value, sub_message)
  428. else:
  429. setattr(message, field.name, _ConvertScalarFieldValue(value, field))
  430. except ParseError as e:
  431. if field and field.containing_oneof is None:
  432. raise ParseError('Failed to parse {0} field: {1}'.format(name, e))
  433. else:
  434. raise ParseError(str(e))
  435. except ValueError as e:
  436. raise ParseError('Failed to parse {0} field: {1}.'.format(name, e))
  437. except TypeError as e:
  438. raise ParseError('Failed to parse {0} field: {1}.'.format(name, e))
  439. def _ConvertAnyMessage(self, value, message):
  440. """Convert a JSON representation into Any message."""
  441. if isinstance(value, dict) and not value:
  442. return
  443. try:
  444. type_url = value['@type']
  445. except KeyError:
  446. raise ParseError('@type is missing when parsing any message.')
  447. sub_message = _CreateMessageFromTypeUrl(type_url)
  448. message_descriptor = sub_message.DESCRIPTOR
  449. full_name = message_descriptor.full_name
  450. if _IsWrapperMessage(message_descriptor):
  451. self._ConvertWrapperMessage(value['value'], sub_message)
  452. elif full_name in _WKTJSONMETHODS:
  453. methodcaller(
  454. _WKTJSONMETHODS[full_name][1], value['value'], sub_message)(self)
  455. else:
  456. del value['@type']
  457. self._ConvertFieldValuePair(value, sub_message)
  458. # Sets Any message
  459. message.value = sub_message.SerializeToString()
  460. message.type_url = type_url
  461. def _ConvertGenericMessage(self, value, message):
  462. """Convert a JSON representation into message with FromJsonString."""
  463. # Durantion, Timestamp, FieldMask have FromJsonString method to do the
  464. # convert. Users can also call the method directly.
  465. message.FromJsonString(value)
  466. def _ConvertValueMessage(self, value, message):
  467. """Convert a JSON representation into Value message."""
  468. if isinstance(value, dict):
  469. self._ConvertStructMessage(value, message.struct_value)
  470. elif isinstance(value, list):
  471. self. _ConvertListValueMessage(value, message.list_value)
  472. elif value is None:
  473. message.null_value = 0
  474. elif isinstance(value, bool):
  475. message.bool_value = value
  476. elif isinstance(value, six.string_types):
  477. message.string_value = value
  478. elif isinstance(value, _INT_OR_FLOAT):
  479. message.number_value = value
  480. else:
  481. raise ParseError('Unexpected type for Value message.')
  482. def _ConvertListValueMessage(self, value, message):
  483. """Convert a JSON representation into ListValue message."""
  484. if not isinstance(value, list):
  485. raise ParseError(
  486. 'ListValue must be in [] which is {0}.'.format(value))
  487. message.ClearField('values')
  488. for item in value:
  489. self._ConvertValueMessage(item, message.values.add())
  490. def _ConvertStructMessage(self, value, message):
  491. """Convert a JSON representation into Struct message."""
  492. if not isinstance(value, dict):
  493. raise ParseError(
  494. 'Struct must be in a dict which is {0}.'.format(value))
  495. for key in value:
  496. self._ConvertValueMessage(value[key], message.fields[key])
  497. return
  498. def _ConvertWrapperMessage(self, value, message):
  499. """Convert a JSON representation into Wrapper message."""
  500. field = message.DESCRIPTOR.fields_by_name['value']
  501. setattr(message, 'value', _ConvertScalarFieldValue(value, field))
  502. def _ConvertMapFieldValue(self, value, message, field):
  503. """Convert map field value for a message map field.
  504. Args:
  505. value: A JSON object to convert the map field value.
  506. message: A protocol message to record the converted data.
  507. field: The descriptor of the map field to be converted.
  508. Raises:
  509. ParseError: In case of convert problems.
  510. """
  511. if not isinstance(value, dict):
  512. raise ParseError(
  513. 'Map field {0} must be in a dict which is {1}.'.format(
  514. field.name, value))
  515. key_field = field.message_type.fields_by_name['key']
  516. value_field = field.message_type.fields_by_name['value']
  517. for key in value:
  518. key_value = _ConvertScalarFieldValue(key, key_field, True)
  519. if value_field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_MESSAGE:
  520. self.ConvertMessage(value[key], getattr(
  521. message, field.name)[key_value])
  522. else:
  523. getattr(message, field.name)[key_value] = _ConvertScalarFieldValue(
  524. value[key], value_field)
  525. def _ConvertScalarFieldValue(value, field, require_str=False):
  526. """Convert a single scalar field value.
  527. Args:
  528. value: A scalar value to convert the scalar field value.
  529. field: The descriptor of the field to convert.
  530. require_str: If True, the field value must be a str.
  531. Returns:
  532. The converted scalar field value
  533. Raises:
  534. ParseError: In case of convert problems.
  535. """
  536. if field.cpp_type in _INT_TYPES:
  537. return _ConvertInteger(value)
  538. elif field.cpp_type in _FLOAT_TYPES:
  539. return _ConvertFloat(value)
  540. elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_BOOL:
  541. return _ConvertBool(value, require_str)
  542. elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_STRING:
  543. if field.type == descriptor.FieldDescriptor.TYPE_BYTES:
  544. return base64.b64decode(value)
  545. else:
  546. # Checking for unpaired surrogates appears to be unreliable,
  547. # depending on the specific Python version, so we check manually.
  548. if _UNPAIRED_SURROGATE_PATTERN.search(value):
  549. raise ParseError('Unpaired surrogate')
  550. return value
  551. elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_ENUM:
  552. # Convert an enum value.
  553. enum_value = field.enum_type.values_by_name.get(value, None)
  554. if enum_value is None:
  555. try:
  556. number = int(value)
  557. enum_value = field.enum_type.values_by_number.get(number, None)
  558. except ValueError:
  559. raise ParseError('Invalid enum value {0} for enum type {1}.'.format(
  560. value, field.enum_type.full_name))
  561. if enum_value is None:
  562. raise ParseError('Invalid enum value {0} for enum type {1}.'.format(
  563. value, field.enum_type.full_name))
  564. return enum_value.number
  565. def _ConvertInteger(value):
  566. """Convert an integer.
  567. Args:
  568. value: A scalar value to convert.
  569. Returns:
  570. The integer value.
  571. Raises:
  572. ParseError: If an integer couldn't be consumed.
  573. """
  574. if isinstance(value, float) and not value.is_integer():
  575. raise ParseError('Couldn\'t parse integer: {0}.'.format(value))
  576. if isinstance(value, six.text_type) and value.find(' ') != -1:
  577. raise ParseError('Couldn\'t parse integer: "{0}".'.format(value))
  578. return int(value)
  579. def _ConvertFloat(value):
  580. """Convert an floating point number."""
  581. if value == 'nan':
  582. raise ParseError('Couldn\'t parse float "nan", use "NaN" instead.')
  583. try:
  584. # Assume Python compatible syntax.
  585. return float(value)
  586. except ValueError:
  587. # Check alternative spellings.
  588. if value == _NEG_INFINITY:
  589. return float('-inf')
  590. elif value == _INFINITY:
  591. return float('inf')
  592. elif value == _NAN:
  593. return float('nan')
  594. else:
  595. raise ParseError('Couldn\'t parse float: {0}.'.format(value))
  596. def _ConvertBool(value, require_str):
  597. """Convert a boolean value.
  598. Args:
  599. value: A scalar value to convert.
  600. require_str: If True, value must be a str.
  601. Returns:
  602. The bool parsed.
  603. Raises:
  604. ParseError: If a boolean value couldn't be consumed.
  605. """
  606. if require_str:
  607. if value == 'true':
  608. return True
  609. elif value == 'false':
  610. return False
  611. else:
  612. raise ParseError('Expected "true" or "false", not {0}.'.format(value))
  613. if not isinstance(value, bool):
  614. raise ParseError('Expected true or false without quotes.')
  615. return value
  616. _WKTJSONMETHODS = {
  617. 'google.protobuf.Any': ['_AnyMessageToJsonObject',
  618. '_ConvertAnyMessage'],
  619. 'google.protobuf.Duration': ['_GenericMessageToJsonObject',
  620. '_ConvertGenericMessage'],
  621. 'google.protobuf.FieldMask': ['_GenericMessageToJsonObject',
  622. '_ConvertGenericMessage'],
  623. 'google.protobuf.ListValue': ['_ListValueMessageToJsonObject',
  624. '_ConvertListValueMessage'],
  625. 'google.protobuf.Struct': ['_StructMessageToJsonObject',
  626. '_ConvertStructMessage'],
  627. 'google.protobuf.Timestamp': ['_GenericMessageToJsonObject',
  628. '_ConvertGenericMessage'],
  629. 'google.protobuf.Value': ['_ValueMessageToJsonObject',
  630. '_ConvertValueMessage']
  631. }