tayloraswift / swift-json

high-performance json parsing and encoding for server applications
https://swiftinit.org/docs/swift-json
Apache License 2.0
75 stars 5 forks source link

Why a custom `Decimal` instead of using the built-in type? #63

Closed davdroman closed 1 year ago

davdroman commented 1 year ago
struct Decimal:Codable  
{
    let units:Int 
    let places:Int 
}

I'm trying to wrap my head around the need for having a user-defined Decimal type for decoding, as opposed to leveraging Swift's own Decimal type. It's rather odd to me, a user of the library, having introduce an arbitrary abstraction to be able to decode something into a basic numeric type.

tayloraswift commented 1 year ago

swift does not have a Decimal type, Foundation does, and JSON is a Foundation-free framework. (this is very important for keeping binary size down.)

and the Foundation Decimal type is not compliant with the IEEE standard, so its use is limited. swift-json defines a very basic decimal type in order to allow users to implement their own lossless decimal handling, if needed. however, in most cases this should not be needed, because JSON.Number vends a variety of helper APIs to convert itself into basic numeric types.

davdroman commented 1 year ago

swift does not have a Decimal type, Foundation does, and JSON is a Foundation-free framework. (this is very important for keeping binary size down.)

This is a very noble goal that I wholeheartedly support. However most (99%+) projects out there heavily rely on Foundation and its convenient types. I think a sweet spot could be to include conditional checks like:

#if canImport(Foundation)
// handy foundation extensions such as first-party Decimal conversion for JSON.Number
#endif

If this is too heavy-handed for the compiler, perhaps it could appear as as a separate product in the package. Something like foundation-json.

I'm only raising this because I believe in your ambition to be the number one JSON decoding/encoding library for Swift. But as a user of this library, I can only think of this goal being attainable by offering the other 99% of your public that are working on something using Foundation the appropriate APIs to do so, instead of expecting everyone to reinvent their own wheel.

davdroman commented 1 year ago

Closing this as I've recently realized how godawful Foundation's Decimal actually is. In hindsight, I must say well done for leaving it up to library consumers to decide which type to use. Cheers 🍻

tayloraswift commented 1 year ago

yep 😂