本文整理汇总了Python中google.protobuf.json_format.ParseError方法的典型用法代码示例。如果您正苦于以下问题:Python json_format.ParseError方法的具体用法?Python json_format.ParseError怎么用?Python json_format.ParseError使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类google.protobuf.json_format
的用法示例。
在下文中一共展示了json_format.ParseError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testParseEnumValue
# 需要导入模块: from google.protobuf import json_format [as 别名]
# 或者: from google.protobuf.json_format import ParseError [as 别名]
def testParseEnumValue(self):
message = json_format_proto3_pb2.TestMessage()
text = '{"enumValue": 0}'
json_format.Parse(text, message)
text = '{"enumValue": 1}'
json_format.Parse(text, message)
self.CheckError(
'{"enumValue": "baz"}',
'Failed to parse enumValue field: Invalid enum value baz '
'for enum type proto3.EnumType.')
# Proto3 accepts numeric unknown enums.
text = '{"enumValue": 12345}'
json_format.Parse(text, message)
# Proto2 does not accept unknown enums.
message = unittest_pb2.TestAllTypes()
self.assertRaisesRegex(
json_format.ParseError,
'Failed to parse optionalNestedEnum field: Invalid enum value 12345 '
'for enum type protobuf_unittest.TestAllTypes.NestedEnum.',
json_format.Parse, '{"optionalNestedEnum": 12345}', message)
示例2: testInvalidIntegerValue
# 需要导入模块: from google.protobuf import json_format [as 别名]
# 或者: from google.protobuf.json_format import ParseError [as 别名]
def testInvalidIntegerValue(self):
message = json_format_proto3_pb2.TestMessage()
text = '{"int32Value": 0x12345}'
self.assertRaises(json_format.ParseError,
json_format.Parse, text, message)
self.CheckError('{"int32Value": 1.5}',
'Failed to parse int32Value field: '
'Couldn\'t parse integer: 1.5.')
self.CheckError('{"int32Value": 012345}',
(r'Failed to load JSON: Expecting \'?,\'? delimiter: '
r'line 1.'))
self.CheckError('{"int32Value": " 1 "}',
'Failed to parse int32Value field: '
'Couldn\'t parse integer: " 1 ".')
self.CheckError('{"int32Value": "1 "}',
'Failed to parse int32Value field: '
'Couldn\'t parse integer: "1 ".')
self.CheckError('{"int32Value": 12345678901234567890}',
'Failed to parse int32Value field: Value out of range: '
'12345678901234567890.')
self.CheckError('{"uint32Value": -1}',
'Failed to parse uint32Value field: '
'Value out of range: -1.')
示例3: testInvalidAny
# 需要导入模块: from google.protobuf import json_format [as 别名]
# 或者: from google.protobuf.json_format import ParseError [as 别名]
def testInvalidAny(self):
message = any_pb2.Any()
text = '{"@type": "type.googleapis.com/google.protobuf.Int32Value"}'
self.assertRaisesRegex(
KeyError,
'value',
json_format.Parse, text, message)
text = '{"value": 1234}'
self.assertRaisesRegex(
json_format.ParseError,
'@type is missing when parsing any message.',
json_format.Parse, text, message)
text = '{"@type": "type.googleapis.com/MessageNotExist", "value": 1234}'
self.assertRaisesRegex(
TypeError,
'Can not find message descriptor by type_url: '
'type.googleapis.com/MessageNotExist.',
json_format.Parse, text, message)
# Only last part is to be used: b/25630112
text = (r'{"@type": "incorrect.googleapis.com/google.protobuf.Int32Value",'
r'"value": 1234}')
json_format.Parse(text, message)
示例4: testInvalidMap
# 需要导入模块: from google.protobuf import json_format [as 别名]
# 或者: from google.protobuf.json_format import ParseError [as 别名]
def testInvalidMap(self):
message = json_format_proto3_pb2.TestMap()
text = '{"int32Map": {"null": 2, "2": 3}}'
self.assertRaisesRegexp(
json_format.ParseError,
'Failed to parse int32Map field: invalid literal',
json_format.Parse, text, message)
text = '{"int32Map": {1: 2, "2": 3}}'
self.assertRaisesRegexp(
json_format.ParseError,
(r'Failed to load JSON: Expecting property name'
r'( enclosed in double quotes)?: line 1'),
json_format.Parse, text, message)
text = '{"boolMap": {"null": 1}}'
self.assertRaisesRegexp(
json_format.ParseError,
'Failed to parse boolMap field: Expected "true" or "false", not null.',
json_format.Parse, text, message)
if sys.version_info < (2, 7):
return
text = r'{"stringMap": {"a": 3, "\u0061": 2}}'
self.assertRaisesRegexp(
json_format.ParseError,
'Failed to load JSON: duplicate key a',
json_format.Parse, text, message)
示例5: testInvalidAny
# 需要导入模块: from google.protobuf import json_format [as 别名]
# 或者: from google.protobuf.json_format import ParseError [as 别名]
def testInvalidAny(self):
message = any_pb2.Any()
text = '{"@type": "type.googleapis.com/google.protobuf.Int32Value"}'
self.assertRaisesRegexp(
KeyError,
'value',
json_format.Parse, text, message)
text = '{"value": 1234}'
self.assertRaisesRegexp(
json_format.ParseError,
'@type is missing when parsing any message.',
json_format.Parse, text, message)
text = '{"@type": "type.googleapis.com/MessageNotExist", "value": 1234}'
self.assertRaisesRegexp(
TypeError,
'Can not find message descriptor by type_url: '
'type.googleapis.com/MessageNotExist.',
json_format.Parse, text, message)
# Only last part is to be used: b/25630112
text = (r'{"@type": "incorrect.googleapis.com/google.protobuf.Int32Value",'
r'"value": 1234}')
json_format.Parse(text, message)
示例6: __call__
# 需要导入模块: from google.protobuf import json_format [as 别名]
# 或者: from google.protobuf.json_format import ParseError [as 别名]
def __call__(self, stream, content_type):
"""
Args:
stream:
content_type:
"""
try:
data = stream.read()
finally:
stream.close()
for possible_response in _possible_responses():
try:
return protobuf_to_dict(json_format.Parse(data, possible_response()))
except (UnicodeDecodeError, DecodeError, json_format.ParseError):
# given that the payload does not have the response type, there no way to infer
# the response without keeping state, so I'm iterating all the options.
pass
return json.loads(data.decode())
示例7: read_user_config
# 需要导入模块: from google.protobuf import json_format [as 别名]
# 或者: from google.protobuf.json_format import ParseError [as 别名]
def read_user_config(artman_user_config_path):
"""Parse and return artman config"""
config_pb = UserConfig()
artman_user_config_path = os.path.expanduser(artman_user_config_path)
if not os.path.isfile(artman_user_config_path):
logger.warn(
'No artman user config defined. Use the default one for this '
'execution. Run `configure-artman` to set up user config.')
return config_pb
try:
with io.open(artman_user_config_path, 'r', encoding='UTF-8') as f:
# Convert yaml into json file as protobuf python load support
# parsing of protobuf in json or text format, not yaml.
json_string = json.dumps(yaml.load(f, Loader=yaml.FullLoader))
json_format.Parse(json_string, config_pb)
except (json_format.ParseError, yaml.parser.ParserError):
logger.error(INVALID_USER_CONFIG_ERROR_MESSAGE_FORMAT % artman_user_config_path)
raise
return config_pb
示例8: _parse
# 需要导入模块: from google.protobuf import json_format [as 别名]
# 或者: from google.protobuf.json_format import ParseError [as 别名]
def _parse(artman_yaml_path):
"""Parse artman yaml config into corresponding protobuf."""
if not os.path.exists(artman_yaml_path):
raise ValueError(CONFIG_NOT_FOUND_ERROR_MESSAGE_FORMAT % artman_yaml_path)
try:
with io.open(artman_yaml_path, 'r', encoding='UTF-8') as f:
# Convert yaml into json file as protobuf python load support paring of
# protobuf in json or text format, not yaml.
artman_config_json_string = json.dumps(yaml.load(f, Loader=yaml.FullLoader))
config_pb = Config()
json_format.Parse(artman_config_json_string, config_pb)
except (json_format.ParseError, yaml.parser.ParserError):
logger.error(INVALID_CONFIG_ERROR_MESSAGE_FORMAT % artman_yaml_path)
raise
return config_pb
示例9: _get_evaluation_result
# 需要导入模块: from google.protobuf import json_format [as 别名]
# 或者: from google.protobuf.json_format import ParseError [as 别名]
def _get_evaluation_result(self, request):
run = request.args.get('run')
try:
run = six.ensure_text(run)
except (UnicodeDecodeError, AttributeError):
pass
data = []
try:
eval_result_output_dir = six.ensure_text(
self._multiplexer.Tensors(run, FairnessIndicatorsPlugin.plugin_name)
[0].tensor_proto.string_val[0])
eval_result = tfma.load_eval_result(output_path=eval_result_output_dir)
# TODO(b/141283811): Allow users to choose different model output names
# and class keys in case of multi-output and multi-class model.
data = widget_view.convert_slicing_metrics_to_ui_input(
eval_result.slicing_metrics)
except (KeyError, json_format.ParseError) as error:
logging.info('Error while fetching evaluation data, %s', error)
return http_util.Respond(request, data, content_type='application/json')
示例10: _get_evaluation_result_from_remote_path
# 需要导入模块: from google.protobuf import json_format [as 别名]
# 或者: from google.protobuf.json_format import ParseError [as 别名]
def _get_evaluation_result_from_remote_path(self, request):
evaluation_output_path = request.args.get('evaluation_output_path')
try:
evaluation_output_path = six.ensure_text(evaluation_output_path)
except (UnicodeDecodeError, AttributeError):
pass
try:
eval_result = tfma.load_eval_result(
os.path.dirname(evaluation_output_path),
output_file_format=self._get_output_file_format(
evaluation_output_path))
data = widget_view.convert_slicing_metrics_to_ui_input(
eval_result.slicing_metrics)
except (KeyError, json_format.ParseError) as error:
logging.info('Error while fetching evaluation data, %s', error)
data = []
return http_util.Respond(request, data, content_type='application/json')
示例11: test_invalid_options
# 需要导入模块: from google.protobuf import json_format [as 别名]
# 或者: from google.protobuf.json_format import ParseError [as 别名]
def test_invalid_options():
with pytest.raises(ParseError) as type_err:
export(Node([Field('kott',
options={'clauber': UNKNOWN})]))
type_err.match('Unexpected type for Value message')
with pytest.raises(ParseError) as item_type_err:
export(Node([Field('puerco',
options={'bayat': [1, UNKNOWN, 3]})]))
item_type_err.match('Unexpected type for Value message')
示例12: CheckError
# 需要导入模块: from google.protobuf import json_format [as 别名]
# 或者: from google.protobuf.json_format import ParseError [as 别名]
def CheckError(self, text, error_message):
message = json_format_proto3_pb2.TestMessage()
self.assertRaisesRegex(
json_format.ParseError,
error_message,
json_format.Parse, text, message)
示例13: testInvalidMap
# 需要导入模块: from google.protobuf import json_format [as 别名]
# 或者: from google.protobuf.json_format import ParseError [as 别名]
def testInvalidMap(self):
message = json_format_proto3_pb2.TestMap()
text = '{"int32Map": {"null": 2, "2": 3}}'
self.assertRaisesRegex(
json_format.ParseError,
'Failed to parse int32Map field: invalid literal',
json_format.Parse, text, message)
text = '{"int32Map": {1: 2, "2": 3}}'
self.assertRaisesRegex(
json_format.ParseError,
(r'Failed to load JSON: Expecting property name'
r'( enclosed in double quotes)?: line 1'),
json_format.Parse, text, message)
text = '{"boolMap": {"null": 1}}'
self.assertRaisesRegex(
json_format.ParseError,
'Failed to parse boolMap field: Expected "true" or "false", not null.',
json_format.Parse, text, message)
if sys.version_info < (2, 7):
return
text = r'{"stringMap": {"a": 3, "\u0061": 2}}'
self.assertRaisesRegex(
json_format.ParseError,
'Failed to load JSON: duplicate key a',
json_format.Parse, text, message)
text = r'{"stringMap": 0}'
self.assertRaisesRegex(
json_format.ParseError,
'Failed to parse stringMap field: Map field string_map must be '
'in a dict which is 0.',
json_format.Parse, text, message)
示例14: testInvalidOneof
# 需要导入模块: from google.protobuf import json_format [as 别名]
# 或者: from google.protobuf.json_format import ParseError [as 别名]
def testInvalidOneof(self):
message = json_format_proto3_pb2.TestOneof()
text = '{"oneofInt32Value": 1, "oneofStringValue": "2"}'
self.assertRaisesRegex(
json_format.ParseError,
'Message type "proto3.TestOneof"'
' should not have multiple "oneof_value" oneof fields.',
json_format.Parse, text, message)
示例15: testInvalidListValue
# 需要导入模块: from google.protobuf import json_format [as 别名]
# 或者: from google.protobuf.json_format import ParseError [as 别名]
def testInvalidListValue(self):
message = json_format_proto3_pb2.TestListValue()
text = '{"value": 1234}'
self.assertRaisesRegex(
json_format.ParseError,
r'Failed to parse value field: ListValue must be in \[\] which is 1234',
json_format.Parse, text, message)