diff options
author | Jie Luo <jieluo@jieluo-macbookair.roam.corp.google.com> | 2015-10-09 17:07:03 -0700 |
---|---|---|
committer | Jie Luo <jieluo@google.com> | 2015-10-19 16:25:54 -0700 |
commit | 2850a98275b1397c511973e207a16ea704ffb18f (patch) | |
tree | 0ab2a62841f28774a39f9a240185952b3caf0eea /python/google | |
parent | 49f24afb45b7add17af3ed4493fa0a94d1cc64da (diff) | |
download | protobuf-2850a98275b1397c511973e207a16ea704ffb18f.tar.gz protobuf-2850a98275b1397c511973e207a16ea704ffb18f.tar.bz2 protobuf-2850a98275b1397c511973e207a16ea704ffb18f.zip |
fix json_format for python2.6:
1, objcect_pair_hook is not supported in python2.6, so duplicated key check is removed in 2.6
2, total_seconds is not suppoted in python2.6, changed to compute seconds directly
Diffstat (limited to 'python/google')
-rw-r--r-- | python/google/protobuf/internal/json_format_test.py | 13 | ||||
-rw-r--r-- | python/google/protobuf/json_format.py | 19 |
2 files changed, 25 insertions, 7 deletions
diff --git a/python/google/protobuf/internal/json_format_test.py b/python/google/protobuf/internal/json_format_test.py index 4e2f35e4..69197865 100644 --- a/python/google/protobuf/internal/json_format_test.py +++ b/python/google/protobuf/internal/json_format_test.py @@ -410,6 +410,9 @@ class JsonFormatTest(JsonFormatBase): '"unknownName".') def testDuplicateField(self): + # Duplicate key check is not supported for python2.6 + if sys.version_info < (2, 7): + return self.CheckError('{"int32Value": 1,\n"int32Value":2}', 'Failed to load JSON: duplicate key int32Value') @@ -468,15 +471,17 @@ class JsonFormatTest(JsonFormatBase): (r'Failed to load JSON: Expecting property name' r'( enclosed in double quotes)?: line 1'), json_format.Parse, text, message) - text = r'{"stringMap": {"a": 3, "\u0061": 2}}' + text = '{"boolMap": {"null": 1}}' self.assertRaisesRegexp( json_format.ParseError, - 'Failed to load JSON: duplicate key a', + 'Failed to parse boolMap field: Expect "true" or "false", not null.', json_format.Parse, text, message) - text = '{"boolMap": {"null": 1}}' + if sys.version_info < (2, 7): + return + text = r'{"stringMap": {"a": 3, "\u0061": 2}}' self.assertRaisesRegexp( json_format.ParseError, - 'Failed to parse boolMap field: Expect "true" or "false", not null.', + 'Failed to load JSON: duplicate key a', json_format.Parse, text, message) def testInvalidTimestamp(self): diff --git a/python/google/protobuf/json_format.py b/python/google/protobuf/json_format.py index 09110e04..d95557d7 100644 --- a/python/google/protobuf/json_format.py +++ b/python/google/protobuf/json_format.py @@ -37,6 +37,7 @@ from datetime import datetime import json import math import re +import sys from google.protobuf import descriptor @@ -114,7 +115,14 @@ def _RegularMessageToJsonObject(message, including_default_value_fields): # Convert a map field. js_map = {} for key in value: - js_map[key] = _ConvertFieldToJsonObject( + if isinstance(key, bool): + if key: + recorded_key = 'true' + else: + recorded_key = 'false' + else: + recorded_key = key + js_map[recorded_key] = _ConvertFieldToJsonObject( field.message_type.fields_by_name['value'], value[key], including_default_value_fields) js[name] = js_map @@ -297,7 +305,11 @@ def Parse(text, message): """ if not isinstance(text, _UNICODETYPE): text = text.decode('utf-8') try: - js = json.loads(text, object_pairs_hook=_DuplicateChecker) + if sys.version_info < (2, 7): + # object_pair_hook is not supported before python2.7 + js = json.loads(text) + else: + js = json.loads(text, object_pairs_hook=_DuplicateChecker) except ValueError as e: raise ParseError('Failed to load JSON: ' + str(e)) _ConvertFieldValuePair(js, message) @@ -419,7 +431,8 @@ def _ConvertTimestampMessage(value, message): second_value = time_value[:point_position] nano_value = time_value[point_position + 1:] date_object = datetime.strptime(second_value, _TIMESTAMPFOMAT) - seconds = (date_object - datetime(1970, 1, 1)).total_seconds() + td = date_object - datetime(1970, 1, 1) + seconds = td.seconds + td.days * 24 * 3600 if len(nano_value) > 9: raise ParseError( 'Failed to parse Timestamp: nanos {0} more than ' |