-
Notifications
You must be signed in to change notification settings - Fork 349
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
Allow serializing enums to integers in JSON output #96
Comments
Hm, I was thinking that protobuf dictated the enums-in-JSON should be strings, but you're right that it says either strings or tags are supported (and we already support parsing either, which I forgot about). So yeah, I think some sort of something like Could you submit a PR? Thanks! |
Just chiming in with some points (after implementing canonical JSON in another project):
|
We do this already, on reading: export function stateEnumFromJSON(object: any): StateEnum {
switch (object) {
case 0:
case "UNKNOWN":
return StateEnum.UNKNOWN;
case 2:
case "ON":
return StateEnum.ON;
case 3:
case "OFF":
return StateEnum.OFF;
case -1:
case "UNRECOGNIZED":
default:
return StateEnum.UNRECOGNIZED;
}
} So I think this issue is about only "Emit enum values as integers instead of strings". This should actually be a really easy change, assuming it's a build-time config option... Supporting protobuf field names and |
Any progress? |
@hijamoya nope, would be great if you're interested in making a PR; the code is around here: https://github.com/stephenh/ts-proto/blob/main/src%2Fenums.ts#L115 |
I created a pull request for this. |
Is it possible to add a build option to serialize enums to integers instead of strings?
Here's what the library currently generates
Here's what I'm trying to achieve
My backend is written in Go and has troubling working with data from my ts client because Go uses integers when generating enums.
The text was updated successfully, but these errors were encountered: