forked from Pircate/CleanJSON
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
286 additions
and
280 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// | ||
// _CleanJSONDecodingStorage.swift | ||
// CleanJSONDecodingStorage.swift | ||
// CleanJSON | ||
// | ||
// Created by Pircate([email protected]) on 2018/10/10 | ||
|
@@ -8,7 +8,7 @@ | |
|
||
import Foundation | ||
|
||
struct _CleanJSONDecodingStorage { | ||
struct CleanJSONDecodingStorage { | ||
|
||
/// The container stack. | ||
/// Elements may be any one of the JSON types (NSNull, NSNumber, String, Array, [String : Any]). | ||
|
This file contains 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// | ||
// _CleanJSONKey.swift | ||
// CleanJSONKey.swift | ||
// CleanJSON | ||
// | ||
// Created by Pircate([email protected]) on 2018/10/11 | ||
|
@@ -8,7 +8,7 @@ | |
|
||
import Foundation | ||
|
||
struct _CleanJSONKey : CodingKey { | ||
struct CleanJSONKey : CodingKey { | ||
|
||
public var stringValue: String | ||
|
||
|
@@ -34,5 +34,5 @@ struct _CleanJSONKey : CodingKey { | |
self.intValue = index | ||
} | ||
|
||
static let `super` = _CleanJSONKey(stringValue: "super")! | ||
static let `super` = CleanJSONKey(stringValue: "super")! | ||
} |
This file contains 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// | ||
// _CleanJSONKeyedDecodingContainer.swift | ||
// CleanJSONKeyedDecodingContainer.swift | ||
// CleanJSON | ||
// | ||
// Created by Pircate([email protected]) on 2018/10/10 | ||
|
@@ -8,7 +8,7 @@ | |
|
||
import Foundation | ||
|
||
struct _CleanJSONKeyedDecodingContainer<K : CodingKey>: KeyedDecodingContainerProtocol { | ||
struct CleanJSONKeyedDecodingContainer<K : CodingKey>: KeyedDecodingContainerProtocol { | ||
|
||
typealias Key = K | ||
|
||
|
@@ -39,7 +39,7 @@ struct _CleanJSONKeyedDecodingContainer<K : CodingKey>: KeyedDecodingContainerPr | |
}, uniquingKeysWith: { (first, _) in first }) | ||
case .custom(let converter): | ||
self.container = Dictionary(container.map { | ||
key, value in (converter(decoder.codingPath + [_CleanJSONKey(stringValue: key, intValue: nil)]).stringValue, value) | ||
key, value in (converter(decoder.codingPath + [CleanJSONKey(stringValue: key, intValue: nil)]).stringValue, value) | ||
}, uniquingKeysWith: { (first, _) in first }) | ||
@unknown default: | ||
self.container = container | ||
|
@@ -486,7 +486,7 @@ struct _CleanJSONKeyedDecodingContainer<K : CodingKey>: KeyedDecodingContainerPr | |
|
||
private func nestedContainer<NestedKey>(wrapping dictionary: [String: Any] = [:]) | ||
-> KeyedDecodingContainer<NestedKey> { | ||
let container = _CleanJSONKeyedDecodingContainer<NestedKey>( | ||
let container = CleanJSONKeyedDecodingContainer<NestedKey>( | ||
referencing: decoder, | ||
wrapping: dictionary) | ||
return KeyedDecodingContainer(container) | ||
|
@@ -501,7 +501,7 @@ struct _CleanJSONKeyedDecodingContainer<K : CodingKey>: KeyedDecodingContainerPr | |
case .throw: | ||
throw DecodingError.Nested.keyNotFound(key, codingPath: codingPath, isUnkeyed: true) | ||
case .useEmptyContainer: | ||
return _CleanJSONUnkeyedDecodingContainer(referencing: self.decoder, wrapping: []) | ||
return CleanJSONUnkeyedDecodingContainer(referencing: self.decoder, wrapping: []) | ||
} | ||
} | ||
|
||
|
@@ -512,11 +512,11 @@ struct _CleanJSONKeyedDecodingContainer<K : CodingKey>: KeyedDecodingContainerPr | |
at: self.codingPath, | ||
expectation: [Any].self, reality: value) | ||
case .useEmptyContainer: | ||
return _CleanJSONUnkeyedDecodingContainer(referencing: self.decoder, wrapping: []) | ||
return CleanJSONUnkeyedDecodingContainer(referencing: self.decoder, wrapping: []) | ||
} | ||
} | ||
|
||
return _CleanJSONUnkeyedDecodingContainer(referencing: self.decoder, wrapping: array) | ||
return CleanJSONUnkeyedDecodingContainer(referencing: self.decoder, wrapping: array) | ||
} | ||
|
||
private func _superDecoder(forKey key: CodingKey) throws -> Decoder { | ||
|
@@ -528,7 +528,7 @@ struct _CleanJSONKeyedDecodingContainer<K : CodingKey>: KeyedDecodingContainerPr | |
} | ||
|
||
public func superDecoder() throws -> Decoder { | ||
return try _superDecoder(forKey: _CleanJSONKey.super) | ||
return try _superDecoder(forKey: CleanJSONKey.super) | ||
} | ||
|
||
public func superDecoder(forKey key: Key) throws -> Decoder { | ||
|
@@ -584,7 +584,7 @@ private extension CleanJSONDecoder.KeyDecodingStrategy { | |
} | ||
} | ||
|
||
private extension _CleanJSONKeyedDecodingContainer { | ||
private extension CleanJSONKeyedDecodingContainer { | ||
|
||
func decodeIfKeyNotFound<T>(_ key: Key) throws -> T where T: Decodable, T: Defaultable { | ||
switch decoder.options.keyNotFoundDecodingStrategy { | ||
|
@@ -630,7 +630,7 @@ extension _CleanJSONDecoder { | |
} | ||
} | ||
|
||
extension _CleanJSONKeyedDecodingContainer { | ||
extension CleanJSONKeyedDecodingContainer { | ||
|
||
func decodeIfPresent(_ type: Bool.Type, forKey key: K) throws -> Bool? { | ||
guard contains(key), let entry = container[key.stringValue] else { return nil } | ||
|
@@ -859,7 +859,7 @@ extension _CleanJSONKeyedDecodingContainer { | |
} | ||
} | ||
|
||
private extension _CleanJSONKeyedDecodingContainer { | ||
private extension CleanJSONKeyedDecodingContainer { | ||
|
||
func decodeIfPresent(_ value: Any, as type: Date.Type, forKey key: K) throws -> Date? { | ||
if let date = try decoder.unbox(value, as: type) { return date } | ||
|
@@ -916,7 +916,7 @@ private extension _CleanJSONKeyedDecodingContainer { | |
|
||
private extension String { | ||
|
||
func decode<T: Decodable>(to type: T.Type, options: CleanJSONDecoder._Options) -> T? { | ||
func decode<T: Decodable>(to type: T.Type, options: CleanJSONDecoder.Options) -> T? { | ||
guard hasPrefix("{") || hasPrefix("[") else { return nil } | ||
|
||
guard let data = data(using: .utf8), | ||
|
This file contains 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// | ||
// _CleanJSONUnkeyedDecodingContainer.swift | ||
// CleanJSONUnkeyedDecodingContainer.swift | ||
// CleanJSON | ||
// | ||
// Created by Pircate([email protected]) on 2018/10/11 | ||
|
@@ -8,7 +8,7 @@ | |
|
||
import Foundation | ||
|
||
struct _CleanJSONUnkeyedDecodingContainer : UnkeyedDecodingContainer { | ||
struct CleanJSONUnkeyedDecodingContainer : UnkeyedDecodingContainer { | ||
// MARK: Properties | ||
|
||
/// A reference to the decoder we're reading from. | ||
|
@@ -45,7 +45,7 @@ struct _CleanJSONUnkeyedDecodingContainer : UnkeyedDecodingContainer { | |
|
||
public mutating func decodeNil() throws -> Bool { | ||
guard !self.isAtEnd else { | ||
throw DecodingError.valueNotFound(Any?.self, DecodingError.Context(codingPath: self.decoder.codingPath + [_CleanJSONKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end.")) | ||
throw DecodingError.valueNotFound(Any?.self, DecodingError.Context(codingPath: self.decoder.codingPath + [CleanJSONKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end.")) | ||
} | ||
|
||
if self.container[self.currentIndex] is NSNull { | ||
|
@@ -61,7 +61,7 @@ struct _CleanJSONUnkeyedDecodingContainer : UnkeyedDecodingContainer { | |
return try decode(isAtEnd: true) | ||
} | ||
|
||
self.decoder.codingPath.append(_CleanJSONKey(index: self.currentIndex)) | ||
self.decoder.codingPath.append(CleanJSONKey(index: self.currentIndex)) | ||
defer { self.decoder.codingPath.removeLast() } | ||
|
||
guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: Bool.self) else { | ||
|
@@ -94,7 +94,7 @@ struct _CleanJSONUnkeyedDecodingContainer : UnkeyedDecodingContainer { | |
return try decode(isAtEnd: true) | ||
} | ||
|
||
self.decoder.codingPath.append(_CleanJSONKey(index: self.currentIndex)) | ||
self.decoder.codingPath.append(CleanJSONKey(index: self.currentIndex)) | ||
defer { self.decoder.codingPath.removeLast() } | ||
|
||
guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: Int.self) else { | ||
|
@@ -127,7 +127,7 @@ struct _CleanJSONUnkeyedDecodingContainer : UnkeyedDecodingContainer { | |
return Int8.defaultValue | ||
} | ||
|
||
self.decoder.codingPath.append(_CleanJSONKey(index: self.currentIndex)) | ||
self.decoder.codingPath.append(CleanJSONKey(index: self.currentIndex)) | ||
defer { self.decoder.codingPath.removeLast() } | ||
|
||
guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: Int8.self) else { | ||
|
@@ -143,7 +143,7 @@ struct _CleanJSONUnkeyedDecodingContainer : UnkeyedDecodingContainer { | |
return Int16.defaultValue | ||
} | ||
|
||
self.decoder.codingPath.append(_CleanJSONKey(index: self.currentIndex)) | ||
self.decoder.codingPath.append(CleanJSONKey(index: self.currentIndex)) | ||
defer { self.decoder.codingPath.removeLast() } | ||
|
||
guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: Int16.self) else { | ||
|
@@ -159,7 +159,7 @@ struct _CleanJSONUnkeyedDecodingContainer : UnkeyedDecodingContainer { | |
return Int32.defaultValue | ||
} | ||
|
||
self.decoder.codingPath.append(_CleanJSONKey(index: self.currentIndex)) | ||
self.decoder.codingPath.append(CleanJSONKey(index: self.currentIndex)) | ||
defer { self.decoder.codingPath.removeLast() } | ||
|
||
guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: Int32.self) else { | ||
|
@@ -175,7 +175,7 @@ struct _CleanJSONUnkeyedDecodingContainer : UnkeyedDecodingContainer { | |
return Int64.defaultValue | ||
} | ||
|
||
self.decoder.codingPath.append(_CleanJSONKey(index: self.currentIndex)) | ||
self.decoder.codingPath.append(CleanJSONKey(index: self.currentIndex)) | ||
defer { self.decoder.codingPath.removeLast() } | ||
|
||
guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: Int64.self) else { | ||
|
@@ -191,7 +191,7 @@ struct _CleanJSONUnkeyedDecodingContainer : UnkeyedDecodingContainer { | |
return try decode(isAtEnd: true) | ||
} | ||
|
||
self.decoder.codingPath.append(_CleanJSONKey(index: self.currentIndex)) | ||
self.decoder.codingPath.append(CleanJSONKey(index: self.currentIndex)) | ||
defer { self.decoder.codingPath.removeLast() } | ||
|
||
guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: UInt.self) else { | ||
|
@@ -224,7 +224,7 @@ struct _CleanJSONUnkeyedDecodingContainer : UnkeyedDecodingContainer { | |
return UInt8.defaultValue | ||
} | ||
|
||
self.decoder.codingPath.append(_CleanJSONKey(index: self.currentIndex)) | ||
self.decoder.codingPath.append(CleanJSONKey(index: self.currentIndex)) | ||
defer { self.decoder.codingPath.removeLast() } | ||
|
||
guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: UInt8.self) else { | ||
|
@@ -240,7 +240,7 @@ struct _CleanJSONUnkeyedDecodingContainer : UnkeyedDecodingContainer { | |
return UInt16.defaultValue | ||
} | ||
|
||
self.decoder.codingPath.append(_CleanJSONKey(index: self.currentIndex)) | ||
self.decoder.codingPath.append(CleanJSONKey(index: self.currentIndex)) | ||
defer { self.decoder.codingPath.removeLast() } | ||
|
||
guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: UInt16.self) else { | ||
|
@@ -256,7 +256,7 @@ struct _CleanJSONUnkeyedDecodingContainer : UnkeyedDecodingContainer { | |
return UInt32.defaultValue | ||
} | ||
|
||
self.decoder.codingPath.append(_CleanJSONKey(index: self.currentIndex)) | ||
self.decoder.codingPath.append(CleanJSONKey(index: self.currentIndex)) | ||
defer { self.decoder.codingPath.removeLast() } | ||
|
||
guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: UInt32.self) else { | ||
|
@@ -272,7 +272,7 @@ struct _CleanJSONUnkeyedDecodingContainer : UnkeyedDecodingContainer { | |
return UInt64.defaultValue | ||
} | ||
|
||
self.decoder.codingPath.append(_CleanJSONKey(index: self.currentIndex)) | ||
self.decoder.codingPath.append(CleanJSONKey(index: self.currentIndex)) | ||
defer { self.decoder.codingPath.removeLast() } | ||
|
||
guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: UInt64.self) else { | ||
|
@@ -288,7 +288,7 @@ struct _CleanJSONUnkeyedDecodingContainer : UnkeyedDecodingContainer { | |
return try decode(isAtEnd: true) | ||
} | ||
|
||
self.decoder.codingPath.append(_CleanJSONKey(index: self.currentIndex)) | ||
self.decoder.codingPath.append(CleanJSONKey(index: self.currentIndex)) | ||
defer { self.decoder.codingPath.removeLast() } | ||
|
||
guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: Float.self) else { | ||
|
@@ -321,7 +321,7 @@ struct _CleanJSONUnkeyedDecodingContainer : UnkeyedDecodingContainer { | |
return try decode(isAtEnd: true) | ||
} | ||
|
||
self.decoder.codingPath.append(_CleanJSONKey(index: self.currentIndex)) | ||
self.decoder.codingPath.append(CleanJSONKey(index: self.currentIndex)) | ||
defer { self.decoder.codingPath.removeLast() } | ||
|
||
guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: Double.self) else { | ||
|
@@ -354,7 +354,7 @@ struct _CleanJSONUnkeyedDecodingContainer : UnkeyedDecodingContainer { | |
return try decode(isAtEnd: true) | ||
} | ||
|
||
self.decoder.codingPath.append(_CleanJSONKey(index: self.currentIndex)) | ||
self.decoder.codingPath.append(CleanJSONKey(index: self.currentIndex)) | ||
defer { self.decoder.codingPath.removeLast() } | ||
|
||
guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: String.self) else { | ||
|
@@ -384,22 +384,22 @@ struct _CleanJSONUnkeyedDecodingContainer : UnkeyedDecodingContainer { | |
|
||
public mutating func decode<T : Decodable>(_ type: T.Type) throws -> T { | ||
guard !self.isAtEnd else { | ||
throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_CleanJSONKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end.")) | ||
throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [CleanJSONKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end.")) | ||
} | ||
|
||
self.decoder.codingPath.append(_CleanJSONKey(index: self.currentIndex)) | ||
self.decoder.codingPath.append(CleanJSONKey(index: self.currentIndex)) | ||
defer { self.decoder.codingPath.removeLast() } | ||
|
||
guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: type) else { | ||
throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_CleanJSONKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead.")) | ||
throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [CleanJSONKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead.")) | ||
} | ||
|
||
self.currentIndex += 1 | ||
return decoded | ||
} | ||
|
||
public mutating func nestedContainer<NestedKey>(keyedBy type: NestedKey.Type) throws -> KeyedDecodingContainer<NestedKey> { | ||
self.decoder.codingPath.append(_CleanJSONKey(index: self.currentIndex)) | ||
self.decoder.codingPath.append(CleanJSONKey(index: self.currentIndex)) | ||
defer { self.decoder.codingPath.removeLast() } | ||
|
||
guard !self.isAtEnd else { | ||
|
@@ -437,12 +437,12 @@ struct _CleanJSONUnkeyedDecodingContainer : UnkeyedDecodingContainer { | |
} | ||
|
||
private func nestedContainer<NestedKey>(wrapping dictionary: [String: Any] = [:]) -> KeyedDecodingContainer<NestedKey> { | ||
let container = _CleanJSONKeyedDecodingContainer<NestedKey>(referencing: self.decoder, wrapping: dictionary) | ||
let container = CleanJSONKeyedDecodingContainer<NestedKey>(referencing: self.decoder, wrapping: dictionary) | ||
return KeyedDecodingContainer(container) | ||
} | ||
|
||
public mutating func nestedUnkeyedContainer() throws -> UnkeyedDecodingContainer { | ||
self.decoder.codingPath.append(_CleanJSONKey(index: self.currentIndex)) | ||
self.decoder.codingPath.append(CleanJSONKey(index: self.currentIndex)) | ||
defer { self.decoder.codingPath.removeLast() } | ||
|
||
guard !self.isAtEnd else { | ||
|
@@ -461,7 +461,7 @@ struct _CleanJSONUnkeyedDecodingContainer : UnkeyedDecodingContainer { | |
debugDescription: "Cannot get keyed decoding container -- found null value instead.") | ||
case .useEmptyContainer: | ||
self.currentIndex += 1 | ||
return _CleanJSONUnkeyedDecodingContainer(referencing: self.decoder, wrapping: []) | ||
return CleanJSONUnkeyedDecodingContainer(referencing: self.decoder, wrapping: []) | ||
} | ||
} | ||
|
||
|
@@ -471,16 +471,16 @@ struct _CleanJSONUnkeyedDecodingContainer : UnkeyedDecodingContainer { | |
throw DecodingError._typeMismatch(at: self.codingPath, expectation: [Any].self, reality: value) | ||
case .useEmptyContainer: | ||
self.currentIndex += 1 | ||
return _CleanJSONUnkeyedDecodingContainer(referencing: self.decoder, wrapping: []) | ||
return CleanJSONUnkeyedDecodingContainer(referencing: self.decoder, wrapping: []) | ||
} | ||
} | ||
|
||
self.currentIndex += 1 | ||
return _CleanJSONUnkeyedDecodingContainer(referencing: self.decoder, wrapping: array) | ||
return CleanJSONUnkeyedDecodingContainer(referencing: self.decoder, wrapping: array) | ||
} | ||
|
||
public mutating func superDecoder() throws -> Decoder { | ||
self.decoder.codingPath.append(_CleanJSONKey(index: self.currentIndex)) | ||
self.decoder.codingPath.append(CleanJSONKey(index: self.currentIndex)) | ||
defer { self.decoder.codingPath.removeLast() } | ||
|
||
guard !self.isAtEnd else { | ||
|
This file contains 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.