Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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)

Copy link
Copy Markdown

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)

Copy link
Copy Markdown

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)

Copy link
Copy Markdown

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