protobuf.js icon indicating copy to clipboard operation
protobuf.js copied to clipboard

toJSON switch enum from int to string

Open ahuangege opened this issue 3 years ago • 10 comments

a message include an enum value, when use toJSON func, the enum will be changed to a string ,however, this is not what i want.

ahuangege avatar Jan 11 '21 04:01 ahuangege

same problem

ShiJianwen avatar Jan 20 '21 08:01 ShiJianwen

I too am having this issue and usually expresses itself as invalid wire type 7 at offset x

sachaw avatar Feb 14 '21 03:02 sachaw

Linking sacaw's issue with the one filed in Meshtastic:

https://github.com/meshtastic/Meshtastic-device/issues/685

mc-hamster avatar Feb 14 '21 04:02 mc-hamster

Same issue. After toJSON(), every values should be same as before when accessing, but it not.

Here's an example.

proto:

enum Type
{
  eGoods = 1;
  eService = 2;
}

message OrderItem
{
  optional Type type = 1;
}

the instance of this message:

OrderItem {
  type: 2,
}

Before toJSON(), access the property type of the instance and it returns 2. But after toJSON(), access the attribute type and it returns 'eService'.

It is a WRONG behavior and causes unexpected result at runtime.

@dcodeIO @alexander-fenster

dreamerblue avatar Mar 06 '21 09:03 dreamerblue

toJSON is a shorthand of toObject:

https://github.com/protobufjs/protobuf.js/blob/da34f43ccd51ad97017e139f137521782f5ef119/src/message.js#L131-L137

https://github.com/protobufjs/protobuf.js/blob/da34f43ccd51ad97017e139f137521782f5ef119/src/message.js#L119-L137

but using util.toJSONOptions. Unless there is a bug somewhere in this (please provide a reproduction), usage is either

  • use toObject instead with the options needed - or -
  • modify util.toJSONOptions to change the behavior of toJSON globally

https://github.com/protobufjs/protobuf.js/blob/da34f43ccd51ad97017e139f137521782f5ef119/src/util/minimal.js#L378-L399

dcodeIO avatar Mar 06 '21 11:03 dcodeIO

toJSON is a shorthand of toObject:

https://github.com/protobufjs/protobuf.js/blob/da34f43ccd51ad97017e139f137521782f5ef119/src/message.js#L131-L137

https://github.com/protobufjs/protobuf.js/blob/da34f43ccd51ad97017e139f137521782f5ef119/src/message.js#L119-L137

but using util.toJSONOptions. Unless there is a bug somewhere in this (please provide a reproduction), usage is either

  • use toObject instead with the options needed - or -
  • modify util.toJSONOptions to change the behavior of toJSON globally

https://github.com/protobufjs/protobuf.js/blob/da34f43ccd51ad97017e139f137521782f5ef119/src/util/minimal.js#L378-L399

Thanks @dcodeIO, maybe it's what we need. Our case is sometimes to set the message instance to response like ctx.body = { orderItem }, so it will call toJSON() and we want the behavior is that enums don't convert to string key.

Is there a way to configure the toJSONOptions globally without modifying the source code? We use the CLI to generate JS static modules to use.

dreamerblue avatar Mar 06 '21 14:03 dreamerblue

Yes, this should be possible with protobuf.util.toJSONOptions = ... or likewise.

dcodeIO avatar Mar 06 '21 15:03 dcodeIO

Nice solution. thanks

dreamerblue avatar Mar 07 '21 01:03 dreamerblue

Try this:

protobuf.util.toJSONOptions ={ longs: String, enums: Number, bytes: String, json: true }

ClaudioVigliarolo avatar Jul 23 '21 18:07 ClaudioVigliarolo

{ longs: String, enums: Number, bytes: String, json: true }

this worked for my case...thanks!

rovolution avatar May 02 '22 19:05 rovolution