diff --git a/src/converter.js b/src/converter.js index 25eed3d45..d375682f8 100644 --- a/src/converter.js +++ b/src/converter.js @@ -250,7 +250,7 @@ converter.toObject = function toObject(mtype) { ("d%s=o.enums===String?%j:%j", prop, field.resolvedType.valuesById[field.typeDefault], field.typeDefault); else if (field.long) gen ("if(typeof BigInt!==\"undefined\"&&o.longs===BigInt){") - ("d%s=BigInt(%s)", prop, field.typeDefault.toString()) + ("d%s=BigInt(%j)", prop, field.typeDefault.toString()) ("}else if(util.Long){") ("var n=new util.Long(%i,%i,%j)", field.typeDefault.low, field.typeDefault.high, field.typeDefault.unsigned) ("d%s=o.longs===String?n.toString():o.longs===Number?n.toNumber():n", prop) diff --git a/tests/api_converters.js b/tests/api_converters.js index b30560ef3..73e0adfff 100644 --- a/tests/api_converters.js +++ b/tests/api_converters.js @@ -106,6 +106,7 @@ tape.test("converters", function(test) { var buf = protobuf.util.newBuffer(3); buf[0] = buf[1] = buf[2] = 49; // "111" var msg = Message.create({ + // This number was chosen to be > 2^63 and < 2^64 uint64Val: protobuf.util.Long.fromString("11000000000000000001", true), uint64Repeated: [2, 3], bytesVal: buf, @@ -113,6 +114,7 @@ tape.test("converters", function(test) { enumVal: 2, enumRepeated: [1, 100, 2], int64Map: { + // These numbers were chosen to be < Number.MIN_SAFE_INTEGER a: protobuf.util.Long.fromString("-200000000000000001"), b: protobuf.util.Long.fromString("-300000000000000001") }