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
12 changes: 6 additions & 6 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import PackageDescription

let package = Package(
name: "json-swift",
targets: [
Target(name: "JSONLib", dependencies: []),
Target(name: "ParserTestHarness", dependencies: ["JSONLib"]),
Target(name: "ParserPerfTestHarness", dependencies: ["JSONLib"])
]
name: "json-swift",
targets: [
Target(name: "JSONLib", dependencies: []),
Target(name: "ParserTestHarness", dependencies: ["JSONLib"]),
Target(name: "ParserPerfTestHarness", dependencies: ["JSONLib"]),
]
)
58 changes: 29 additions & 29 deletions Sources/JSONLib/Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,44 @@

/// Represents error information for JSON parsing issues.
public final class JsonParserError: Swift.Error {
public typealias ErrorInfoDictionary = [String:String]
public typealias ErrorInfoDictionary = [String: String]

/// The error code used to differentiate between various error states.
public let code: Int
/// The error code used to differentiate between various error states.
public let code: Int

/// A string that is used to group errors into related error buckets.
public let domain: String
/// A string that is used to group errors into related error buckets.
public let domain: String

/// A place to store any custom information that needs to be passed along with the error instance.
public let userInfo: ErrorInfoDictionary?
/// A place to store any custom information that needs to be passed along with the error instance.
public let userInfo: ErrorInfoDictionary?


/// Initializes a new `Error` instance.
public init(code: Int, domain: String, userInfo: ErrorInfoDictionary?) {
self.code = code
self.domain = domain
self.userInfo = userInfo
}
/// Initializes a new `Error` instance.
public init(code: Int, domain: String, userInfo: ErrorInfoDictionary?) {
self.code = code
self.domain = domain
self.userInfo = userInfo
}
}

/// The standard keys used in `Error` and `userInfo`.
public struct ErrorKeys {
private init() {}
public static let LocalizedDescription = "NSLocalizedDescription"
public static let LocalizedFailureReason = "NSLocalizedFailureReason"
public static let LocalizedRecoverySuggestion = "NSLocalizedRecoverySuggestion"
public static let LocalizedRecoveryOptions = "NSLocalizedRecoveryOptions"
public static let RecoveryAttempter = "NSRecoveryAttempter"
public static let HelpAnchor = "NSHelpAnchor"
public static let StringEncoding = "NSStringEncoding"
public static let URL = "NSURL"
public static let FilePath = "NSFilePath"
private init() {}

public static let LocalizedDescription = "NSLocalizedDescription"
public static let LocalizedFailureReason = "NSLocalizedFailureReason"
public static let LocalizedRecoverySuggestion = "NSLocalizedRecoverySuggestion"
public static let LocalizedRecoveryOptions = "NSLocalizedRecoveryOptions"
public static let RecoveryAttempter = "NSRecoveryAttempter"
public static let HelpAnchor = "NSHelpAnchor"

public static let StringEncoding = "NSStringEncoding"
public static let URL = "NSURL"
public static let FilePath = "NSFilePath"
}

extension JsonParserError: CustomStringConvertible {
public var description: String {
return "Error code: \(self.code), domain: \(self.domain)\ninfo: \(String(describing: self.userInfo))"
}
public var description: String {
return
"Error code: \(self.code), domain: \(self.domain)\ninfo: \(String(describing: self.userInfo))"
}
}
25 changes: 12 additions & 13 deletions Sources/JSONLib/Functional.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ infix operator ⇒ : FunctionalPrecedence
/// - parameter rhs: The value to apply to the function
/// - returns: The transformation of `rhs` using `lhs`.
public func ⇒ <A, B>(lhs: ((A) -> B)?, rhs: A?) -> B? {
if let lhs = lhs {
if let rhs = rhs {
return lhs(rhs)
}
if let lhs = lhs {
if let rhs = rhs {
return lhs(rhs)
}

return nil
}

return nil
}

/// Allows for a value to be transformed by a function, allowing for optionals.
Expand All @@ -31,13 +31,13 @@ public func ⇒ <A, B>(lhs: ((A) -> B)?, rhs: A?) -> B? {
/// - parameter rhs: The transformative function
/// - returns: The transformation of `lhs` using `rhs`.
public func ⇒ <A, B>(lhs: A?, rhs: ((A) -> B)?) -> B? {
if let lhs = lhs {
if let rhs = rhs {
return rhs(lhs)
}
if let lhs = lhs {
if let rhs = rhs {
return rhs(lhs)
}

return nil
}

return nil
}

/// Allows for a transformative function to be applied to a value.
Expand All @@ -47,7 +47,6 @@ public func ⇒ <A, B>(lhs: A?, rhs: ((A) -> B)?) -> B? {
/// - returns: The transformation of `rhs` using `lhs`.
public func ⇒ <A, B>(lhs: (A) -> B, rhs: A) -> B { return lhs(rhs) }


/// Allows for a value to be transformed by a function.
///
/// - parameter lhs: The value to apply to the function
Expand Down
210 changes: 104 additions & 106 deletions Sources/JSONLib/JSValue.Accessors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,130 +8,128 @@
*/

extension JSValue {

/// Attempts to retrieve a `String` out of the `JSValue`.
///
/// - returns: If the `JSValue` is a `String`, then the stored `String` value is returned, otherwise `nil`.
public var string: String? {
switch self {
case .string(let value): return value
default: return nil
}

/// Attempts to retrieve a `String` out of the `JSValue`.
///
/// - returns: If the `JSValue` is a `String`, then the stored `String` value is returned, otherwise `nil`.
public var string: String? {
switch self {
case .string(let value): return value
default: return nil
}
}

/// Attempts to retrieve a `Double` out of the `JSValue`.
///
/// - returns: If the `JSValue` is a `JSNumber`, then the stored `Double` value is returned, otherwise `nil`.
public var number: Double? {
switch self {
case .number(let value): return value
default: return nil
}
/// Attempts to retrieve a `Double` out of the `JSValue`.
///
/// - returns: If the `JSValue` is a `JSNumber`, then the stored `Double` value is returned, otherwise `nil`.
public var number: Double? {
switch self {
case .number(let value): return value
default: return nil
}
}

/// Attempts to retrieve an `Int` out of the `JSValue`.
///
/// - returns: If the `JSValue` is a `Double`, then the stored `Double` value is returned, otherwise `nil`.
public var integer: Int? {
switch self {
case .number(let value): return Int(value)
default: return nil
}
/// Attempts to retrieve an `Int` out of the `JSValue`.
///
/// - returns: If the `JSValue` is a `Double`, then the stored `Double` value is returned, otherwise `nil`.
public var integer: Int? {
switch self {
case .number(let value): return Int(value)
default: return nil
}
}


/// Attempts to retrieve a `Bool` out of the `JSValue`.
///
/// - returns: If the `JSValue` is a `Bool`, then the stored `Bool` value is returned, otherwise `nil`.
public var bool: Bool? {
switch self {
case .bool(let value): return value
default: return nil
}
/// Attempts to retrieve a `Bool` out of the `JSValue`.
///
/// - returns: If the `JSValue` is a `Bool`, then the stored `Bool` value is returned, otherwise `nil`.
public var bool: Bool? {
switch self {
case .bool(let value): return value
default: return nil
}
}

/// Attempts to retrieve a `[String:JSValue]` out of the `JSValue`.
///
/// - returns: If the `JSValue` is a `[String:JSValue]`, then the stored `[String:JSValue]` value is returned, otherwise `nil`.
public var object: [String:JSValue]? {
switch self {
case .object(let value): return value
default: return nil
}
/// Attempts to retrieve a `[String:JSValue]` out of the `JSValue`.
///
/// - returns: If the `JSValue` is a `[String:JSValue]`, then the stored `[String:JSValue]` value is returned, otherwise `nil`.
public var object: [String: JSValue]? {
switch self {
case .object(let value): return value
default: return nil
}
/// Attempts to retrieve a `[JSValue]` out of the `JSValue`.
///
/// - returns: If the `JSValue` is a `JSArray`, then the stored `[JSValue]` value is returned, otherwise `nil`.
public var array: [JSValue]? {
switch self {
case .array(let value): return value
default: return nil
}
}

/// Attempts to retrieve a `[JSValue]` out of the `JSValue`.
///
/// - returns: If the `JSValue` is a `JSArray`, then the stored `[JSValue]` value is returned, otherwise `nil`.
public var array: [JSValue]? {
switch self {
case .array(let value): return value
default: return nil
}
/// Used to determine if a `nil` value is stored within `JSValue`. There is no intrinsic type for this value.
///
/// - returns: If the `JSValue` is a `JSNull`, then the `true` is returned, otherwise `false`.
public var null: Bool {
switch self {
case .null: return true
default: return false
}
}

/// Used to determine if a `nil` value is stored within `JSValue`. There is no intrinsic type for this value.
///
/// - returns: If the `JSValue` is a `JSNull`, then the `true` is returned, otherwise `false`.
public var null: Bool {
switch self {
case .null: return true
default: return false
}
}
}

/// Provide a usability extension to allow chaining index accessors without having to
/// marked it as optional.
/// e.g. foo["hi"]?["this"]?["sucks"]?.string vs. foo["so"]["much"]["better"].string
extension Optional where Wrapped == JSValue {
/// Attempts to retrieve a `String` out of the `JSValue`.
///
/// - returns: If the `JSValue` is a `String`, then the stored `String` value is returned, otherwise `nil`.
public var string: String? {
return self?.string
}
/// Attempts to retrieve a `String` out of the `JSValue`.
///
/// - returns: If the `JSValue` is a `String`, then the stored `String` value is returned, otherwise `nil`.
public var string: String? {
return self?.string
}

/// Attempts to retrieve a `Double` out of the `JSValue`.
///
/// - returns: If the `JSValue` is a `JSNumber`, then the stored `Double` value is returned, otherwise `nil`.
public var number: Double? {
return self?.number
}
/// Attempts to retrieve a `Double` out of the `JSValue`.
///
/// - returns: If the `JSValue` is a `JSNumber`, then the stored `Double` value is returned, otherwise `nil`.
public var number: Double? {
return self?.number
}

/// Attempts to retrieve an `Int` out of the `JSValue`.
///
/// - returns: If the `JSValue` is a `Double`, then the stored `Double` value is returned, otherwise `nil`.
public var integer: Int? {
return self?.integer
}
/// Attempts to retrieve an `Int` out of the `JSValue`.
///
/// - returns: If the `JSValue` is a `Double`, then the stored `Double` value is returned, otherwise `nil`.
public var integer: Int? {
return self?.integer
}


/// Attempts to retrieve a `Bool` out of the `JSValue`.
///
/// - returns: If the `JSValue` is a `Bool`, then the stored `Bool` value is returned, otherwise `nil`.
public var bool: Bool? {
return self?.bool
}
/// Attempts to retrieve a `Bool` out of the `JSValue`.
///
/// - returns: If the `JSValue` is a `Bool`, then the stored `Bool` value is returned, otherwise `nil`.
public var bool: Bool? {
return self?.bool
}

/// Attempts to retrieve a `[String:JSValue]` out of the `JSValue`.
///
/// - returns: If the `JSValue` is a `[String:JSValue]`, then the stored `[String:JSValue]` value is returned, otherwise `nil`.
public var object: [String:JSValue]? {
return self?.object
}
/// Attempts to retrieve a `[JSValue]` out of the `JSValue`.
///
/// - returns: If the `JSValue` is a `JSArray`, then the stored `[JSValue]` value is returned, otherwise `nil`.
public var array: [JSValue]? {
return self?.array
}
/// Used to determine if a `nil` value is stored within `JSValue`. There is no intrinsic type for this value.
///
/// - returns: If the `JSValue` is a `JSNull`, then the `true` is returned, otherwise `false`.
public var null: Bool {
return self?.null ?? false
}
}
/// Attempts to retrieve a `[String:JSValue]` out of the `JSValue`.
///
/// - returns: If the `JSValue` is a `[String:JSValue]`, then the stored `[String:JSValue]` value is returned, otherwise `nil`.
public var object: [String: JSValue]? {
return self?.object
}

/// Attempts to retrieve a `[JSValue]` out of the `JSValue`.
///
/// - returns: If the `JSValue` is a `JSArray`, then the stored `[JSValue]` value is returned, otherwise `nil`.
public var array: [JSValue]? {
return self?.array
}

/// Used to determine if a `nil` value is stored within `JSValue`. There is no intrinsic type for this value.
///
/// - returns: If the `JSValue` is a `JSNull`, then the `true` is returned, otherwise `false`.
public var null: Bool {
return self?.null ?? false
}
}
8 changes: 4 additions & 4 deletions Sources/JSONLib/JSValue.Encodings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// /// within the contents of a string value.
// public struct Encodings {
// private init() {}
//
//
// /// The encoding prefix for all base64 encoded values.
// public static let base64 = "data:text/plain;base64,"
// }
Expand All @@ -29,11 +29,11 @@
// // return [Byte](bytes)
// // }
// fatalError("nyi")
//
//
// default:
// return nil
// }
//
//
// return nil
// }
//}
//}
Loading