def testOneofFields(self): message = json_format_proto3_pb2.TestOneof() # Always print does not affect oneof fields. self.assertEqual(json_format.MessageToJson(message, True), '{}') message.oneof_int32_value = 0 self.assertEqual(json_format.MessageToJson(message, True), '{\n' ' "oneofInt32Value": 0\n' '}') parsed_message = json_format_proto3_pb2.TestOneof() self.CheckParseBack(message, parsed_message)
def testNullValue(self): message = json_format_proto3_pb2.TestOneof() message.oneof_null_value = 0 self.assertEqual(json_format.MessageToJson(message), '{\n "oneofNullValue": null\n}') parsed_message = json_format_proto3_pb2.TestOneof() self.CheckParseBack(message, parsed_message) # Check old format is also accepted new_message = json_format_proto3_pb2.TestOneof() json_format.Parse('{\n "oneofNullValue": "NULL_VALUE"\n}', new_message) self.assertEqual(json_format.MessageToJson(new_message), '{\n "oneofNullValue": null\n}')
def testInvalidOneof(self): message = json_format_proto3_pb2.TestOneof() text = '{"oneofInt32Value": 1, "oneofStringValue": "2"}' self.assertRaisesRegexp( json_format.ParseError, 'Message type "proto3.TestOneof"' ' should not have multiple "oneof_value" oneof fields.', json_format.Parse, text, message)