Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/google/protobuf/json_format.py: 14%

442 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2023-03-26 06:25 +0000

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 

33Simple 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 

45 

46import base64 

47from collections import OrderedDict 

48import json 

49import math 

50from operator import methodcaller 

51import re 

52import sys 

53 

54from google.protobuf.internal import type_checkers 

55from google.protobuf import descriptor 

56from google.protobuf import message_factory 

57from google.protobuf import symbol_database 

58 

59 

60_TIMESTAMPFOMAT = '%Y-%m-%dT%H:%M:%S' 

61_INT_TYPES = frozenset([descriptor.FieldDescriptor.CPPTYPE_INT32, 

62 descriptor.FieldDescriptor.CPPTYPE_UINT32, 

63 descriptor.FieldDescriptor.CPPTYPE_INT64, 

64 descriptor.FieldDescriptor.CPPTYPE_UINT64]) 

65_INT64_TYPES = frozenset([descriptor.FieldDescriptor.CPPTYPE_INT64, 

66 descriptor.FieldDescriptor.CPPTYPE_UINT64]) 

67_FLOAT_TYPES = frozenset([descriptor.FieldDescriptor.CPPTYPE_FLOAT, 

68 descriptor.FieldDescriptor.CPPTYPE_DOUBLE]) 

69_INFINITY = 'Infinity' 

70_NEG_INFINITY = '-Infinity' 

71_NAN = 'NaN' 

72 

73_UNPAIRED_SURROGATE_PATTERN = re.compile( 

74 u'[\ud800-\udbff](?![\udc00-\udfff])|(?<![\ud800-\udbff])[\udc00-\udfff]') 

75 

76_VALID_EXTENSION_NAME = re.compile(r'\[[a-zA-Z0-9\._]*\]$') 

77 

78 

79class Error(Exception): 

80 """Top-level module error for json_format.""" 

81 

82 

83class SerializeToJsonError(Error): 

84 """Thrown if serialization to JSON fails.""" 

85 

86 

87class ParseError(Error): 

88 """Thrown in case of parsing error.""" 

89 

90 

91def MessageToJson( 

92 message, 

93 including_default_value_fields=False, 

94 preserving_proto_field_name=False, 

95 indent=2, 

96 sort_keys=False, 

97 use_integers_for_enums=False, 

98 descriptor_pool=None, 

99 float_precision=None, 

100 ensure_ascii=True): 

101 """Converts protobuf message to JSON format. 

102 

103 Args: 

104 message: The protocol buffers message instance to serialize. 

105 including_default_value_fields: If True, singular primitive fields, 

106 repeated fields, and map fields will always be serialized. If 

107 False, only serialize non-empty fields. Singular message fields 

108 and oneof fields are not affected by this option. 

109 preserving_proto_field_name: If True, use the original proto field 

110 names as defined in the .proto file. If False, convert the field 

111 names to lowerCamelCase. 

112 indent: The JSON object will be pretty-printed with this indent level. 

113 An indent level of 0 or negative will only insert newlines. If the 

114 indent level is None, no newlines will be inserted. 

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

116 use_integers_for_enums: If true, print integers instead of enum names. 

117 descriptor_pool: A Descriptor Pool for resolving types. If None use the 

118 default. 

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

120 ensure_ascii: If True, strings with non-ASCII characters are escaped. 

121 If False, Unicode strings are returned unchanged. 

122 

123 Returns: 

124 A string containing the JSON formatted protocol buffer message. 

125 """ 

126 printer = _Printer( 

127 including_default_value_fields, 

128 preserving_proto_field_name, 

129 use_integers_for_enums, 

130 descriptor_pool, 

131 float_precision=float_precision) 

132 return printer.ToJsonString(message, indent, sort_keys, ensure_ascii) 

133 

134 

135def MessageToDict( 

136 message, 

137 including_default_value_fields=False, 

138 preserving_proto_field_name=False, 

139 use_integers_for_enums=False, 

140 descriptor_pool=None, 

141 float_precision=None): 

142 """Converts protobuf message to a dictionary. 

143 

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

145 

146 Args: 

147 message: The protocol buffers message instance to serialize. 

148 including_default_value_fields: If True, singular primitive fields, 

149 repeated fields, and map fields will always be serialized. If 

150 False, only serialize non-empty fields. Singular message fields 

151 and oneof fields are not affected by this option. 

152 preserving_proto_field_name: If True, use the original proto field 

153 names as defined in the .proto file. If False, convert the field 

154 names to lowerCamelCase. 

155 use_integers_for_enums: If true, print integers instead of enum names. 

156 descriptor_pool: A Descriptor Pool for resolving types. If None use the 

157 default. 

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

159 

160 Returns: 

161 A dict representation of the protocol buffer message. 

162 """ 

163 printer = _Printer( 

164 including_default_value_fields, 

165 preserving_proto_field_name, 

166 use_integers_for_enums, 

167 descriptor_pool, 

168 float_precision=float_precision) 

169 # pylint: disable=protected-access 

170 return printer._MessageToJsonObject(message) 

171 

172 

173def _IsMapEntry(field): 

174 return (field.type == descriptor.FieldDescriptor.TYPE_MESSAGE and 

175 field.message_type.has_options and 

176 field.message_type.GetOptions().map_entry) 

177 

178 

179class _Printer(object): 

180 """JSON format printer for protocol message.""" 

181 

182 def __init__( 

183 self, 

184 including_default_value_fields=False, 

185 preserving_proto_field_name=False, 

186 use_integers_for_enums=False, 

187 descriptor_pool=None, 

188 float_precision=None): 

189 self.including_default_value_fields = including_default_value_fields 

190 self.preserving_proto_field_name = preserving_proto_field_name 

191 self.use_integers_for_enums = use_integers_for_enums 

192 self.descriptor_pool = descriptor_pool 

193 if float_precision: 

194 self.float_format = '.{}g'.format(float_precision) 

195 else: 

196 self.float_format = None 

197 

198 def ToJsonString(self, message, indent, sort_keys, ensure_ascii): 

199 js = self._MessageToJsonObject(message) 

200 return json.dumps( 

201 js, indent=indent, sort_keys=sort_keys, ensure_ascii=ensure_ascii) 

202 

203 def _MessageToJsonObject(self, message): 

204 """Converts message to an object according to Proto3 JSON Specification.""" 

205 message_descriptor = message.DESCRIPTOR 

206 full_name = message_descriptor.full_name 

207 if _IsWrapperMessage(message_descriptor): 

208 return self._WrapperMessageToJsonObject(message) 

209 if full_name in _WKTJSONMETHODS: 

210 return methodcaller(_WKTJSONMETHODS[full_name][0], message)(self) 

211 js = {} 

212 return self._RegularMessageToJsonObject(message, js) 

213 

214 def _RegularMessageToJsonObject(self, message, js): 

215 """Converts normal message according to Proto3 JSON Specification.""" 

216 fields = message.ListFields() 

217 

218 try: 

219 for field, value in fields: 

220 if self.preserving_proto_field_name: 

221 name = field.name 

222 else: 

223 name = field.json_name 

224 if _IsMapEntry(field): 

225 # Convert a map field. 

226 v_field = field.message_type.fields_by_name['value'] 

227 js_map = {} 

228 for key in value: 

229 if isinstance(key, bool): 

230 if key: 

231 recorded_key = 'true' 

232 else: 

233 recorded_key = 'false' 

234 else: 

235 recorded_key = str(key) 

236 js_map[recorded_key] = self._FieldToJsonObject( 

237 v_field, value[key]) 

238 js[name] = js_map 

239 elif field.label == descriptor.FieldDescriptor.LABEL_REPEATED: 

240 # Convert a repeated field. 

241 js[name] = [self._FieldToJsonObject(field, k) 

242 for k in value] 

243 elif field.is_extension: 

244 name = '[%s]' % field.full_name 

245 js[name] = self._FieldToJsonObject(field, value) 

246 else: 

247 js[name] = self._FieldToJsonObject(field, value) 

248 

249 # Serialize default value if including_default_value_fields is True. 

250 if self.including_default_value_fields: 

251 message_descriptor = message.DESCRIPTOR 

252 for field in message_descriptor.fields: 

253 # Singular message fields and oneof fields will not be affected. 

254 if ((field.label != descriptor.FieldDescriptor.LABEL_REPEATED and 

255 field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_MESSAGE) or 

256 field.containing_oneof): 

257 continue 

258 if self.preserving_proto_field_name: 

259 name = field.name 

260 else: 

261 name = field.json_name 

262 if name in js: 

263 # Skip the field which has been serialized already. 

264 continue 

265 if _IsMapEntry(field): 

266 js[name] = {} 

267 elif field.label == descriptor.FieldDescriptor.LABEL_REPEATED: 

268 js[name] = [] 

269 else: 

270 js[name] = self._FieldToJsonObject(field, field.default_value) 

271 

272 except ValueError as e: 

273 raise SerializeToJsonError( 

274 'Failed to serialize {0} field: {1}.'.format(field.name, e)) from e 

275 

276 return js 

277 

278 def _FieldToJsonObject(self, field, value): 

279 """Converts field value according to Proto3 JSON Specification.""" 

280 if field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_MESSAGE: 

281 return self._MessageToJsonObject(value) 

282 elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_ENUM: 

283 if self.use_integers_for_enums: 

284 return value 

285 if field.enum_type.full_name == 'google.protobuf.NullValue': 

286 return None 

287 enum_value = field.enum_type.values_by_number.get(value, None) 

288 if enum_value is not None: 

289 return enum_value.name 

290 else: 

291 if field.enum_type.is_closed: 

292 raise SerializeToJsonError('Enum field contains an integer value ' 

293 'which can not mapped to an enum value.') 

294 else: 

295 return value 

296 elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_STRING: 

297 if field.type == descriptor.FieldDescriptor.TYPE_BYTES: 

298 # Use base64 Data encoding for bytes 

299 return base64.b64encode(value).decode('utf-8') 

300 else: 

301 return value 

302 elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_BOOL: 

303 return bool(value) 

304 elif field.cpp_type in _INT64_TYPES: 

305 return str(value) 

306 elif field.cpp_type in _FLOAT_TYPES: 

307 if math.isinf(value): 

308 if value < 0.0: 

309 return _NEG_INFINITY 

310 else: 

311 return _INFINITY 

312 if math.isnan(value): 

313 return _NAN 

314 if field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_FLOAT: 

315 if self.float_format: 

316 return float(format(value, self.float_format)) 

317 else: 

318 return type_checkers.ToShortestFloat(value) 

319 

320 return value 

321 

322 def _AnyMessageToJsonObject(self, message): 

323 """Converts Any message according to Proto3 JSON Specification.""" 

324 if not message.ListFields(): 

325 return {} 

326 # Must print @type first, use OrderedDict instead of {} 

327 js = OrderedDict() 

328 type_url = message.type_url 

329 js['@type'] = type_url 

330 sub_message = _CreateMessageFromTypeUrl(type_url, self.descriptor_pool) 

331 sub_message.ParseFromString(message.value) 

332 message_descriptor = sub_message.DESCRIPTOR 

333 full_name = message_descriptor.full_name 

334 if _IsWrapperMessage(message_descriptor): 

335 js['value'] = self._WrapperMessageToJsonObject(sub_message) 

336 return js 

337 if full_name in _WKTJSONMETHODS: 

338 js['value'] = methodcaller(_WKTJSONMETHODS[full_name][0], 

339 sub_message)(self) 

340 return js 

341 return self._RegularMessageToJsonObject(sub_message, js) 

342 

343 def _GenericMessageToJsonObject(self, message): 

344 """Converts message according to Proto3 JSON Specification.""" 

345 # Duration, Timestamp and FieldMask have ToJsonString method to do the 

346 # convert. Users can also call the method directly. 

347 return message.ToJsonString() 

348 

349 def _ValueMessageToJsonObject(self, message): 

350 """Converts Value message according to Proto3 JSON Specification.""" 

351 which = message.WhichOneof('kind') 

352 # If the Value message is not set treat as null_value when serialize 

353 # to JSON. The parse back result will be different from original message. 

354 if which is None or which == 'null_value': 

355 return None 

356 if which == 'list_value': 

357 return self._ListValueMessageToJsonObject(message.list_value) 

358 if which == 'number_value': 

359 value = message.number_value 

360 if math.isinf(value): 

361 raise ValueError('Fail to serialize Infinity for Value.number_value, ' 

362 'which would parse as string_value') 

363 if math.isnan(value): 

364 raise ValueError('Fail to serialize NaN for Value.number_value, ' 

365 'which would parse as string_value') 

366 else: 

367 value = getattr(message, which) 

368 oneof_descriptor = message.DESCRIPTOR.fields_by_name[which] 

369 return self._FieldToJsonObject(oneof_descriptor, value) 

370 

371 def _ListValueMessageToJsonObject(self, message): 

372 """Converts ListValue message according to Proto3 JSON Specification.""" 

373 return [self._ValueMessageToJsonObject(value) 

374 for value in message.values] 

375 

376 def _StructMessageToJsonObject(self, message): 

377 """Converts Struct message according to Proto3 JSON Specification.""" 

378 fields = message.fields 

379 ret = {} 

380 for key in fields: 

381 ret[key] = self._ValueMessageToJsonObject(fields[key]) 

382 return ret 

383 

384 def _WrapperMessageToJsonObject(self, message): 

385 return self._FieldToJsonObject( 

386 message.DESCRIPTOR.fields_by_name['value'], message.value) 

387 

388 

389def _IsWrapperMessage(message_descriptor): 

390 return message_descriptor.file.name == 'google/protobuf/wrappers.proto' 

391 

392 

393def _DuplicateChecker(js): 

394 result = {} 

395 for name, value in js: 

396 if name in result: 

397 raise ParseError('Failed to load JSON: duplicate key {0}.'.format(name)) 

398 result[name] = value 

399 return result 

400 

401 

402def _CreateMessageFromTypeUrl(type_url, descriptor_pool): 

403 """Creates a message from a type URL.""" 

404 db = symbol_database.Default() 

405 pool = db.pool if descriptor_pool is None else descriptor_pool 

406 type_name = type_url.split('/')[-1] 

407 try: 

408 message_descriptor = pool.FindMessageTypeByName(type_name) 

409 except KeyError as e: 

410 raise TypeError( 

411 'Can not find message descriptor by type_url: {0}'.format(type_url) 

412 ) from e 

413 message_class = message_factory.GetMessageClass(message_descriptor) 

414 return message_class() 

415 

416 

417def Parse(text, 

418 message, 

419 ignore_unknown_fields=False, 

420 descriptor_pool=None, 

421 max_recursion_depth=100): 

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

423 

424 Args: 

425 text: Message JSON representation. 

426 message: A protocol buffer message to merge into. 

427 ignore_unknown_fields: If True, do not raise errors for unknown fields. 

428 descriptor_pool: A Descriptor Pool for resolving types. If None use the 

429 default. 

430 max_recursion_depth: max recursion depth of JSON message to be 

431 deserialized. JSON messages over this depth will fail to be 

432 deserialized. Default value is 100. 

433 

434 Returns: 

435 The same message passed as argument. 

436 

437 Raises:: 

438 ParseError: On JSON parsing problems. 

439 """ 

440 if not isinstance(text, str): 

441 text = text.decode('utf-8') 

442 try: 

443 js = json.loads(text, object_pairs_hook=_DuplicateChecker) 

444 except ValueError as e: 

445 raise ParseError('Failed to load JSON: {0}.'.format(str(e))) from e 

446 return ParseDict(js, message, ignore_unknown_fields, descriptor_pool, 

447 max_recursion_depth) 

448 

449 

450def ParseDict(js_dict, 

451 message, 

452 ignore_unknown_fields=False, 

453 descriptor_pool=None, 

454 max_recursion_depth=100): 

455 """Parses a JSON dictionary representation into a message. 

456 

457 Args: 

458 js_dict: Dict representation of a JSON message. 

459 message: A protocol buffer message to merge into. 

460 ignore_unknown_fields: If True, do not raise errors for unknown fields. 

461 descriptor_pool: A Descriptor Pool for resolving types. If None use the 

462 default. 

463 max_recursion_depth: max recursion depth of JSON message to be 

464 deserialized. JSON messages over this depth will fail to be 

465 deserialized. Default value is 100. 

466 

467 Returns: 

468 The same message passed as argument. 

469 """ 

470 parser = _Parser(ignore_unknown_fields, descriptor_pool, max_recursion_depth) 

471 parser.ConvertMessage(js_dict, message, '') 

472 return message 

473 

474 

475_INT_OR_FLOAT = (int, float) 

476 

477 

478class _Parser(object): 

479 """JSON format parser for protocol message.""" 

480 

481 def __init__(self, ignore_unknown_fields, descriptor_pool, 

482 max_recursion_depth): 

483 self.ignore_unknown_fields = ignore_unknown_fields 

484 self.descriptor_pool = descriptor_pool 

485 self.max_recursion_depth = max_recursion_depth 

486 self.recursion_depth = 0 

487 

488 def ConvertMessage(self, value, message, path): 

489 """Convert a JSON object into a message. 

490 

491 Args: 

492 value: A JSON object. 

493 message: A WKT or regular protocol message to record the data. 

494 path: parent path to log parse error info. 

495 

496 Raises: 

497 ParseError: In case of convert problems. 

498 """ 

499 self.recursion_depth += 1 

500 if self.recursion_depth > self.max_recursion_depth: 

501 raise ParseError('Message too deep. Max recursion depth is {0}'.format( 

502 self.max_recursion_depth)) 

503 message_descriptor = message.DESCRIPTOR 

504 full_name = message_descriptor.full_name 

505 if not path: 

506 path = message_descriptor.name 

507 if _IsWrapperMessage(message_descriptor): 

508 self._ConvertWrapperMessage(value, message, path) 

509 elif full_name in _WKTJSONMETHODS: 

510 methodcaller(_WKTJSONMETHODS[full_name][1], value, message, path)(self) 

511 else: 

512 self._ConvertFieldValuePair(value, message, path) 

513 self.recursion_depth -= 1 

514 

515 def _ConvertFieldValuePair(self, js, message, path): 

516 """Convert field value pairs into regular message. 

517 

518 Args: 

519 js: A JSON object to convert the field value pairs. 

520 message: A regular protocol message to record the data. 

521 path: parent path to log parse error info. 

522 

523 Raises: 

524 ParseError: In case of problems converting. 

525 """ 

526 names = [] 

527 message_descriptor = message.DESCRIPTOR 

528 fields_by_json_name = dict((f.json_name, f) 

529 for f in message_descriptor.fields) 

530 for name in js: 

531 try: 

532 field = fields_by_json_name.get(name, None) 

533 if not field: 

534 field = message_descriptor.fields_by_name.get(name, None) 

535 if not field and _VALID_EXTENSION_NAME.match(name): 

536 if not message_descriptor.is_extendable: 

537 raise ParseError( 

538 'Message type {0} does not have extensions at {1}'.format( 

539 message_descriptor.full_name, path)) 

540 identifier = name[1:-1] # strip [] brackets 

541 # pylint: disable=protected-access 

542 field = message.Extensions._FindExtensionByName(identifier) 

543 # pylint: enable=protected-access 

544 if not field: 

545 # Try looking for extension by the message type name, dropping the 

546 # field name following the final . separator in full_name. 

547 identifier = '.'.join(identifier.split('.')[:-1]) 

548 # pylint: disable=protected-access 

549 field = message.Extensions._FindExtensionByName(identifier) 

550 # pylint: enable=protected-access 

551 if not field: 

552 if self.ignore_unknown_fields: 

553 continue 

554 raise ParseError( 

555 ('Message type "{0}" has no field named "{1}" at "{2}".\n' 

556 ' Available Fields(except extensions): "{3}"').format( 

557 message_descriptor.full_name, name, path, 

558 [f.json_name for f in message_descriptor.fields])) 

559 if name in names: 

560 raise ParseError('Message type "{0}" should not have multiple ' 

561 '"{1}" fields at "{2}".'.format( 

562 message.DESCRIPTOR.full_name, name, path)) 

563 names.append(name) 

564 value = js[name] 

565 # Check no other oneof field is parsed. 

566 if field.containing_oneof is not None and value is not None: 

567 oneof_name = field.containing_oneof.name 

568 if oneof_name in names: 

569 raise ParseError('Message type "{0}" should not have multiple ' 

570 '"{1}" oneof fields at "{2}".'.format( 

571 message.DESCRIPTOR.full_name, oneof_name, 

572 path)) 

573 names.append(oneof_name) 

574 

575 if value is None: 

576 if (field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_MESSAGE 

577 and field.message_type.full_name == 'google.protobuf.Value'): 

578 sub_message = getattr(message, field.name) 

579 sub_message.null_value = 0 

580 elif (field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_ENUM 

581 and field.enum_type.full_name == 'google.protobuf.NullValue'): 

582 setattr(message, field.name, 0) 

583 else: 

584 message.ClearField(field.name) 

585 continue 

586 

587 # Parse field value. 

588 if _IsMapEntry(field): 

589 message.ClearField(field.name) 

590 self._ConvertMapFieldValue(value, message, field, 

591 '{0}.{1}'.format(path, name)) 

592 elif field.label == descriptor.FieldDescriptor.LABEL_REPEATED: 

593 message.ClearField(field.name) 

594 if not isinstance(value, list): 

595 raise ParseError('repeated field {0} must be in [] which is ' 

596 '{1} at {2}'.format(name, value, path)) 

597 if field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_MESSAGE: 

598 # Repeated message field. 

599 for index, item in enumerate(value): 

600 sub_message = getattr(message, field.name).add() 

601 # None is a null_value in Value. 

602 if (item is None and 

603 sub_message.DESCRIPTOR.full_name != 'google.protobuf.Value'): 

604 raise ParseError('null is not allowed to be used as an element' 

605 ' in a repeated field at {0}.{1}[{2}]'.format( 

606 path, name, index)) 

607 self.ConvertMessage(item, sub_message, 

608 '{0}.{1}[{2}]'.format(path, name, index)) 

609 else: 

610 # Repeated scalar field. 

611 for index, item in enumerate(value): 

612 if item is None: 

613 raise ParseError('null is not allowed to be used as an element' 

614 ' in a repeated field at {0}.{1}[{2}]'.format( 

615 path, name, index)) 

616 getattr(message, field.name).append( 

617 _ConvertScalarFieldValue( 

618 item, field, '{0}.{1}[{2}]'.format(path, name, index))) 

619 elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_MESSAGE: 

620 if field.is_extension: 

621 sub_message = message.Extensions[field] 

622 else: 

623 sub_message = getattr(message, field.name) 

624 sub_message.SetInParent() 

625 self.ConvertMessage(value, sub_message, '{0}.{1}'.format(path, name)) 

626 else: 

627 if field.is_extension: 

628 message.Extensions[field] = _ConvertScalarFieldValue( 

629 value, field, '{0}.{1}'.format(path, name)) 

630 else: 

631 setattr( 

632 message, field.name, 

633 _ConvertScalarFieldValue(value, field, 

634 '{0}.{1}'.format(path, name))) 

635 except ParseError as e: 

636 if field and field.containing_oneof is None: 

637 raise ParseError( 

638 'Failed to parse {0} field: {1}.'.format(name, e) 

639 ) from e 

640 else: 

641 raise ParseError(str(e)) from e 

642 except ValueError as e: 

643 raise ParseError( 

644 'Failed to parse {0} field: {1}.'.format(name, e) 

645 ) from e 

646 except TypeError as e: 

647 raise ParseError( 

648 'Failed to parse {0} field: {1}.'.format(name, e) 

649 ) from e 

650 

651 def _ConvertAnyMessage(self, value, message, path): 

652 """Convert a JSON representation into Any message.""" 

653 if isinstance(value, dict) and not value: 

654 return 

655 try: 

656 type_url = value['@type'] 

657 except KeyError as e: 

658 raise ParseError( 

659 '@type is missing when parsing any message at {0}'.format(path) 

660 ) from e 

661 

662 try: 

663 sub_message = _CreateMessageFromTypeUrl(type_url, self.descriptor_pool) 

664 except TypeError as e: 

665 raise ParseError('{0} at {1}'.format(e, path)) from e 

666 message_descriptor = sub_message.DESCRIPTOR 

667 full_name = message_descriptor.full_name 

668 if _IsWrapperMessage(message_descriptor): 

669 self._ConvertWrapperMessage(value['value'], sub_message, 

670 '{0}.value'.format(path)) 

671 elif full_name in _WKTJSONMETHODS: 

672 methodcaller(_WKTJSONMETHODS[full_name][1], value['value'], sub_message, 

673 '{0}.value'.format(path))( 

674 self) 

675 else: 

676 del value['@type'] 

677 self._ConvertFieldValuePair(value, sub_message, path) 

678 value['@type'] = type_url 

679 # Sets Any message 

680 message.value = sub_message.SerializeToString() 

681 message.type_url = type_url 

682 

683 def _ConvertGenericMessage(self, value, message, path): 

684 """Convert a JSON representation into message with FromJsonString.""" 

685 # Duration, Timestamp, FieldMask have a FromJsonString method to do the 

686 # conversion. Users can also call the method directly. 

687 try: 

688 message.FromJsonString(value) 

689 except ValueError as e: 

690 raise ParseError('{0} at {1}'.format(e, path)) from e 

691 

692 def _ConvertValueMessage(self, value, message, path): 

693 """Convert a JSON representation into Value message.""" 

694 if isinstance(value, dict): 

695 self._ConvertStructMessage(value, message.struct_value, path) 

696 elif isinstance(value, list): 

697 self._ConvertListValueMessage(value, message.list_value, path) 

698 elif value is None: 

699 message.null_value = 0 

700 elif isinstance(value, bool): 

701 message.bool_value = value 

702 elif isinstance(value, str): 

703 message.string_value = value 

704 elif isinstance(value, _INT_OR_FLOAT): 

705 message.number_value = value 

706 else: 

707 raise ParseError('Value {0} has unexpected type {1} at {2}'.format( 

708 value, type(value), path)) 

709 

710 def _ConvertListValueMessage(self, value, message, path): 

711 """Convert a JSON representation into ListValue message.""" 

712 if not isinstance(value, list): 

713 raise ParseError('ListValue must be in [] which is {0} at {1}'.format( 

714 value, path)) 

715 message.ClearField('values') 

716 for index, item in enumerate(value): 

717 self._ConvertValueMessage(item, message.values.add(), 

718 '{0}[{1}]'.format(path, index)) 

719 

720 def _ConvertStructMessage(self, value, message, path): 

721 """Convert a JSON representation into Struct message.""" 

722 if not isinstance(value, dict): 

723 raise ParseError('Struct must be in a dict which is {0} at {1}'.format( 

724 value, path)) 

725 # Clear will mark the struct as modified so it will be created even if 

726 # there are no values. 

727 message.Clear() 

728 for key in value: 

729 self._ConvertValueMessage(value[key], message.fields[key], 

730 '{0}.{1}'.format(path, key)) 

731 return 

732 

733 def _ConvertWrapperMessage(self, value, message, path): 

734 """Convert a JSON representation into Wrapper message.""" 

735 field = message.DESCRIPTOR.fields_by_name['value'] 

736 setattr( 

737 message, 'value', 

738 _ConvertScalarFieldValue(value, field, path='{0}.value'.format(path))) 

739 

740 def _ConvertMapFieldValue(self, value, message, field, path): 

741 """Convert map field value for a message map field. 

742 

743 Args: 

744 value: A JSON object to convert the map field value. 

745 message: A protocol message to record the converted data. 

746 field: The descriptor of the map field to be converted. 

747 path: parent path to log parse error info. 

748 

749 Raises: 

750 ParseError: In case of convert problems. 

751 """ 

752 if not isinstance(value, dict): 

753 raise ParseError( 

754 'Map field {0} must be in a dict which is {1} at {2}'.format( 

755 field.name, value, path)) 

756 key_field = field.message_type.fields_by_name['key'] 

757 value_field = field.message_type.fields_by_name['value'] 

758 for key in value: 

759 key_value = _ConvertScalarFieldValue(key, key_field, 

760 '{0}.key'.format(path), True) 

761 if value_field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_MESSAGE: 

762 self.ConvertMessage(value[key], 

763 getattr(message, field.name)[key_value], 

764 '{0}[{1}]'.format(path, key_value)) 

765 else: 

766 getattr(message, field.name)[key_value] = _ConvertScalarFieldValue( 

767 value[key], value_field, path='{0}[{1}]'.format(path, key_value)) 

768 

769 

770def _ConvertScalarFieldValue(value, field, path, require_str=False): 

771 """Convert a single scalar field value. 

772 

773 Args: 

774 value: A scalar value to convert the scalar field value. 

775 field: The descriptor of the field to convert. 

776 path: parent path to log parse error info. 

777 require_str: If True, the field value must be a str. 

778 

779 Returns: 

780 The converted scalar field value 

781 

782 Raises: 

783 ParseError: In case of convert problems. 

784 """ 

785 try: 

786 if field.cpp_type in _INT_TYPES: 

787 return _ConvertInteger(value) 

788 elif field.cpp_type in _FLOAT_TYPES: 

789 return _ConvertFloat(value, field) 

790 elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_BOOL: 

791 return _ConvertBool(value, require_str) 

792 elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_STRING: 

793 if field.type == descriptor.FieldDescriptor.TYPE_BYTES: 

794 if isinstance(value, str): 

795 encoded = value.encode('utf-8') 

796 else: 

797 encoded = value 

798 # Add extra padding '=' 

799 padded_value = encoded + b'=' * (4 - len(encoded) % 4) 

800 return base64.urlsafe_b64decode(padded_value) 

801 else: 

802 # Checking for unpaired surrogates appears to be unreliable, 

803 # depending on the specific Python version, so we check manually. 

804 if _UNPAIRED_SURROGATE_PATTERN.search(value): 

805 raise ParseError('Unpaired surrogate') 

806 return value 

807 elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_ENUM: 

808 # Convert an enum value. 

809 enum_value = field.enum_type.values_by_name.get(value, None) 

810 if enum_value is None: 

811 try: 

812 number = int(value) 

813 enum_value = field.enum_type.values_by_number.get(number, None) 

814 except ValueError as e: 

815 raise ParseError('Invalid enum value {0} for enum type {1}'.format( 

816 value, field.enum_type.full_name)) from e 

817 if enum_value is None: 

818 if field.enum_type.is_closed: 

819 raise ParseError('Invalid enum value {0} for enum type {1}'.format( 

820 value, field.enum_type.full_name)) 

821 else: 

822 return number 

823 return enum_value.number 

824 except ParseError as e: 

825 raise ParseError('{0} at {1}'.format(e, path)) from e 

826 

827 

828def _ConvertInteger(value): 

829 """Convert an integer. 

830 

831 Args: 

832 value: A scalar value to convert. 

833 

834 Returns: 

835 The integer value. 

836 

837 Raises: 

838 ParseError: If an integer couldn't be consumed. 

839 """ 

840 if isinstance(value, float) and not value.is_integer(): 

841 raise ParseError('Couldn\'t parse integer: {0}'.format(value)) 

842 

843 if isinstance(value, str) and value.find(' ') != -1: 

844 raise ParseError('Couldn\'t parse integer: "{0}"'.format(value)) 

845 

846 if isinstance(value, bool): 

847 raise ParseError('Bool value {0} is not acceptable for ' 

848 'integer field'.format(value)) 

849 

850 return int(value) 

851 

852 

853def _ConvertFloat(value, field): 

854 """Convert an floating point number.""" 

855 if isinstance(value, float): 

856 if math.isnan(value): 

857 raise ParseError('Couldn\'t parse NaN, use quoted "NaN" instead') 

858 if math.isinf(value): 

859 if value > 0: 

860 raise ParseError('Couldn\'t parse Infinity or value too large, ' 

861 'use quoted "Infinity" instead') 

862 else: 

863 raise ParseError('Couldn\'t parse -Infinity or value too small, ' 

864 'use quoted "-Infinity" instead') 

865 if field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_FLOAT: 

866 # pylint: disable=protected-access 

867 if value > type_checkers._FLOAT_MAX: 

868 raise ParseError('Float value too large') 

869 # pylint: disable=protected-access 

870 if value < type_checkers._FLOAT_MIN: 

871 raise ParseError('Float value too small') 

872 if value == 'nan': 

873 raise ParseError('Couldn\'t parse float "nan", use "NaN" instead') 

874 try: 

875 # Assume Python compatible syntax. 

876 return float(value) 

877 except ValueError as e: 

878 # Check alternative spellings. 

879 if value == _NEG_INFINITY: 

880 return float('-inf') 

881 elif value == _INFINITY: 

882 return float('inf') 

883 elif value == _NAN: 

884 return float('nan') 

885 else: 

886 raise ParseError('Couldn\'t parse float: {0}'.format(value)) from e 

887 

888 

889def _ConvertBool(value, require_str): 

890 """Convert a boolean value. 

891 

892 Args: 

893 value: A scalar value to convert. 

894 require_str: If True, value must be a str. 

895 

896 Returns: 

897 The bool parsed. 

898 

899 Raises: 

900 ParseError: If a boolean value couldn't be consumed. 

901 """ 

902 if require_str: 

903 if value == 'true': 

904 return True 

905 elif value == 'false': 

906 return False 

907 else: 

908 raise ParseError('Expected "true" or "false", not {0}'.format(value)) 

909 

910 if not isinstance(value, bool): 

911 raise ParseError('Expected true or false without quotes') 

912 return value 

913 

914_WKTJSONMETHODS = { 

915 'google.protobuf.Any': ['_AnyMessageToJsonObject', 

916 '_ConvertAnyMessage'], 

917 'google.protobuf.Duration': ['_GenericMessageToJsonObject', 

918 '_ConvertGenericMessage'], 

919 'google.protobuf.FieldMask': ['_GenericMessageToJsonObject', 

920 '_ConvertGenericMessage'], 

921 'google.protobuf.ListValue': ['_ListValueMessageToJsonObject', 

922 '_ConvertListValueMessage'], 

923 'google.protobuf.Struct': ['_StructMessageToJsonObject', 

924 '_ConvertStructMessage'], 

925 'google.protobuf.Timestamp': ['_GenericMessageToJsonObject', 

926 '_ConvertGenericMessage'], 

927 'google.protobuf.Value': ['_ValueMessageToJsonObject', 

928 '_ConvertValueMessage'] 

929}