-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow decoding Decimal from regular numbers #26
Comments
Related Swift bugreport: https://bugs.swift.org/browse/SR-7054 |
Hi @Cyberbeni, I get your point. Thanks for explaining here. Of course I could add a special decode function to When you implement your own That's why I'm afraid the real solution to your problem is to propose a change to the Swift STL which would add a function Does that make sense? |
I realized that you have to do it in the generic decode function and for all 3 types of containers. |
It‘s not so much a problem of my containers but the container protocol. |
And similarly for the other 2 containers. |
Okay. That would work, but in this case I would need to link Foundation - which kinda is not the goal of this Endeavour. But you‘re free to fork any time... @Cyberbeni Does this work for you? |
I also came here in hopes of finding a solution to the JSON->Decimal problem. This blog post is a good description of the issue in Swift. |
(I don't need this at my current job, that I also had when I opened this issue, the last 2 comments from March should lead you to a relatively easy solution @xanderdunn ) |
Thanks @Cyberbeni, your solution was to fork the codebase and modify this |
yes |
Decimals are originally meant to be encoded/decoded as a dictionary: https://github.com/apple/swift/blob/3978d81c840c899bd090243d7d344f8dceacd74b/stdlib/public/Darwin/Foundation/Decimal.swift#L118
Apple's JSONDecoder provides a way to decode them from single numbers but sadly numbers are parsed as Double by JSONSerialization, so it loses precision: https://github.com/apple/swift/blob/3978d81c840c899bd090243d7d344f8dceacd74b/stdlib/public/Darwin/Foundation/JSONEncoder.swift#L2464
Since pure-swift-json parses numbers as string, we could provide a similar way of parsing Decimal from regular numbers. As far as I can understand, JSONSingleValueDecodingContainter would need a special decode function for Decimal.Type that would work similarly to the second link.
Decimal's init also takes a Locale, it should be allowed to be customized and the default should probably be "en_US".
Most likely a separate issue but there should also be an option for encoding Decimals the same way instead of using the default method.
The text was updated successfully, but these errors were encountered: