rundfunk47 / Juice

A modern and simple JSON Encoder / Decoder for Swift 3
MIT License
6 stars 1 forks source link

Parsing json number without decimal part into Double? fails #2

Closed jkufver closed 6 years ago

jkufver commented 6 years ago

I receive runtimeHours as json to my app;

{
...
"runtimeHours": 1234,
...
}

In my model the value is an optional Double. I found that my values always is nil using this code:

runtimeHours = try? json.decode(["runtimeHours"])

I thinks this is a bug, 1234 should be treated as a valid Double.

If I do try! instead the exception is:

Thread 1: Fatal error: 'try!' expression unexpectedly raised an error: Juice.DictionaryDecodingError(keyPath: ["runtimeHours"], underlyingError: Juice.MismatchError.typeMismatch(expected: Swift.Double, got: 1234))

Finally, my workaround now is this:

        if let intValue: Int = try? json.decode(["runtimeHours"]) {
            runtimeHours = Double(intValue)
        } else {
            runtimeHours = try? json.decode(["runtimeHours"])
        }

BTW, I like your pod a lot. Cheers.

rundfunk47 commented 6 years ago

Oops, didn't see this until now. Yes, I would also call this a bug, fixed in 1.0.9!