-
Notifications
You must be signed in to change notification settings - Fork 1.2k
JSONEncoder implementation #1048
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
eac219a
JSONEncoder implemented
bubski f90dacf
JSONEncoder now using JSONSerialization with reference numeric types
bubski 6147753
Changed value of useReferenceNumericTypes flag
bubski 1b36a51
Merge branch 'master' of github.com:apple/swift-corelibs-foundation i…
bubski 43e0e4a
Fixed strict bool matching in JSONEncoder
bubski b23ca90
Merge branch 'master' of github.com:apple/swift-corelibs-foundation i…
bubski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See http://swift.org/LICENSE.txt for license information | ||
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
|
||
//===----------------------------------------------------------------------===// | ||
// Errors | ||
//===----------------------------------------------------------------------===// | ||
|
||
// Adding the following extensions to EncodingError and DecodingError allows them to bridge to NSErrors implicitly. | ||
|
||
fileprivate let NSCodingPathErrorKey = "NSCodingPath" | ||
fileprivate let NSDebugDescriptionErrorKey = "NSDebugDescription" | ||
|
||
extension EncodingError : CustomNSError { | ||
public static var errorDomain: String = NSCocoaErrorDomain | ||
|
||
public var errorCode: Int { | ||
switch self { | ||
case .invalidValue(_, _): return CocoaError.coderInvalidValue.rawValue | ||
} | ||
} | ||
|
||
public var errorUserInfo: [String : Any] { | ||
let context: Context | ||
switch self { | ||
case .invalidValue(_, let c): context = c | ||
} | ||
|
||
return [NSCodingPathErrorKey: context.codingPath, | ||
NSDebugDescriptionErrorKey: context.debugDescription] | ||
} | ||
} | ||
|
||
extension DecodingError : CustomNSError { | ||
public static var errorDomain: String = NSCocoaErrorDomain | ||
|
||
public var errorCode: Int { | ||
switch self { | ||
case .valueNotFound(_, _): fallthrough | ||
case .keyNotFound(_, _): | ||
return CocoaError.coderValueNotFound.rawValue | ||
|
||
case .typeMismatch(_, _): fallthrough | ||
case .dataCorrupted(_): | ||
return CocoaError.coderReadCorrupt.rawValue | ||
} | ||
} | ||
|
||
public var errorUserInfo: [String : Any] { | ||
let context: Context | ||
switch self { | ||
case .typeMismatch(_, let c): context = c | ||
case .valueNotFound(_, let c): context = c | ||
case .keyNotFound(_, let c): context = c | ||
case .dataCorrupted(let c): context = c | ||
} | ||
|
||
return [NSCodingPathErrorKey: context.codingPath, | ||
NSDebugDescriptionErrorKey: context.debugDescription] | ||
} | ||
} | ||
|
||
//===----------------------------------------------------------------------===// | ||
// Error Utilities | ||
//===----------------------------------------------------------------------===// | ||
|
||
internal extension DecodingError { | ||
/// Returns a `.typeMismatch` error describing the expected type. | ||
/// | ||
/// - parameter path: The path of `CodingKey`s taken to decode a value of this type. | ||
/// - parameter expectation: The type expected to be encountered. | ||
/// - parameter reality: The value that was encountered instead of the expected type. | ||
/// - returns: A `DecodingError` with the appropriate path and debug description. | ||
internal static func _typeMismatch(at path: [CodingKey?], expectation: Any.Type, reality: Any) -> DecodingError { | ||
let description = "Expected to decode \(expectation) but found \(_typeDescription(of: reality)) instead." | ||
return .typeMismatch(expectation, Context(codingPath: path, debugDescription: description)) | ||
} | ||
|
||
/// Returns a description of the type of `value` appropriate for an error message. | ||
/// | ||
/// - parameter value: The value whose type to describe. | ||
/// - returns: A string describing `value`. | ||
/// - precondition: `value` is one of the types below. | ||
fileprivate static func _typeDescription(of value: Any) -> String { | ||
if value is NSNull { | ||
return "a null value" | ||
} else if value is NSNumber /* FIXME: If swift-corelibs-foundation isn't updated to use NSNumber, this check will be necessary: || value is Int || value is Double */ { | ||
return "a number" | ||
} else if value is String { | ||
return "a string/data" | ||
} else if value is [Any] { | ||
return "an array" | ||
} else if value is [String : Any] { | ||
return "a dictionary" | ||
} else { | ||
// This should never happen -- we somehow have a non-JSON type here. | ||
preconditionFailure("Invalid storage type \(type(of: value)).") | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we don't have error bridging in Linux, I think this whole section (short of the helper methods on
DecodingError
) can be removed.