In proto3, we can ensure variables are serialized even if they have default values by using the optional keyword. I want to achieve the same in C, but protobuf-c does not support the optional keyword. Could you help me with what I can do in this situation? I want to serialize a variable with a default value and send the counterpoint.
We serialize the proto file (proto3) and send it via nats as follows.
size_t message_size_ru_configuration = ruconfiguration__get_packed_size(&ru_configuration);
uint8_t* serialized_data_ru_configuration = malloc(message_size_ru_configuration);
ruconfiguration__pack(&ru_configuration, serialized_data_ru_configuration); // Serialize the message
MIS_async_publish("ru.config.set", (const char *)serialized_data_ru_configuration, message_size_ru_configuration);
free(serialized_data_ru_configuration);
We subscribe and receive the data from the sent side, we convert this data to json with message to json as follows.
std::string from_char(data, dataSize);
ulak_mplane_api::RuControlConfiguration deserialized_ru_conf;
deserialized_ru_conf.ParseFromString(from_char);
std::cout<<deserialized_ru_conf.DebugString()<<std::endl;
ru_conf_done = true;
std::string req_message_str;
google::protobuf::util::MessageToJsonString(deserialized_ru_conf, &req_message_str);
std::cout<<req_message_str<<std::endl;
We see that the default values are not serialized and sent in the incoming data, but we also have the following observation: If the parameter is optional, even though it has a default value, it is serialized and the data comes to us, but we are using c language and c is not support optional keyword in proto3 , What can we do in this situtaiton , can you help us ?
ahmet-f is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3