google/protobuf.dart

toProto3Json converts Int64 values to strings

njlawton opened this issue · 1 comments

When calling the toProto3Json, 64 bit integers are converted to Strings.

case PbFieldType._INT64_BIT:
case PbFieldType._SINT64_BIT:
case PbFieldType._SFIXED64_BIT:
case PbFieldType._FIXED64_BIT:
return fieldValue.toString();

This is an issue when storing protos in databases through json APIs e.g. Firestore. They are written as strings so you cannot use a numeric index. I quite often use Int64 fields for timestamps in milliseconds / microseconds which do not fit in a 32 bit range.

Is it possible to keep int64 fields as numbers in toProto3Json?

If not, can we document this in the function comment? I feel this is very divergent from the caller would expect.

This is according to specification: https://protobuf.dev/programming-guides/proto3/#json:~:text=strings%20are%20accepted.-,int64,-%2C%20fixed64%2C%20uint64

Javascript numbers are 64 bit floats. They can only accurately represent integers up to 52 bits.

Not sure we want to point this out in the function comment, as this is the specified behavior.