Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
20 changes: 20 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 Expand Up @@ -1061,6 +1067,13 @@ extension JSON {

public var int: Int? {
get {
if let num = self.number as? NSDecimalNumber {
let returnInt = num.intValue
if num.isEqual(returnInt) {
return returnInt
}
return nil
}
return self.number?.intValue
}
set {
Expand All @@ -1074,6 +1087,13 @@ extension JSON {

public var intValue: Int {
get {
if let num = self.numberValue as? NSDecimalNumber {
let returnInt = num.intValue
if num.isEqual(returnInt) {
return returnInt
}
return 0
}
return self.numberValue.intValue
}
set {
Expand Down
45 changes: 43 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,16 @@ 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")

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.double)
XCTAssertEqual(json.doubleValue, 0)
}

func testFloat() {
Expand All @@ -115,6 +132,16 @@ class NumberTests: XCTestCase {
XCTAssertEqual(json.float!, -98766.23)
XCTAssertEqual(json.floatValue, -98766.23)
XCTAssertEqual(json.numberValue, NSNumber(value: -98766.23))

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.float!, 9876543210.123456789)
XCTAssertEqual(json.floatValue, 9876543210.123456789)
XCTAssertEqual(json.numberValue, 9876543210.123456789)
XCTAssertEqual(json.stringValue, "9876543210.123456789")

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.float)
XCTAssertEqual(json.floatValue, 0)
}

func testInt() {
Expand All @@ -140,6 +167,20 @@ class NumberTests: XCTestCase {
XCTAssertEqual(json.int!, 98765421)
XCTAssertEqual(json.intValue, 98765421)
XCTAssertEqual(json.numberValue, NSNumber(value: 98765421))

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"
XCTAssertEqual(json.int!, 9876543210)
XCTAssertEqual(json.intValue, 9876543210)
XCTAssertEqual(json.numberValue, 9876543210)
XCTAssertEqual(json.stringValue, "9876543210")

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 = "3.14"
XCTAssertNil(json.int)
XCTAssertEqual(json.intValue, 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 = "swift"
XCTAssertNil(json.int)
XCTAssertEqual(json.intValue, 0)
}

func testUInt() {
Expand Down