| OLD | NEW |
| 1 #! /usr/bin/env python | 1 #! /usr/bin/env python |
| 2 # | 2 # |
| 3 # Protocol Buffers - Google's data interchange format | 3 # Protocol Buffers - Google's data interchange format |
| 4 # Copyright 2008 Google Inc. All rights reserved. | 4 # Copyright 2008 Google Inc. All rights reserved. |
| 5 # https://developers.google.com/protocol-buffers/ | 5 # https://developers.google.com/protocol-buffers/ |
| 6 # | 6 # |
| 7 # Redistribution and use in source and binary forms, with or without | 7 # Redistribution and use in source and binary forms, with or without |
| 8 # modification, are permitted provided that the following conditions are | 8 # modification, are permitted provided that the following conditions are |
| 9 # met: | 9 # met: |
| 10 # | 10 # |
| (...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 '{}') | 240 '{}') |
| 241 message.oneof_int32_value = 0 | 241 message.oneof_int32_value = 0 |
| 242 self.assertEqual( | 242 self.assertEqual( |
| 243 json_format.MessageToJson(message, True), | 243 json_format.MessageToJson(message, True), |
| 244 '{\n' | 244 '{\n' |
| 245 ' "oneofInt32Value": 0\n' | 245 ' "oneofInt32Value": 0\n' |
| 246 '}') | 246 '}') |
| 247 parsed_message = json_format_proto3_pb2.TestOneof() | 247 parsed_message = json_format_proto3_pb2.TestOneof() |
| 248 self.CheckParseBack(message, parsed_message) | 248 self.CheckParseBack(message, parsed_message) |
| 249 | 249 |
| 250 def testSurrogates(self): |
| 251 # Test correct surrogate handling. |
| 252 message = json_format_proto3_pb2.TestMessage() |
| 253 json_format.Parse('{"stringValue": "\\uD83D\\uDE01"}', message) |
| 254 self.assertEqual(message.string_value, |
| 255 b'\xF0\x9F\x98\x81'.decode('utf-8', 'strict')) |
| 256 |
| 257 # Error case: unpaired high surrogate. |
| 258 self.CheckError( |
| 259 '{"stringValue": "\\uD83D"}', |
| 260 r'Invalid \\uXXXX escape|Unpaired.*surrogate') |
| 261 |
| 262 # Unpaired low surrogate. |
| 263 self.CheckError( |
| 264 '{"stringValue": "\\uDE01"}', |
| 265 r'Invalid \\uXXXX escape|Unpaired.*surrogate') |
| 266 |
| 250 def testTimestampMessage(self): | 267 def testTimestampMessage(self): |
| 251 message = json_format_proto3_pb2.TestTimestamp() | 268 message = json_format_proto3_pb2.TestTimestamp() |
| 252 message.value.seconds = 0 | 269 message.value.seconds = 0 |
| 253 message.value.nanos = 0 | 270 message.value.nanos = 0 |
| 254 message.repeated_value.add().seconds = 20 | 271 message.repeated_value.add().seconds = 20 |
| 255 message.repeated_value[0].nanos = 1 | 272 message.repeated_value[0].nanos = 1 |
| 256 message.repeated_value.add().seconds = 0 | 273 message.repeated_value.add().seconds = 0 |
| 257 message.repeated_value[1].nanos = 10000 | 274 message.repeated_value[1].nanos = 10000 |
| 258 message.repeated_value.add().seconds = 100000000 | 275 message.repeated_value.add().seconds = 100000000 |
| 259 message.repeated_value[2].nanos = 0 | 276 message.repeated_value[2].nanos = 0 |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 451 ' "value": 5678\n' | 468 ' "value": 5678\n' |
| 452 ' },\n' | 469 ' },\n' |
| 453 ' {}],\n' | 470 ' {}],\n' |
| 454 ' "value": {\n' | 471 ' "value": {\n' |
| 455 ' "@type": "type.googleapis.com/proto3.MessageType",\n' | 472 ' "@type": "type.googleapis.com/proto3.MessageType",\n' |
| 456 ' "value": 1234\n' | 473 ' "value": 1234\n' |
| 457 ' }\n' | 474 ' }\n' |
| 458 '}\n')) | 475 '}\n')) |
| 459 parsed_message = json_format_proto3_pb2.TestAny() | 476 parsed_message = json_format_proto3_pb2.TestAny() |
| 460 self.CheckParseBack(message, parsed_message) | 477 self.CheckParseBack(message, parsed_message) |
| 478 # Must print @type first |
| 479 test_message = json_format_proto3_pb2.TestMessage( |
| 480 bool_value=True, |
| 481 int32_value=20, |
| 482 int64_value=-20, |
| 483 uint32_value=20, |
| 484 uint64_value=20, |
| 485 double_value=3.14, |
| 486 string_value='foo') |
| 487 message.Clear() |
| 488 message.value.Pack(test_message) |
| 489 self.assertEqual( |
| 490 json_format.MessageToJson(message, False)[0:68], |
| 491 '{\n' |
| 492 ' "value": {\n' |
| 493 ' "@type": "type.googleapis.com/proto3.TestMessage"') |
| 461 | 494 |
| 462 def testWellKnownInAnyMessage(self): | 495 def testWellKnownInAnyMessage(self): |
| 463 message = any_pb2.Any() | 496 message = any_pb2.Any() |
| 464 int32_value = wrappers_pb2.Int32Value() | 497 int32_value = wrappers_pb2.Int32Value() |
| 465 int32_value.value = 1234 | 498 int32_value.value = 1234 |
| 466 message.Pack(int32_value) | 499 message.Pack(int32_value) |
| 467 self.assertEqual( | 500 self.assertEqual( |
| 468 json.loads(json_format.MessageToJson(message, True)), | 501 json.loads(json_format.MessageToJson(message, True)), |
| 469 json.loads( | 502 json.loads( |
| 470 '{\n' | 503 '{\n' |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 599 'Type "proto3.EnumType" has no value named baz.') | 632 'Type "proto3.EnumType" has no value named baz.') |
| 600 | 633 |
| 601 def testParseBadIdentifer(self): | 634 def testParseBadIdentifer(self): |
| 602 self.CheckError('{int32Value: 1}', | 635 self.CheckError('{int32Value: 1}', |
| 603 (r'Failed to load JSON: Expecting property name' | 636 (r'Failed to load JSON: Expecting property name' |
| 604 r'( enclosed in double quotes)?: line 1')) | 637 r'( enclosed in double quotes)?: line 1')) |
| 605 self.CheckError('{"unknownName": 1}', | 638 self.CheckError('{"unknownName": 1}', |
| 606 'Message type "proto3.TestMessage" has no field named ' | 639 'Message type "proto3.TestMessage" has no field named ' |
| 607 '"unknownName".') | 640 '"unknownName".') |
| 608 | 641 |
| 642 def testIgnoreUnknownField(self): |
| 643 text = '{"unknownName": 1}' |
| 644 parsed_message = json_format_proto3_pb2.TestMessage() |
| 645 json_format.Parse(text, parsed_message, ignore_unknown_fields=True) |
| 646 text = ('{\n' |
| 647 ' "repeatedValue": [ {\n' |
| 648 ' "@type": "type.googleapis.com/proto3.MessageType",\n' |
| 649 ' "unknownName": 1\n' |
| 650 ' }]\n' |
| 651 '}\n') |
| 652 parsed_message = json_format_proto3_pb2.TestAny() |
| 653 json_format.Parse(text, parsed_message, ignore_unknown_fields=True) |
| 654 |
| 609 def testDuplicateField(self): | 655 def testDuplicateField(self): |
| 610 # Duplicate key check is not supported for python2.6 | 656 # Duplicate key check is not supported for python2.6 |
| 611 if sys.version_info < (2, 7): | 657 if sys.version_info < (2, 7): |
| 612 return | 658 return |
| 613 self.CheckError('{"int32Value": 1,\n"int32Value":2}', | 659 self.CheckError('{"int32Value": 1,\n"int32Value":2}', |
| 614 'Failed to load JSON: duplicate key int32Value.') | 660 'Failed to load JSON: duplicate key int32Value.') |
| 615 | 661 |
| 616 def testInvalidBoolValue(self): | 662 def testInvalidBoolValue(self): |
| 617 self.CheckError('{"boolValue": 1}', | 663 self.CheckError('{"boolValue": 1}', |
| 618 'Failed to parse boolValue field: ' | 664 'Failed to parse boolValue field: ' |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 760 'type.googleapis.com/MessageNotExist.', | 806 'type.googleapis.com/MessageNotExist.', |
| 761 json_format.Parse, text, message) | 807 json_format.Parse, text, message) |
| 762 # Only last part is to be used: b/25630112 | 808 # Only last part is to be used: b/25630112 |
| 763 text = (r'{"@type": "incorrect.googleapis.com/google.protobuf.Int32Value",' | 809 text = (r'{"@type": "incorrect.googleapis.com/google.protobuf.Int32Value",' |
| 764 r'"value": 1234}') | 810 r'"value": 1234}') |
| 765 json_format.Parse(text, message) | 811 json_format.Parse(text, message) |
| 766 | 812 |
| 767 | 813 |
| 768 if __name__ == '__main__': | 814 if __name__ == '__main__': |
| 769 unittest.main() | 815 unittest.main() |
| OLD | NEW |