aboutsummaryrefslogtreecommitdiffhomepage
path: root/python/google/protobuf/json_format.py
diff options
context:
space:
mode:
Diffstat (limited to 'python/google/protobuf/json_format.py')
-rw-r--r--python/google/protobuf/json_format.py269
1 files changed, 61 insertions, 208 deletions
diff --git a/python/google/protobuf/json_format.py b/python/google/protobuf/json_format.py
index d95557d7..cb76e116 100644
--- a/python/google/protobuf/json_format.py
+++ b/python/google/protobuf/json_format.py
@@ -28,22 +28,29 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-"""Contains routines for printing protocol messages in JSON format."""
+"""Contains routines for printing protocol messages in JSON format.
+
+Simple usage example:
+
+ # Create a proto object and serialize it to a json format string.
+ message = my_proto_pb2.MyMessage(foo='bar')
+ json_string = json_format.MessageToJson(message)
+
+ # Parse a json format string to proto object.
+ message = json_format.Parse(json_string, my_proto_pb2.MyMessage())
+"""
__author__ = 'jieluo@google.com (Jie Luo)'
import base64
-from datetime import datetime
import json
import math
-import re
+from six import text_type
import sys
from google.protobuf import descriptor
_TIMESTAMPFOMAT = '%Y-%m-%dT%H:%M:%S'
-_NUMBER = re.compile(u'[0-9+-][0-9e.+-]*')
-_INTEGER = re.compile(u'[0-9+-]')
_INT_TYPES = frozenset([descriptor.FieldDescriptor.CPPTYPE_INT32,
descriptor.FieldDescriptor.CPPTYPE_UINT32,
descriptor.FieldDescriptor.CPPTYPE_INT64,
@@ -52,17 +59,20 @@ _INT64_TYPES = frozenset([descriptor.FieldDescriptor.CPPTYPE_INT64,
descriptor.FieldDescriptor.CPPTYPE_UINT64])
_FLOAT_TYPES = frozenset([descriptor.FieldDescriptor.CPPTYPE_FLOAT,
descriptor.FieldDescriptor.CPPTYPE_DOUBLE])
-if str is bytes:
- _UNICODETYPE = unicode
-else:
- _UNICODETYPE = str
+_INFINITY = 'Infinity'
+_NEG_INFINITY = '-Infinity'
+_NAN = 'NaN'
+
+class Error(Exception):
+ """Top-level module error for json_format."""
-class SerializeToJsonError(Exception):
+
+class SerializeToJsonError(Error):
"""Thrown if serialization to JSON fails."""
-class ParseError(Exception):
+class ParseError(Error):
"""Thrown in case of parsing error."""
@@ -86,12 +96,8 @@ def MessageToJson(message, including_default_value_fields=False):
def _MessageToJsonObject(message, including_default_value_fields):
"""Converts message to an object according to Proto3 JSON Specification."""
message_descriptor = message.DESCRIPTOR
- if _IsTimestampMessage(message_descriptor):
- return _TimestampMessageToJsonObject(message)
- if _IsDurationMessage(message_descriptor):
- return _DurationMessageToJsonObject(message)
- if _IsFieldMaskMessage(message_descriptor):
- return _FieldMaskMessageToJsonObject(message)
+ if hasattr(message, 'ToJsonString'):
+ return message.ToJsonString()
if _IsWrapperMessage(message_descriptor):
return _WrapperMessageToJsonObject(message)
return _RegularMessageToJsonObject(message, including_default_value_fields)
@@ -107,12 +113,14 @@ def _RegularMessageToJsonObject(message, including_default_value_fields):
"""Converts normal message according to Proto3 JSON Specification."""
js = {}
fields = message.ListFields()
+ include_default = including_default_value_fields
try:
for field, value in fields:
name = field.camelcase_name
if _IsMapEntry(field):
# Convert a map field.
+ v_field = field.message_type.fields_by_name['value']
js_map = {}
for key in value:
if isinstance(key, bool):
@@ -122,20 +130,15 @@ def _RegularMessageToJsonObject(message, including_default_value_fields):
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_map[recorded_key] = _FieldToJsonObject(
+ v_field, value[key], including_default_value_fields)
js[name] = js_map
elif field.label == descriptor.FieldDescriptor.LABEL_REPEATED:
# Convert a repeated field.
- repeated = []
- for element in value:
- repeated.append(_ConvertFieldToJsonObject(
- field, element, including_default_value_fields))
- js[name] = repeated
+ js[name] = [_FieldToJsonObject(field, k, include_default)
+ for k in value]
else:
- js[name] = _ConvertFieldToJsonObject(
- field, value, including_default_value_fields)
+ js[name] = _FieldToJsonObject(field, value, include_default)
# Serialize default value if including_default_value_fields is True.
if including_default_value_fields:
@@ -155,16 +158,16 @@ def _RegularMessageToJsonObject(message, including_default_value_fields):
elif field.label == descriptor.FieldDescriptor.LABEL_REPEATED:
js[name] = []
else:
- js[name] = _ConvertFieldToJsonObject(field, field.default_value)
+ js[name] = _FieldToJsonObject(field, field.default_value)
except ValueError as e:
raise SerializeToJsonError(
- 'Failed to serialize {0} field: {1}'.format(field.name, e))
+ 'Failed to serialize {0} field: {1}.'.format(field.name, e))
return js
-def _ConvertFieldToJsonObject(
+def _FieldToJsonObject(
field, value, including_default_value_fields=False):
"""Converts field value according to Proto3 JSON Specification."""
if field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_MESSAGE:
@@ -183,101 +186,26 @@ def _ConvertFieldToJsonObject(
else:
return value
elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_BOOL:
- if value:
- return True
- else:
- return False
+ return bool(value)
elif field.cpp_type in _INT64_TYPES:
return str(value)
elif field.cpp_type in _FLOAT_TYPES:
if math.isinf(value):
if value < 0.0:
- return '-Infinity'
+ return _NEG_INFINITY
else:
- return 'Infinity'
+ return _INFINITY
if math.isnan(value):
- return 'NaN'
+ return _NAN
return value
-def _IsTimestampMessage(message_descriptor):
- return (message_descriptor.name == 'Timestamp' and
- message_descriptor.file.name == 'google/protobuf/timestamp.proto')
-
-
-def _TimestampMessageToJsonObject(message):
- """Converts Timestamp message according to Proto3 JSON Specification."""
- nanos = message.nanos % 1e9
- dt = datetime.utcfromtimestamp(
- message.seconds + (message.nanos - nanos) / 1e9)
- result = dt.isoformat()
- if (nanos % 1e9) == 0:
- # If there are 0 fractional digits, the fractional
- # point '.' should be omitted when serializing.
- return result + 'Z'
- if (nanos % 1e6) == 0:
- # Serialize 3 fractional digits.
- return result + '.%03dZ' % (nanos / 1e6)
- if (nanos % 1e3) == 0:
- # Serialize 6 fractional digits.
- return result + '.%06dZ' % (nanos / 1e3)
- # Serialize 9 fractional digits.
- return result + '.%09dZ' % nanos
-
-
-def _IsDurationMessage(message_descriptor):
- return (message_descriptor.name == 'Duration' and
- message_descriptor.file.name == 'google/protobuf/duration.proto')
-
-
-def _DurationMessageToJsonObject(message):
- """Converts Duration message according to Proto3 JSON Specification."""
- if message.seconds < 0 or message.nanos < 0:
- result = '-'
- seconds = - message.seconds + int((0 - message.nanos) / 1e9)
- nanos = (0 - message.nanos) % 1e9
- else:
- result = ''
- seconds = message.seconds + int(message.nanos / 1e9)
- nanos = message.nanos % 1e9
- result += '%d' % seconds
- if (nanos % 1e9) == 0:
- # If there are 0 fractional digits, the fractional
- # point '.' should be omitted when serializing.
- return result + 's'
- if (nanos % 1e6) == 0:
- # Serialize 3 fractional digits.
- return result + '.%03ds' % (nanos / 1e6)
- if (nanos % 1e3) == 0:
- # Serialize 6 fractional digits.
- return result + '.%06ds' % (nanos / 1e3)
- # Serialize 9 fractional digits.
- return result + '.%09ds' % nanos
-
-
-def _IsFieldMaskMessage(message_descriptor):
- return (message_descriptor.name == 'FieldMask' and
- message_descriptor.file.name == 'google/protobuf/field_mask.proto')
-
-
-def _FieldMaskMessageToJsonObject(message):
- """Converts FieldMask message according to Proto3 JSON Specification."""
- result = ''
- first = True
- for path in message.paths:
- if not first:
- result += ','
- result += path
- first = False
- return result
-
-
def _IsWrapperMessage(message_descriptor):
return message_descriptor.file.name == 'google/protobuf/wrappers.proto'
def _WrapperMessageToJsonObject(message):
- return _ConvertFieldToJsonObject(
+ return _FieldToJsonObject(
message.DESCRIPTOR.fields_by_name['value'], message.value)
@@ -285,7 +213,7 @@ def _DuplicateChecker(js):
result = {}
for name, value in js:
if name in result:
- raise ParseError('Failed to load JSON: duplicate key ' + name)
+ raise ParseError('Failed to load JSON: duplicate key {0}.'.format(name))
result[name] = value
return result
@@ -303,7 +231,7 @@ def Parse(text, message):
Raises::
ParseError: On JSON parsing problems.
"""
- if not isinstance(text, _UNICODETYPE): text = text.decode('utf-8')
+ if not isinstance(text, text_type): text = text.decode('utf-8')
try:
if sys.version_info < (2, 7):
# object_pair_hook is not supported before python2.7
@@ -311,7 +239,7 @@ def Parse(text, message):
else:
js = json.loads(text, object_pairs_hook=_DuplicateChecker)
except ValueError as e:
- raise ParseError('Failed to load JSON: ' + str(e))
+ raise ParseError('Failed to load JSON: {0}.'.format(str(e)))
_ConvertFieldValuePair(js, message)
return message
@@ -362,7 +290,7 @@ def _ConvertFieldValuePair(js, message):
message.ClearField(field.name)
if not isinstance(value, list):
raise ParseError('repeated field {0} must be in [] which is '
- '{1}'.format(name, value))
+ '{1}.'.format(name, value))
for item in value:
if item is None:
continue
@@ -383,9 +311,9 @@ def _ConvertFieldValuePair(js, message):
else:
raise ParseError(str(e))
except ValueError as e:
- raise ParseError('Failed to parse {0} field: {1}'.format(name, e))
+ raise ParseError('Failed to parse {0} field: {1}.'.format(name, e))
except TypeError as e:
- raise ParseError('Failed to parse {0} field: {1}'.format(name, e))
+ raise ParseError('Failed to parse {0} field: {1}.'.format(name, e))
def _ConvertMessage(value, message):
@@ -399,88 +327,13 @@ def _ConvertMessage(value, message):
ParseError: In case of convert problems.
"""
message_descriptor = message.DESCRIPTOR
- if _IsTimestampMessage(message_descriptor):
- _ConvertTimestampMessage(value, message)
- elif _IsDurationMessage(message_descriptor):
- _ConvertDurationMessage(value, message)
- elif _IsFieldMaskMessage(message_descriptor):
- _ConvertFieldMaskMessage(value, message)
+ if hasattr(message, 'FromJsonString'):
+ message.FromJsonString(value)
elif _IsWrapperMessage(message_descriptor):
_ConvertWrapperMessage(value, message)
else:
_ConvertFieldValuePair(value, message)
-
-def _ConvertTimestampMessage(value, message):
- """Convert a JSON representation into Timestamp message."""
- timezone_offset = value.find('Z')
- if timezone_offset == -1:
- timezone_offset = value.find('+')
- if timezone_offset == -1:
- timezone_offset = value.rfind('-')
- if timezone_offset == -1:
- raise ParseError(
- 'Failed to parse timestamp: missing valid timezone offset.')
- time_value = value[0:timezone_offset]
- # Parse datetime and nanos
- point_position = time_value.find('.')
- if point_position == -1:
- second_value = time_value
- nano_value = ''
- else:
- second_value = time_value[:point_position]
- nano_value = time_value[point_position + 1:]
- date_object = datetime.strptime(second_value, _TIMESTAMPFOMAT)
- 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 '
- '9 fractional digits.'.format(nano_value))
- if nano_value:
- nanos = round(float('0.' + nano_value) * 1e9)
- else:
- nanos = 0
- # Parse timezone offsets
- if value[timezone_offset] == 'Z':
- if len(value) != timezone_offset + 1:
- raise ParseError(
- 'Failed to parse timestamp: invalid trailing data {0}.'.format(value))
- else:
- timezone = value[timezone_offset:]
- pos = timezone.find(':')
- if pos == -1:
- raise ParseError(
- 'Invalid timezone offset value: ' + timezone)
- if timezone[0] == '+':
- seconds += (int(timezone[1:pos])*60+int(timezone[pos+1:]))*60
- else:
- seconds -= (int(timezone[1:pos])*60+int(timezone[pos+1:]))*60
- # Set seconds and nanos
- message.seconds = int(seconds)
- message.nanos = int(nanos)
-
-
-def _ConvertDurationMessage(value, message):
- """Convert a JSON representation into Duration message."""
- if value[-1] != 's':
- raise ParseError(
- 'Duration must end with letter "s": ' + value)
- try:
- duration = float(value[:-1])
- except ValueError:
- raise ParseError(
- 'Couldn\'t parse duration: ' + value)
- message.seconds = int(duration)
- message.nanos = int(round((duration - message.seconds) * 1e9))
-
-
-def _ConvertFieldMaskMessage(value, message):
- """Convert a JSON representation into FieldMask message."""
- for path in value.split(','):
- message.paths.append(path)
-
-
def _ConvertWrapperMessage(value, message):
"""Convert a JSON representation into Wrapper message."""
field = message.DESCRIPTOR.fields_by_name['value']
@@ -512,13 +365,13 @@ def _ConvertMapFieldValue(value, message, field):
value[key], value_field)
-def _ConvertScalarFieldValue(value, field, require_quote=False):
+def _ConvertScalarFieldValue(value, field, require_str=False):
"""Convert a single scalar field value.
Args:
value: A scalar value to convert the scalar field value.
field: The descriptor of the field to convert.
- require_quote: If True, '"' is required for the field value.
+ require_str: If True, the field value must be a str.
Returns:
The converted scalar field value
@@ -531,7 +384,7 @@ def _ConvertScalarFieldValue(value, field, require_quote=False):
elif field.cpp_type in _FLOAT_TYPES:
return _ConvertFloat(value)
elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_BOOL:
- return _ConvertBool(value, require_quote)
+ return _ConvertBool(value, require_str)
elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_STRING:
if field.type == descriptor.FieldDescriptor.TYPE_BYTES:
return base64.b64decode(value)
@@ -561,10 +414,10 @@ def _ConvertInteger(value):
ParseError: If an integer couldn't be consumed.
"""
if isinstance(value, float):
- raise ParseError('Couldn\'t parse integer: {0}'.format(value))
+ raise ParseError('Couldn\'t parse integer: {0}.'.format(value))
- if isinstance(value, _UNICODETYPE) and not _INTEGER.match(value):
- raise ParseError('Couldn\'t parse integer: "{0}"'.format(value))
+ if isinstance(value, text_type) and value.find(' ') != -1:
+ raise ParseError('Couldn\'t parse integer: "{0}".'.format(value))
return int(value)
@@ -572,28 +425,28 @@ def _ConvertInteger(value):
def _ConvertFloat(value):
"""Convert an floating point number."""
if value == 'nan':
- raise ParseError('Couldn\'t parse float "nan", use "NaN" instead')
+ raise ParseError('Couldn\'t parse float "nan", use "NaN" instead.')
try:
# Assume Python compatible syntax.
return float(value)
except ValueError:
# Check alternative spellings.
- if value == '-Infinity':
+ if value == _NEG_INFINITY:
return float('-inf')
- elif value == 'Infinity':
+ elif value == _INFINITY:
return float('inf')
- elif value == 'NaN':
+ elif value == _NAN:
return float('nan')
else:
- raise ParseError('Couldn\'t parse float: {0}'.format(value))
+ raise ParseError('Couldn\'t parse float: {0}.'.format(value))
-def _ConvertBool(value, require_quote):
+def _ConvertBool(value, require_str):
"""Convert a boolean value.
Args:
value: A scalar value to convert.
- require_quote: If True, '"' is required for the boolean value.
+ require_str: If True, value must be a str.
Returns:
The bool parsed.
@@ -601,13 +454,13 @@ def _ConvertBool(value, require_quote):
Raises:
ParseError: If a boolean value couldn't be consumed.
"""
- if require_quote:
+ if require_str:
if value == 'true':
return True
elif value == 'false':
return False
else:
- raise ParseError('Expect "true" or "false", not {0}.'.format(value))
+ raise ParseError('Expected "true" or "false", not {0}.'.format(value))
if not isinstance(value, bool):
raise ParseError('Expected true or false without quotes.')