json_format.py 29 KB

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