(self, field, value)
return value
Converts field value according to Proto3 JSON Specification.
Converts field value according to Proto3 JSON Specification.
Converts field value according to Proto3 JSON Specification .
def _FieldToJsonObject(self, field, value):
    """Converts field value according to Proto3 JSON Specification."""
    if field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_MESSAGE:
      return self._MessageToJsonObject(value)
    elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_ENUM:
      enum_value = field.enum_type.values_by_number.get(value, None)
      if enum_value is not None:
        return enum_value.name
      else:
        raise SerializeToJsonError('Enum field contains an integer value '
                                   'which can not mapped to an enum value.')
    elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_STRING:
      if field.type == descriptor.FieldDescriptor.TYPE_BYTES:
        # Use base64 Data encoding for bytes
        return base64.b64encode(value).decode('utf-8')
      else:
        return value
    elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_BOOL:
      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 _NEG_INFINITY
        else:
          return _INFINITY
      if math.isnan(value):
        return _NAN
    return value
def _FieldToJsonObject ( self , field , value ) : if field . cpp_type == descriptor . FieldDescriptor . CPPTYPE_MESSAGE : return self . _MessageToJsonObject ( value ) elif field . cpp_type == descriptor . FieldDescriptor . CPPTYPE_ENUM : enum_value = field . enum_type . values_by_number . get ( value , None ) if enum_value is not None : return enum_value . name else : raise SerializeToJsonError ( 'Enum field contains an integer value ' 'which can not mapped to an enum value.' ) elif field . cpp_type == descriptor . FieldDescriptor . CPPTYPE_STRING : if field . type == descriptor . FieldDescriptor . TYPE_BYTES : # Use base64 Data encoding for bytes return base64 . b64encode ( value ) . decode ( 'utf-8' ) else : return value elif field . cpp_type == descriptor . FieldDescriptor . CPPTYPE_BOOL : 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 _NEG_INFINITY else : return _INFINITY if math . isnan ( value ) : return _NAN return value