Skip to content
This repository has been archived by the owner on Oct 14, 2020. It is now read-only.

Commit

Permalink
Fixed an accessor bug for optional fields
Browse files Browse the repository at this point in the history
When a field doesn't exist we now return nil where possible if the field does exist however and the initializer throws, so does the call to get
  • Loading branch information
vdka committed Sep 20, 2016
1 parent 0abedec commit 3181775
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion JSON.xcodeproj/JSON_Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>0.11.3</string>
<string>0.11.4</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
7 changes: 4 additions & 3 deletions Sources/JSON/JSONAccessors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ extension JSON {
return try T(json: self)
}

/// - Note: This call will throw iff the initializer does.
public func get<T: JSONInitializable>() throws -> T? {
return try T(json: self)
}
Expand Down Expand Up @@ -68,8 +69,10 @@ extension JSON {
return try T(json: json)
}

/// If the Field exists in the JSON then this will call to the expected types initializer
/// - Note: This call will throw iff the initializer does
public func get<T: JSONInitializable>(_ field: String) throws -> T? {
guard let json = self[field] else { throw JSON.Error.badField(field) }
guard let json = self[field] else { return nil }
return try T(json: json)
}

Expand All @@ -79,7 +82,6 @@ extension JSON {
return try array.map(T.init(json:))
}

// TODO(vdka): Using Any here will likely be a slow down
/// Returns the content matching the type of its destination
public func get<T: RawRepresentable>(_ field: String) throws -> T
where T.RawValue: JSONInitializable
Expand All @@ -90,7 +92,6 @@ extension JSON {
return value
}

// TODO(vdka): Using Any here will likely be a slow down
/// Returns the content matching the type of its destination
public func get<T: RawRepresentable>(_ field: String) throws -> [T]
where T.RawValue: JSONInitializable
Expand Down
1 change: 0 additions & 1 deletion Tests/JSONTests/ParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,6 @@ class ParsingTests: XCTestCase {
)
}

// NOTE(vdka): Need to add special handling for this.
func testTrollBlockComment() {

expect("/*/ {'key':'harry'}", toThrowWithReason: .invalidSyntax, withOptions: .allowComments)
Expand Down

0 comments on commit 3181775

Please sign in to comment.