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

ProtoToJsonString : int64 is converted to string even when jstype=JSON_NUMBER #3423

Closed
vivekvelankar opened this issue Jul 27, 2017 · 5 comments

Comments

@vivekvelankar
Copy link

I want the int64 field in one of the proto message as Integer only.
I understand that the default behaviour is to render it as string.

In my case I need it to be rendered as Integer while converting to JsonString.
From the google/protobuf/descriptor.proto specification, I see that jstype can be used.
Even after using [jstype=JS_NUMBER] for my int64 field, i am still getting the string in my proto to Json converison.
protoc version details-
vivek@velankar:~/main$ protoc --version
libprotoc 3.0.0

Verified using the example from the examples directory. I changed the following in proto file-
int64 id = 2 [jstype = JS_NUMBER] ; // Unique ID number for this person.

I added the MessageToJsonString call in the list_people.cc to verify
Proto:
Person ID: 1234
Name: vvvvv

JSON:
string id {"name":"vvvvv","id":"1234"}

I did try to fix it myself by changing RenderInt64 which makes json print int64 as integers unconditionally.
But we should have the option to choose this behaviour using jstype option as mentioned in the documentation.

Let me know if anything else required from my side.
Thanks

@xfxyjwf
Copy link
Contributor

xfxyjwf commented Jul 27, 2017

That behavior (int64 field serialized as string JSON) cannot be changed. There is no plan to add an option for that because a plain int64 value is not exchangeable between different JSON implementations.

You either need to make the receiving side accept string for int64, or change the field type to int32 or double.

@vivekvelankar
Copy link
Author

I made the change in the protobuf library to support that.
Also this is mentioned in the documentation, so I thought its a missing feature.

@xfxyjwf
Copy link
Contributor

xfxyjwf commented Oct 14, 2017

jstype is a legacy option for a different purpose. It's specific to Javascript implementation and does not affect other languages (like MessageToJsonString in C++) at all.

@vivekvelankar
Copy link
Author

Ok. My only reference is the comment/todo written in google/protobuf/descriptor.proto.

@haberman
Copy link
Member

The comment in descriptor.proto seems to clearly say it is only for JavaScript. If there is a misleading comment, please let us know.

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

No branches or pull requests

3 participants