Skip to content

Add JSON.isArray and JSON.isObject #9

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

Merged
merged 2 commits into from
Nov 3, 2024
Merged
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
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ on: push

jobs:
test:
runs-on: macos-13
runs-on: macos-14

steps:
- uses: actions/checkout@v3
- name: Select Xcode 15
run: sudo xcode-select -s /Applications/Xcode_15.0.app
- name: Select Xcode 16
run: sudo xcode-select -s /Applications/Xcode_16.0.app
- name: Test
run: swift test
40 changes: 20 additions & 20 deletions Sources/JSON/Dictionary+JSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,67 +3,67 @@ import Foundation
public extension [String: Any] {
func boolValue(forKey key: String) -> Bool? {
if let v = self[key] as? Bool {
return v
v
} else if let v = self[key] as? NSNumber, v.isBool {
return v.boolValue
v.boolValue
} else {
return nil
nil
}
}

func doubleValue(forKey key: String) -> Double? {
if let v = self[key] as? Double {
return v
v
} else if let v = self[key] as? NSNumber, !v.isBool {
return v.doubleValue
v.doubleValue
} else {
return nil
nil
}
}

func intValue(forKey key: String) -> Int? {
if let v = self[key] as? Int {
return v
v
} else if let v = self[key] as? Double {
return Int(v)
Int(v)
} else if let v = self[key] as? NSNumber, !v.isBool {
return v.intValue
v.intValue
} else {
return nil
nil
}
}
}

public extension [String: Any]? {
func boolValue(forKey key: String) -> Bool? {
if let v = self?[key] as? Bool {
return v
v
} else if let v = self?[key] as? NSNumber, v.isBool {
return v.boolValue
v.boolValue
} else {
return nil
nil
}
}

func doubleValue(forKey key: String) -> Double? {
if let v = self?[key] as? Double {
return v
v
} else if let v = self?[key] as? NSNumber, !v.isBool {
return v.doubleValue
v.doubleValue
} else {
return nil
nil
}
}

func intValue(forKey key: String) -> Int? {
if let v = self?[key] as? Int {
return v
v
} else if let v = self?[key] as? Double {
return Int(v)
Int(v)
} else if let v = self?[key] as? NSNumber, !v.isBool {
return v.intValue
v.intValue
} else {
return nil
nil
}
}
}
Expand Down
63 changes: 43 additions & 20 deletions Sources/JSON/JSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,22 +150,22 @@ public enum JSON:
public var rawValue: Any {
switch self {
case let .array(array):
return array.map(\.rawValue)
array.map(\.rawValue)

case let .boolean(bool):
return bool
bool

case let .dictionary(dictionary):
return dictionary.mapValues(\.rawValue)
dictionary.mapValues(\.rawValue)

case .null:
return NSNull()
NSNull()

case let .number(number):
return number
number

case let .string(string):
return string
string
}
}

Expand Down Expand Up @@ -203,9 +203,9 @@ public enum JSON:
/// underlying type is `.number`, otherwise `nil`.
public var integerValue: Int? {
if let double = rawValue as? Double {
return Int(double)
Int(double)
} else {
return nil
nil
}
}

Expand All @@ -216,6 +216,19 @@ public enum JSON:
return string
}

/// Returns `true` if the receiver is an array, otherwise `false`.
public var isArray: Bool {
guard case .array = self else { return false }
return true
}

/// Returns `true` if the receiver is a JSON object (a "dictionary"),
/// otherwise `false`.
public var isObject: Bool {
guard case .dictionary = self else { return false }
return true
}

public var description: String {
String(describing: rawValue)
}
Expand Down Expand Up @@ -280,25 +293,25 @@ extension JSON: Equatable {
public static func == (_ arg1: JSON, _ arg2: JSON) -> Bool {
switch (arg1, arg2) {
case let (.array(one), .array(two)):
return one == two
one == two

case let (.boolean(one), .boolean(two)):
return one == two
one == two

case let (.dictionary(one), .dictionary(two)):
return one == two
one == two

case let (.number(one), .number(two)):
return one == two
one == two

case (.null, .null):
return true
true

case let (.string(one), .string(two)):
return one == two
one == two

default:
return false
false
}
}
}
Expand Down Expand Up @@ -508,9 +521,9 @@ public extension JSON? {
/// underlying type is `.number`, otherwise `nil`.
var integerValue: Int? {
if let double = self?.rawValue as? Double {
return Int(double)
Int(double)
} else {
return nil
nil
}
}

Expand All @@ -521,6 +534,19 @@ public extension JSON? {
return string
}

/// Returns `true` if the receiver is an array, otherwise `false`.
var isArray: Bool {
guard let self, self.isArray else { return false }
return true
}

/// Returns `true` if the receiver is a JSON object (a "dictionary"),
/// otherwise `false`.
var isObject: Bool {
guard let self, self.isObject else { return false }
return true
}

static func == (_ arg1: JSON, _ arg2: JSON?) -> Bool {
guard let arg2 else { return false }
return arg1 == arg2
Expand Down Expand Up @@ -620,7 +646,6 @@ private extension Any? {
case let e as NSNumber where e.isBool: return .boolean(e.boolValue)
case let e as NSNumber: return .number(e.doubleValue)
case let e as String: return .string(e)

// The above cases should catch everything, but, in case they
// don't, we try remaining types here.
case let e as Bool: return .boolean(e)
Expand All @@ -637,9 +662,7 @@ private extension Any? {
case let e as UInt16: return .number(Double(e))
case let e as UInt32: return .number(Double(e))
case let e as UInt64: return .number(Double(e))

case let e as JSON: return e

default: return nil
}
}
Expand Down
76 changes: 76 additions & 0 deletions Tests/JSONTests/JSONTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -339,4 +339,80 @@ final class JSONTests: XCTestCase {
}
XCTAssertEqual(dict, mixed)
}

func testIsArray() throws {
let array: JSON = ["one", 2, 3.0, true, nil]
XCTAssertTrue(array.isArray)

let object: JSON = ["one": 1, "two": 2, "three": 3]
XCTAssertFalse(object.isArray)

let string: JSON = "string"
XCTAssertFalse(string.isArray)

let number: JSON = 123
XCTAssertFalse(number.isArray)

let bool: JSON = true
XCTAssertFalse(bool.isArray)

let null: JSON = nil
XCTAssertFalse(null.isArray)

let optionalArray: JSON? = ["one", 2, 3.0, true, nil]
XCTAssertTrue(optionalArray.isArray)

let optionalObject: JSON? = ["one": 1, "two": 2, "three": 3]
XCTAssertFalse(optionalObject.isArray)

let optionalString: JSON? = "string"
XCTAssertFalse(optionalString.isArray)

let optionalNumber: JSON? = 123
XCTAssertFalse(optionalNumber.isArray)

let optionalBool: JSON? = true
XCTAssertFalse(optionalBool.isArray)

let optionalNull: JSON? = nil
XCTAssertFalse(optionalNull.isArray)
}

func testIsObject() throws {
let array: JSON = ["one", 2, 3.0, true, nil]
XCTAssertFalse(array.isObject)

let object: JSON = ["one": 1, "two": 2, "three": 3]
XCTAssertTrue(object.isObject)

let string: JSON = "string"
XCTAssertFalse(string.isObject)

let number: JSON = 123
XCTAssertFalse(number.isObject)

let bool: JSON = true
XCTAssertFalse(bool.isObject)

let null: JSON = nil
XCTAssertFalse(null.isObject)

let optionalArray: JSON? = ["one", 2, 3.0, true, nil]
XCTAssertFalse(optionalArray.isObject)

let optionalObject: JSON? = ["one": 1, "two": 2, "three": 3]
XCTAssertTrue(optionalObject.isObject)

let optionalString: JSON? = "string"
XCTAssertFalse(optionalString.isObject)

let optionalNumber: JSON? = 123
XCTAssertFalse(optionalNumber.isObject)

let optionalBool: JSON? = true
XCTAssertFalse(optionalBool.isObject)

let optionalNull: JSON? = nil
XCTAssertFalse(optionalNull.isObject)
}
}
Loading