Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JsonStringToMessage fail if there is "bytes" member in proto file #5719

Closed
s894330 opened this issue Feb 13, 2019 · 4 comments
Closed

JsonStringToMessage fail if there is "bytes" member in proto file #5719

s894330 opened this issue Feb 13, 2019 · 4 comments
Assignees

Comments

@s894330
Copy link

s894330 commented Feb 13, 2019

What version of protobuf and what language are you using?
Version: v3.6.1
Language: C++

What operating system (Linux, Windows, ...) and version?
Ubuntu 1404.5
What runtime / compiler are you using (e.g., python version or gcc version)
g++ 4.8.4
What did you do?
Steps to reproduce the behavior:

  1. Create simple test.proto like bellow
syntax = "proto2";
package tutorial;
message MyMessage {
  optional bytes msg_id = 1;  //this will cause JsonStringToMessage fail 
//  optional uint32 msg_id = 1;
}
  1. Create simple test.cc like bellow
#include <iostream>
#include <string>
#include "google/protobuf/util/json_util.h"
#include "test.pb.h"

using google::protobuf::util::JsonStringToMessage;
using namespace std;

int main(int argc, char* argv[]) {
  GOOGLE_PROTOBUF_VERIFY_VERSION;

  const string test_msg = "{\"msg_id\":\"1\"}";
  tutorial::MyMessage message;

  if (JsonStringToMessage(test_msg, &message).ok()) {
      cout << "parse ok, id: " << message.msg_id() <<  endl;
  } else {
      cout << "Failed to parse msg from json" << endl;
  }
  google::protobuf::ShutdownProtobufLibrary();

  return 0;
}
  1. compile and execute it
  2. See error "Failed to parse msg from json"

What did you expect to see
If everything OK, we will see "parse ok, id: 1"
What did you see instead?
I see error "Failed to parse msg from json"

error_example.zip

@s894330
Copy link
Author

s894330 commented Feb 13, 2019

If I change the proto member "msg_id" 's type from bytes to uint32, it will execute success。
So please help to double check to see if this is bug or not

thank you very much :-)

@s894330
Copy link
Author

s894330 commented Feb 25, 2019

I use MessageToJsonString first to see what is the real output when set msg_id to "1"
And I see it is encoded to "MQ=="
And also, this string "MQ==" can be passed by JsonStringToMessage()

#include <iostream>
#include <string>
#include "google/protobuf/util/json_util.h"
#include "test.pb.h"

using google::protobuf::util::MessageToJsonString;
using google::protobuf::util::JsonStringToMessage;
using google::protobuf::util::JsonPrintOptions;
using namespace std;

int main(int argc, char* argv[]) {
  GOOGLE_PROTOBUF_VERIFY_VERSION;

  tutorial::MyMessage message, message2;
  message.set_msg_id("1");
  string msg;
  string json = "{\"msg_id\":\"MQ==\"}";
  JsonPrintOptions options;
  options.preserve_proto_field_names = true;
  if (MessageToJsonString(message, &msg, options).ok()) {
      cout << "Json msg: " << msg << endl;
  } else {
      cout << "failed to message to jsonstring" << endl;
  }


  cout << "json string: " << json << endl;
  if (JsonStringToMessage(json, &message2).ok()) {
	cout << "id: " << message2.msg_id() << endl;
  } else {
	cout << "failled to jsonstring to message" << endl;
  }

  google::protobuf::ShutdownProtobufLibrary();

  return 0;
}

I think this is a special mechanism in the protobuf when convert string to bytes

If I want to implement this special mechanism by my self without using protobuf. i.e. I can generate acceptable Json message byte string (from byte[]) which can let JsonStringToMessage() pass.
How can I do that in C#?

thank you :-)

@s894330
Copy link
Author

s894330 commented Mar 4, 2019

After further study, I realize the encode which protobuf used for bytes is base64
And I can use " System.Convert.ToBase64String()" in C# to convert string for JsonStringToMessage()'s input

I think this is not bug, so I will go to close this issue, thank you

@s894330 s894330 closed this as completed Mar 4, 2019
@joye92
Copy link

joye92 commented Oct 29, 2019

If string directly converts to proto bytes , could the result be error?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants