Python google.protobuf.json_format.Parse() Examples
The following are 30
code examples of google.protobuf.json_format.Parse().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
google.protobuf.json_format
, or try the search function
.
Example #1
Source File: checkpoint.py From calamari with Apache License 2.0 | 6 votes |
def __init__(self, json_path: str, auto_update=True, dry_run=False): self.json_path = json_path if json_path.endswith('.json') else json_path + '.json' self.json_path = os.path.abspath(os.path.expanduser(os.path.expandvars(self.json_path))) self.ckpt_path = os.path.splitext(self.json_path)[0] self.dry_run = dry_run # do not parse as proto, since some parameters might have changed with open(self.json_path, 'r') as f: self.json = json.load(f) self.version = self.json['version'] if 'version' in self.json else 0 if self.version != Checkpoint.VERSION: if auto_update: self.update_checkpoint() else: raise Exception("Version of checkpoint is {} but {} is required. Please upgrade the model or " "set the auto update flag.".format(self.version, Checkpoint.VERSION)) else: print("Checkpoint version {} is up-to-date.".format(self.version)) with open(self.json_path, 'r') as f: self.checkpoint = json_format.Parse(f.read(), CheckpointParams())
Example #2
Source File: json_format_test.py From lambda-packs with MIT License | 6 votes |
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)
Example #3
Source File: login.py From interop with Apache License 2.0 | 6 votes |
def post(self, request): creds = Credentials() try: json_format.Parse(request.body, creds) except Exception as e: return HttpResponseBadRequest( 'Failed to parse request. Error: %s' % str(e)) if not creds.HasField('username') or not creds.HasField('password'): return HttpResponseBadRequest('Request missing fields.') user = authenticate(username=creds.username, password=creds.password) if user is not None and user.is_active: # Successful authentication with active user, login login(request, user) return HttpResponse('User logged in: %s' % user.username) else: # Invalid user credentials, invalid request return HttpResponse( 'Invalid credentials in request. Could not log in.', status=401)
Example #4
Source File: json_format_test.py From lambda-packs with MIT License | 6 votes |
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.')
Example #5
Source File: json_format_test.py From lambda-packs with MIT License | 6 votes |
def testSurrogates(self): # Test correct surrogate handling. message = json_format_proto3_pb2.TestMessage() json_format.Parse('{"stringValue": "\\uD83D\\uDE01"}', message) self.assertEqual(message.string_value, b'\xF0\x9F\x98\x81'.decode('utf-8', 'strict')) # Error case: unpaired high surrogate. self.CheckError( '{"stringValue": "\\uD83D"}', r'Invalid \\uXXXX escape|Unpaired.*surrogate') # Unpaired low surrogate. self.CheckError( '{"stringValue": "\\uDE01"}', r'Invalid \\uXXXX escape|Unpaired.*surrogate')
Example #6
Source File: summary.py From deep_image_model with Apache License 2.0 | 6 votes |
def get_summary_description(node_def): """Given a TensorSummary node_def, retrieve its SummaryDescription. When a Summary op is instantiated, a SummaryDescription of associated metadata is stored in its NodeDef. This method retrieves the description. Args: node_def: the node_def_pb2.NodeDef of a TensorSummary op Returns: a summary_pb2.SummaryDescription Raises: ValueError: if the node is not a summary op. """ if node_def.op != 'TensorSummary': raise ValueError("Can't get_summary_description on %s" % node_def.op) description_str = _compat.as_str_any(node_def.attr['description'].s) summary_description = SummaryDescription() _json_format.Parse(description_str, summary_description) return summary_description
Example #7
Source File: json_format_test.py From coremltools with BSD 3-Clause "New" or "Revised" License | 6 votes |
def testJsonEscapeString(self): message = json_format_proto3_pb2.TestMessage() if sys.version_info[0] < 3: message.string_value = '&\n<\"\r>\b\t\f\\\001/\xe2\x80\xa8\xe2\x80\xa9' else: message.string_value = '&\n<\"\r>\b\t\f\\\001/' message.string_value += (b'\xe2\x80\xa8\xe2\x80\xa9').decode('utf-8') self.assertEqual( json_format.MessageToJson(message), '{\n "stringValue": ' '"&\\n<\\\"\\r>\\b\\t\\f\\\\\\u0001/\\u2028\\u2029"\n}') parsed_message = json_format_proto3_pb2.TestMessage() self.CheckParseBack(message, parsed_message) text = u'{"int32Value": "\u0031"}' json_format.Parse(text, message) self.assertEqual(message.int32_value, 1)
Example #8
Source File: json_format_test.py From coremltools with BSD 3-Clause "New" or "Revised" License | 6 votes |
def testSurrogates(self): # Test correct surrogate handling. message = json_format_proto3_pb2.TestMessage() json_format.Parse('{"stringValue": "\\uD83D\\uDE01"}', message) self.assertEqual(message.string_value, b'\xF0\x9F\x98\x81'.decode('utf-8', 'strict')) # Error case: unpaired high surrogate. self.CheckError( '{"stringValue": "\\uD83D"}', r'Invalid \\uXXXX escape|Unpaired.*surrogate') # Unpaired low surrogate. self.CheckError( '{"stringValue": "\\uDE01"}', r'Invalid \\uXXXX escape|Unpaired.*surrogate')
Example #9
Source File: api.py From recipes-py with Apache License 2.0 | 6 votes |
def add_comment(self, category, message, path, start_line=0, end_line=0, start_char=0, end_char=0, suggestions=()): comment = Data.Comment() comment.category = category comment.message = message comment.path = path comment.start_line = start_line comment.end_line = end_line comment.start_char = start_char comment.end_char = end_char for s in suggestions: # Convert from dict to proto message by way of JSON. json_format.Parse(self.m.json.dumps(s), comment.suggestions.add()) if comment not in self._comments: self._comments.append(comment)
Example #10
Source File: utils_test.py From interop with Apache License 2.0 | 6 votes |
def test_conversion(self): """Tests that it can convert a valid request.""" self.client.force_login(self.superuser) request_proto = interop_admin_api_pb2.GpsConversionRequest() request_proto.latitude = 'N38-08-46.57' request_proto.longitude = 'W076-25-41.39' response = self.client.post(gps_conversion_url, json_format.MessageToJson(request_proto), content_type='application/json') self.assertEqual(200, response.status_code) response_proto = interop_admin_api_pb2.GpsConversionResponse() json_format.Parse(response.content, response_proto) self.assertAlmostEqual(response_proto.latitude, 38.146269, places=5) self.assertAlmostEqual(response_proto.longitude, -76.428164, places=5)
Example #11
Source File: odlcs.py From interop with Apache License 2.0 | 6 votes |
def put(self, request, pk): """Updates the review status of a odlc.""" review_proto = interop_admin_api_pb2.OdlcReview() try: json_format.Parse(request.body, review_proto) except Exception: return HttpResponseBadRequest('Failed to parse review proto.') try: odlc = find_odlc(request, int(pk)) except Odlc.DoesNotExist: return HttpResponseNotFound('Odlc %s not found' % pk) except ValueError as e: return HttpResponseForbidden(str(e)) update_odlc_from_review_proto(odlc, review_proto) odlc.save() return HttpResponse(json_format.MessageToJson( odlc_to_review_proto(odlc)), content_type="application/json")
Example #12
Source File: json_format_test.py From lambda-packs with MIT License | 6 votes |
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)
Example #13
Source File: json_format_test.py From lambda-packs with MIT License | 6 votes |
def testPreservingProtoFieldNames(self): message = json_format_proto3_pb2.TestMessage() message.int32_value = 12345 self.assertEqual('{\n "int32Value": 12345\n}', json_format.MessageToJson(message)) self.assertEqual('{\n "int32_value": 12345\n}', json_format.MessageToJson(message, False, True)) # When including_default_value_fields is True. message = json_format_proto3_pb2.TestTimestamp() self.assertEqual('{\n "repeatedValue": []\n}', json_format.MessageToJson(message, True, False)) self.assertEqual('{\n "repeated_value": []\n}', json_format.MessageToJson(message, True, True)) # Parsers accept both original proto field names and lowerCamelCase names. message = json_format_proto3_pb2.TestMessage() json_format.Parse('{"int32Value": 54321}', message) self.assertEqual(54321, message.int32_value) json_format.Parse('{"int32_value": 12345}', message) self.assertEqual(12345, message.int32_value)
Example #14
Source File: summary.py From lambda-packs with MIT License | 6 votes |
def get_summary_description(node_def): """Given a TensorSummary node_def, retrieve its SummaryDescription. When a Summary op is instantiated, a SummaryDescription of associated metadata is stored in its NodeDef. This method retrieves the description. Args: node_def: the node_def_pb2.NodeDef of a TensorSummary op Returns: a summary_pb2.SummaryDescription Raises: ValueError: if the node is not a summary op. """ if node_def.op != 'TensorSummary': raise ValueError("Can't get_summary_description on %s" % node_def.op) description_str = _compat.as_str_any(node_def.attr['description'].s) summary_description = SummaryDescription() _json_format.Parse(description_str, summary_description) return summary_description
Example #15
Source File: client.py From interop with Apache License 2.0 | 6 votes |
def post_odlc(self, odlc): """POST odlc. Args: odlc: The odlc to upload. Returns: The odlc after upload, which will include the odlc ID and user. Raises: InteropError: Error from server. requests.Timeout: Request timeout. ValueError or AttributeError: Malformed response from server. """ r = self.post('/api/odlcs', data=json_format.MessageToJson(odlc)) odlc = interop_api_pb2.Odlc() json_format.Parse(r.text, odlc) return odlc
Example #16
Source File: client.py From interop with Apache License 2.0 | 6 votes |
def get_odlc(self, odlc_id): """GET odlc. Args: odlc_id: The ID of the odlc to get. Returns: Odlc object with corresponding ID. Raises: InteropError: Error from server. requests.Timeout: Request timeout. ValueError or AttributeError: Malformed response from server. """ r = self.get('/api/odlcs/%d' % odlc_id) odlc = interop_api_pb2.Odlc() json_format.Parse(r.text, odlc) return odlc
Example #17
Source File: client.py From interop with Apache License 2.0 | 6 votes |
def get_odlcs(self, mission=None): """GET odlcs. Args: mission: Optional. ID of a mission to restrict by. Returns: List of Odlc objects which are viewable by user. Raises: InteropError: Error from server. requests.Timeout: Request timeout. ValueError or AttributeError: Malformed response from server. """ url = '/api/odlcs' if mission: url += '?mission=%d' % mission r = self.get(url) odlcs = [] for odlc_dict in r.json(): odlc_proto = interop_api_pb2.Odlc() json_format.Parse(json.dumps(odlc_dict), odlc_proto) odlcs.append(odlc_proto) return odlcs
Example #18
Source File: json_format_test.py From auto-alt-text-lambda-api with MIT License | 6 votes |
def testJsonEscapeString(self): message = json_format_proto3_pb2.TestMessage() if sys.version_info[0] < 3: message.string_value = '&\n<\"\r>\b\t\f\\\001/\xe2\x80\xa8\xe2\x80\xa9' else: message.string_value = '&\n<\"\r>\b\t\f\\\001/' message.string_value += (b'\xe2\x80\xa8\xe2\x80\xa9').decode('utf-8') self.assertEqual( json_format.MessageToJson(message), '{\n "stringValue": ' '"&\\n<\\\"\\r>\\b\\t\\f\\\\\\u0001/\\u2028\\u2029"\n}') parsed_message = json_format_proto3_pb2.TestMessage() self.CheckParseBack(message, parsed_message) text = u'{"int32Value": "\u0031"}' json_format.Parse(text, message) self.assertEqual(message.int32_value, 1)
Example #19
Source File: client.py From interop with Apache License 2.0 | 6 votes |
def get_teams(self): """GET the status of teams. Returns: List of TeamStatus objects for active teams. Raises: InteropError: Error from server. requests.Timeout: Request timeout. ValueError or AttributeError: Malformed response from server. """ r = self.get('/api/teams') teams = [] for team_dict in r.json(): team_proto = interop_api_pb2.TeamStatus() json_format.Parse(json.dumps(team_dict), team_proto) teams.append(team_proto) return teams
Example #20
Source File: rib_api_service.py From SROS-grpc-services with BSD 3-Clause "New" or "Revised" License | 6 votes |
def end_of_rib(self, id=None, table_id=None, json=None): ''' Constructs EndOfRib message. ''' if json: msg = json_format.Parse(json, rib.ModifyRequest.Request()) else: if not id: id = self.request_id() msg = rib.ModifyRequest.Request(id=id, END_OF_RIB=rib.EndOfRib(id=table_id)) self.request[id] = msg return msg.id
Example #21
Source File: json_format_test.py From auto-alt-text-lambda-api with MIT License | 6 votes |
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.')
Example #22
Source File: json_format_test.py From auto-alt-text-lambda-api with MIT License | 6 votes |
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)
Example #23
Source File: predictor.py From sagemaker-python-sdk with Apache License 2.0 | 6 votes |
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())
Example #24
Source File: stats_options.py From data-validation with Apache License 2.0 | 6 votes |
def from_json(cls, options_json: Text) -> 'StatsOptions': """Construct an instance of stats options from a JSON representation. Args: options_json: A JSON representation of the __dict__ attribute of a StatsOptions instance. Returns: A StatsOptions instance constructed by setting the __dict__ attribute to the deserialized value of options_json. """ options_dict = json.loads(options_json) if 'schema_json' in options_dict: options_dict['_schema'] = json_format.Parse(options_dict['schema_json'], schema_pb2.Schema()) del options_dict['schema_json'] options = cls() options.__dict__ = options_dict return options
Example #25
Source File: extended_prediction_dataset.py From calamari with Apache License 2.0 | 6 votes |
def _load_sample(self, sample): gt_txt_path = sample['pred_path'] if gt_txt_path is None: return None, None if gt_txt_path.endswith('.json'): with codecs.open(gt_txt_path, 'r', 'utf-8') as f: p = Parse(str(f.read()), Predictions()) if len(p.predictions) == 0: return None, None voted_p = p.predictions[0] for vp in p.predictions: if vp.id == 'voted': voted_p = vp sample['best_prediction'] = voted_p sample['predictions'] = p return None, voted_p.sentence
Example #26
Source File: json_format_test.py From auto-alt-text-lambda-api with MIT License | 6 votes |
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)
Example #27
Source File: summary.py From auto-alt-text-lambda-api with MIT License | 6 votes |
def get_summary_description(node_def): """Given a TensorSummary node_def, retrieve its SummaryDescription. When a Summary op is instantiated, a SummaryDescription of associated metadata is stored in its NodeDef. This method retrieves the description. Args: node_def: the node_def_pb2.NodeDef of a TensorSummary op Returns: a summary_pb2.SummaryDescription Raises: ValueError: if the node is not a summary op. """ if node_def.op != 'TensorSummary': raise ValueError("Can't get_summary_description on %s" % node_def.op) description_str = _compat.as_str_any(node_def.attr['description'].s) summary_description = SummaryDescription() _json_format.Parse(description_str, summary_description) return summary_description
Example #28
Source File: upload_odlcs.py From interop with Apache License 2.0 | 6 votes |
def upload_odlc(client, odlc_file, image_file): """Upload a single odlc to the server Args: client: interop.Client connected to the server odlc_file: Path to file containing odlc details in the Object File Format. image_file: Path to odlc thumbnail. May be None. """ odlc = interop_api_pb2.Odlc() with open(odlc_file) as f: json_format.Parse(f.read(), odlc) logger.info('Uploading odlc %s: %r' % (odlc_file, odlc)) odlc = client.post_odlc(odlc).result() if image_file: logger.info('Uploading odlc thumbnail %s' % image_file) with open(image_file, 'rb') as img: client.post_odlc_image(odlc.id, img.read()).result() else: logger.warning('No thumbnail for odlc %s' % odlc_file)
Example #29
Source File: json_format_test.py From coremltools with BSD 3-Clause "New" or "Revised" License | 5 votes |
def CheckParseBack(self, message, parsed_message): json_format.Parse(json_format.MessageToJson(message), parsed_message) self.assertEqual(message, parsed_message)
Example #30
Source File: json_format_test.py From coremltools with BSD 3-Clause "New" or "Revised" License | 5 votes |
def CheckError(self, text, error_message): message = json_format_proto3_pb2.TestMessage() self.assertRaisesRegexp( json_format.ParseError, error_message, json_format.Parse, text, message)