Skip to content

Using field names based on json_name annotation in generated static code #992

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

Open
cusher opened this issue Feb 28, 2018 · 2 comments · May be fixed by #1825
Open

Using field names based on json_name annotation in generated static code #992

cusher opened this issue Feb 28, 2018 · 2 comments · May be fixed by #1825

Comments

@cusher
Copy link

cusher commented Feb 28, 2018

protobuf.js version: 6.8.0

I've run into an issue a few times when parsing JSON data into the statically generated classes, when the fields in the JSON data are named using using the json_name annotation for the field names instead of the normal field names.

For example, if a proto file looks like:

import "google/api/annotations.proto";
import "otherstuff.proto";
message MyClass {
    repeated Thing thing = 1 [json_name="my_things"];
}

And you are parsing in the object like

const myObject = MyClass.fromObject(someObjectParsedFromJsonData);

the object field currently needs to be named thing instead of my_things.

It would be nice if the alternate naming scheme could be used directly, either through a command line switch or using another method like fromJson() in place of fromObject().

@dcodeIO
Copy link
Member

dcodeIO commented Feb 28, 2018

The option you are looking for is keepCase. It's available in Root.load, parse and pbjs (--keep-case). If it's not specified, fields are converted to camelCase. (Or is there something broken?)

@cusher
Copy link
Author

cusher commented Feb 28, 2018

I'm already using --keep-case. Afaict that only, as you indicate, tells it not to convert to camel case. I'm looking for an option to parse in fields where the json_name is different altogether like in my example.

@michaelbgreen michaelbgreen linked a pull request Oct 12, 2022 that will close this issue
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

Successfully merging a pull request may close this issue.

2 participants