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

Anyway to force deocde fixed64/uint64 as string ? #591

Closed
k8w opened this issue Dec 29, 2016 · 6 comments
Closed

Anyway to force deocde fixed64/uint64 as string ? #591

k8w opened this issue Dec 29, 2016 · 6 comments
Labels

Comments

@k8w
Copy link

k8w commented Dec 29, 2016

Notice that now it's decoded as long Object.
Anyway to force it to be as string ?
Sometimes string is more useful.

For example, when a uint64 is used as id, string is more convenient to compare and use.

@dcodeIO
Copy link
Member

dcodeIO commented Dec 29, 2016

Long objects implement toString([radix=10]) for this purpose (see).

Note that you could also just compare any Long object using == instead of === to a string but this performs the conversion operation for each such comparison.

@k8w
Copy link
Author

k8w commented Dec 29, 2016

Of course long.js is useful.

But actually, Long is JSON-unfriendly.

Say I have a service that get some message from a grpc service, and return a json to browser.
If fixed64 and uint64 is Long, it is not convenient to transform everyone to string.

Especially at some cases when protobuf message need to convert to JSON, and transport to browser...
And they are not used for calculate

@dcodeIO
Copy link
Member

dcodeIO commented Dec 29, 2016

You know that there is Message#asJSON, which supports { longs: String } as an option, right?

The reason why longs aren't just strings by default is that every conversion from bits to strings and vice-versa has a cost, and you really don't want this to happen implicitly all the time.

@k8w
Copy link
Author

k8w commented Dec 29, 2016

Thanks, asJSON really solve this perfectly.

Sorry for that I really don't know this before, maybe it's because never find this function in generated static js files.

Maybe consider to include asJSON in generated js ?

@dcodeIO
Copy link
Member

dcodeIO commented Dec 29, 2016

There is no asJSON/from for static code, yet. Related: #588

@dcodeIO
Copy link
Member

dcodeIO commented Jan 3, 2017

Closing this for now as 6.4.0, which is now released, should solve the issue. Feel free to reopen the issue if necessary.

@dcodeIO dcodeIO closed this as completed Jan 3, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants