Skip to content
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

String to Double? conversion fix #910

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions Source/SwiftyJSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,12 @@ extension JSON {
public var number: NSNumber? {
get {
switch self.type {
case .string:
let decimal: NSDecimalNumber? = NSDecimalNumber(string: self.object as? String)
if decimal == NSDecimalNumber.notANumber { // indicates parse error
return nil
}
return decimal
case .number:
return self.rawNumber
case .bool:
Expand Down
17 changes: 15 additions & 2 deletions Tests/SwiftyJSONTests/NumberTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,17 @@ class NumberTests: XCTestCase {
XCTAssertEqual(json.stringValue, "9876543210.123457")

json.string = "1000000000000000000000000000.1"
XCTAssertNil(json.number)
XCTAssertEqual(json.number!.description, "1000000000000000000000000000.1" )
XCTAssertEqual(json.number!, 1000000000000000000000000000.1 )
XCTAssertEqual(json.numberValue.description, "1000000000000000000000000000.1")

XCTAssertEqual(json.numberValue, 1000000000000000000000000000.1)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)

json.string = "swift"
XCTAssertNil(json.number)
XCTAssertEqual(json.numberValue, 0)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)

json.string = "1e+27"
XCTAssertEqual(json.number!.description, "1000000000000000000000000000")
XCTAssertEqual(json.numberValue.description, "1000000000000000000000000000")

//setter
Expand Down Expand Up @@ -97,6 +104,12 @@ class NumberTests: XCTestCase {
XCTAssertEqual(json.boolValue, false)
XCTAssertEqual(json.doubleValue, 0.0)
XCTAssertEqual(json.numberValue, 0)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)

json = "9876543210.123456789"
XCTAssertEqual(json.double!, 9876543210.123456789)
XCTAssertEqual(json.doubleValue, 9876543210.123456789)
XCTAssertEqual(json.numberValue, 9876543210.123456789)
XCTAssertEqual(json.stringValue, "9876543210.123456789")
}

func testFloat() {
Expand Down