[BUG][C++] MessageToJsonString always convert enum value 0 to string
DESCRIPTION
Hi all, I get into a problem with MessageToJsonString API when I am trying to convert a message object which contains an enum field into json string. I have set JsonOptions::always_print_enums_as_ints to true. When the enum field value is greater than 0 thing goes well, json value of this field is an integer . However when the enum filed value is 0, the json value will become a string.
EXAMPLE
Part of my proto file is as following:
enum E_GENDER
{
E_GENDER_MALE = 0;
E_GENDER_FEMALE = 1;
}
message PlayerInfo
{
E_GENDER gender = 1;
}
Part of my convertion code is as following:
std::string ConvertMessageToJson(const Message* poMsg)
{
std::string strMsg;
google::protobuf::util::JsonOptions stOpt;
stOpt.always_print_enums_as_ints = true;
stOpt.always_print_primitive_fields = true;
stOpt.preserve_proto_field_names = true;
google::protobuf::util::MessageToJsonString(*poMsg, &strMsg, stOpt);
return strMsg;
}
When field gender is E_GENDER_FEMALE, the result is what I want:
{"gender":1}
When field gender is E_GENDER_MALE, the result is:
{"gender":"E_GENDER_MALE"}
ENVIRONMENT
- os: Debian 8
- language: C++
- proto language: proto3
- protoc version: libprotoc 3.5.1
Hey, I am a beginner here and would like to contribute to protobuf. I've successfully compiled a few .proto files for c++. Any help on how to solve this bug is appreciated. Thanks
@vamsibulusu That would be great if you want to take a shot at fixing this bug. You can start here and try to figure out why MessageToJsonString is printing enums as strings despite the always_print_enums_as_ints option in JsonOptions.
I just tried this case with libprotobuf 15, it outputs {"gender":1} as expected. What's your libprotobuf version for testing? I'll close this for now. If it still has issue, feel free to reopen this.
@BSBandme Thanks for your attention. But have you tried E_GENDER_MALE which value is 0 ? In my case, the enum value greater than 0 is alright but when it comes to 0, it will be converted as string instead of integer.
Sorry that I misread your comment before. But I also tried E_GENDER_MALE, it returned {"gender":0}as expect.
My testing code is
#include "google/protobuf/util/json_util.h"
#include "test.pb.h"
using google::protobuf::util::JsonOptions;
using google::protobuf::Message;
std::string ConvertMessageToJson(const Message* poMsg)
{
std::string strMsg;
google::protobuf::util::JsonOptions stOpt;
stOpt.always_print_enums_as_ints = true;
stOpt.always_print_primitive_fields = true;
stOpt.preserve_proto_field_names = true;
google::protobuf::util::MessageToJsonString(*poMsg, &strMsg, stOpt);
return strMsg;
}
int main() {
std::string str_msg;
JsonOptions st_opt;
issue4661::PlayerInfo player_info;
player_info.set_gender(issue4661::E_GENDER::E_GENDER_MALE);
std::cout << ConvertMessageToJson(&player_info) << std::endl;
return 0;
}
proto file is:
syntax = "proto3";
package issue4661;
enum E_GENDER {
E_GENDER_MALE = 0;
E_GENDER_FEMALE = 1;
}
message PlayerInfo {
E_GENDER gender = 1;
}
Would you please describe more, like how you set the field to E_GENDER_MALE?
Hi, a couple of weeks ago, I discovered the same bug that default enums (i.e. 0) are always mapped to a string even if mapping to integers is requested. I still can reproduce the bug with the test code posted above: {"gender":"E_GENDER_MALE"}
I'm using protobuf 3.5.1 (C++)
Have you tried to update the protobuf to some higher version? I think at least the master branch already solved this bug.
As in my project we use the libprotobuf.a static library built by others, so I don't know the exact version I used. Maybe it's because of our protobuf version is too low. However I still would like to post my code for you to judge. @BSBandme
proto define
enum GENDER_TYPE
{
GENDER_TYPE_INVALID= 0; // We add an Invalid item to avoid this problem
GENDER_TYPE_MALE = 1;
GENDER_TYPE_FEMALE = 2;
}
message PlayerSimple{
int32 playerID = 1;
string playerName = 2;
GENDER_TYPE gender = 3;
repeated int32 apperance = 4;
repeated int32 wear = 5;
}
test code
#include <google/protobuf/util/json_util.h>
#include "proto/csprotocol.pb.h"
using namespace TheTrailProto;
std::string ConvertMessageToJson(const google::protobuf::Message* poMsg)
{
std::string strMsg;
google::protobuf::util::JsonOptions stOpt;
stOpt.always_print_enums_as_ints = true;
stOpt.always_print_primitive_fields = true;
stOpt.preserve_proto_field_names = true;
google::protobuf::util::MessageToJsonString(*poMsg, &strMsg, stOpt);
return strMsg;
}
int main()
{
PlayerSimple playerSimple;
playerSimple.set_gender(GENDER_TYPE_INVALID);
std::cout << ConvertMessageToJson(&playerSimple) << std::endl;
playerSimple.set_gender(GENDER_TYPE_MALE);
std::cout << ConvertMessageToJson(&playerSimple) << std::endl;
return 0;
}
print result
{"playerID":0,"playerName":"","gender":"GENDER_TYPE_INVALID","apperance":[],"wear":[]} {"playerID":0,"playerName":"","gender":1,"apperance":[],"wear":[]}
I think this has been fix in #4395, so please update your protobuf version to one that newer than Mar 26th will fix this error.
Thanks for your help! we'll try to update to 3.6.0 @BSBandme