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 3 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
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){

Choose a reason for hiding this comment

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

Opening Brace Spacing Violation: Opening braces should be preceded by a single space and on the same line as the declaration. (opening_brace)

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
47 changes: 45 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,17 @@ 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")

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 +133,17 @@ class NumberTests: XCTestCase {
XCTAssertEqual(json.float!, -98766.23)
XCTAssertEqual(json.floatValue, -98766.23)
XCTAssertEqual(json.numberValue, NSNumber(value: -98766.23))

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")

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 +169,20 @@ class NumberTests: XCTestCase {
XCTAssertEqual(json.int!, 98765421)
XCTAssertEqual(json.intValue, 98765421)
XCTAssertEqual(json.numberValue, NSNumber(value: 98765421))

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")

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)

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