swift-bson icon indicating copy to clipboard operation
swift-bson copied to clipboard

SWIFT-325 BSON init int64 inconsistency. (Not a bug)

Open icyield opened this issue 3 years ago • 1 comments

When initing a BSON from a literal it is an int64 by default, but is easy to change (.int32(5)). However when using JSON the "default" type is int32.

Is there a method to force JSON to give int64 as the default type for small value ints?

We have

/// Initialize a BSON from an integer. On 64-bit systems, this will result in an .int64. On 32-bit systems, /// this will result in an .int32. public init(integerLiteral value: Int) { self.init(value) }

But for JSON we have // Spec requires that we try int32, try int64, then try double for decoding numbers if let int32 = try Int32(fromExtJSON: json, keyPath: keyPath) { self = int32.bson return }

icyield avatar Nov 14 '22 11:11 icyield

Hi @icyield - unfortunately the extended JSON spec requires us to try to fit numeric values into the smallest type possible (see here), so we cannot default numbers to 64-bit integers. If you'd like to force your JSON to decode numbers to 64-bit integers, one option is to decode to a struct that contains Int64s. Otherwise, you may need to update your fields manually.

SWIFT-325 covers adding number encoding/decoding strategies, and I've added a comment for us to consider adding strategies such as the one you've requested to our ExtendedJSONDecoder as well. Feel free to follow that ticket for updates on this work.

isabelatkinson avatar Dec 02 '22 21:12 isabelatkinson