Skip to content

Number decoding too strict for use with WKWebView #6

@eun-ice

Description

@eun-ice

First: Thank you for this library.

However, is it possible to decode a NSNumber that is internally stored as float into an Int64? NSNumber.int64Value works fine, but your library explicitly checks the type and prevents conversion, which is not desired in my use-case.

My use case is that I want to use it to call Swift code in a WKWebView using a WKScriptMessageHandler.

In essence in JavaScript I call

window.webkit.messageHandlers.MyFineHandler.postMessage({val:1000});

WKWebView calls a WKScriptMessageHandler with an NSDictionary, but val is sometimes stored as a float and not as an integer causing the conversion to fail. In JavaScript I don't think I have control over whether the number is a float or an integer.

Minimum reproducer:

struct TestStruct: Codable {
    let val: Int64
}
let dict: NSDictionary = ["val": 6.0 as NSNumber]
let res = try KeyValueDecoder().decode(TestStruct.self, from: dict)

Is this possible to do with your library?

It works for my use-case if I change line 152 of KeyValueDecoder.swift from

-} else if let int64 = (value as? NSNumber)?.getInt64Value() {
+} else if let int64 = (value as? NSNumber)?.int64Value {

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions