Protobuf.js: Using field names based on json_name annotation in generated static code

Created on 28 Feb 2018  路  2Comments  路  Source: protobufjs/protobuf.js

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().

Most helpful comment

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.

All 2 comments

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?)

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

terranmoccasin picture terranmoccasin  路  5Comments

andiwonder picture andiwonder  路  3Comments

wesleytodd picture wesleytodd  路  4Comments

asafcombo picture asafcombo  路  4Comments

psaelango picture psaelango  路  4Comments