Skip to content

Allow decoding Decimal from regular numbers #26

Open
@Cyberbeni

Description

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.

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions