Skip to content
Open
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
93 changes: 93 additions & 0 deletions Source/SwiftyJSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public enum SwiftyJSONError: Int, Swift.Error {
case wrongType = 901
case notExist = 500
case invalidJSON = 490
case someParsingError = 1
}

extension SwiftyJSONError: CustomNSError {
Expand All @@ -69,6 +70,8 @@ extension SwiftyJSONError: CustomNSError {
return [NSLocalizedDescriptionKey: "JSON is invalid."]
case .elementTooDeep:
return [NSLocalizedDescriptionKey: "Element too deep. Increase maxObjectDepth and make sure there is no reference loop."]
case .someParsingError:
return [NSLocalizedDescriptionKey: "Some parsing error"]
}
}
}
Expand Down Expand Up @@ -1451,3 +1454,93 @@ public enum writingOptionsKeys {
case maxObjextDepth
case encoding
}

// MARK: -

public protocol JSONMaterializableType {

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)

static var implementation: (JSON, () -> Error) throws -> Self { get }
}

extension Bool: JSONMaterializableType {

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)

public static var implementation: (JSON, () -> Error) throws -> Bool {
return { (json: JSON, factory: () -> Error) throws -> Bool in
if case .some(let bool) = json.bool {
return bool
}
throw factory()
}
}
}

extension Double: JSONMaterializableType {

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)

public static var implementation: (JSON, () -> Error) throws -> Double {
return { (json: JSON, factory: () -> Error) throws -> Double in
if case .some(let double) = json.double {
return double
}
throw factory()
}
}
}

extension Int: JSONMaterializableType {

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)

public static var implementation: (JSON, () -> Error) throws -> Int {
return { (json: JSON, factory: () -> Error) throws -> Int in
if case .some(let int) = json.int {
return int
}
throw factory()
}
}
}

extension String: JSONMaterializableType {

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)

public static var implementation: (JSON, () -> Error) throws -> String {
return { (json: JSON, factory: () -> Error) throws -> String in
if case .some(let string) = json.string {
return string
}
throw factory()
}
}
}

// might include file/line where the parsing error occured
private func factoryError(file: String, line: Int) -> () -> Error {
return { SwiftyJSONError.someParsingError }
}

public extension JSON {

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)

func materialize<T>(file: String = #file, line: Int = #line) -> T? where T: JSONMaterializableType {
return try? T.implementation(self, factoryError(file: file, line: line))
}

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)

func materialize<T>(file: String = #file, line: Int = #line) throws -> [T] where T: JSONMaterializableType {
switch array {
case .some(let array):
return try array.map({ try T.implementation($0, factoryError(file: file, line: line)) })
default:
throw factoryError(file: file, line: line)()
}
}

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)

func materialize<T>(file: String = #file, line: Int = #line) throws -> T where T: JSONMaterializableType {
return try T.implementation(self, factoryError(file: file, line: line))
}

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)

func materialize<T>(file: String = #file, line: Int = #line) throws -> T where T: RawRepresentable, T.RawValue: JSONMaterializableType {
let rawValue: T.RawValue = try materialize(file: file, line: line)
switch T(rawValue: rawValue) {
case .some(let value):
return value
default:
throw factoryError(file: file, line: line)()
}
}
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

File Line Length Violation: File should contain 400 lines or less: currently contains 1546 (file_length)