Skip to content

Commit

Permalink
优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
Mccc committed Sep 26, 2024
1 parent 1bab7c6 commit 1d21d70
Show file tree
Hide file tree
Showing 8 changed files with 736 additions and 706 deletions.
974 changes: 489 additions & 485 deletions Example/Pods/Pods.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,7 @@ extension JSONDecoderImpl.KeyedContainer {

// 检查 SmartPublished 包装器类型
if let publishedType = T.self as? any SmartPublishedProtocol.Type,
let wrappedValue = decoded as? Any,
let publishedValue = publishedType.createInstance(with: wrappedValue) as? T {
let publishedValue = publishedType.createInstance(with: decoded) as? T {
return publishedValue
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ extension Bool: TypeTransformable {
case .string(let string):
if ["1","YES","Yes","yes","TRUE","True","true"].contains(string) { return true }
if ["0","NO","No","no","FALSE","False","false"].contains(string) { return false }
case .number(let number):
case .number(_):
if let int = try? impl.unwrapFixedWidthInteger(from: value, as: Int.self) {
if int == 1 {
return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ extension _SpecialTreatmentEncoder {
return value
}


switch self.options.dateEncodingStrategy {
case .deferredToDate:
let encoder = self.getEncoder(for: additionalKey)
Expand Down Expand Up @@ -112,6 +111,10 @@ extension _SpecialTreatmentEncoder {
try closure(date, encoder)
// The closure didn't encode anything. Return the default keyed container.
return encoder.value ?? .object([:])
@unknown default:
let encoder = self.getEncoder(for: additionalKey)
try date.encode(to: encoder)
return encoder.value ?? .null
}
}

Expand All @@ -131,6 +134,10 @@ extension _SpecialTreatmentEncoder {
try closure(data, encoder)
// The closure didn't encode anything. Return the default keyed container.
return encoder.value ?? .object([:])
@unknown default:
let encoder = self.getEncoder(for: additionalKey)
try data.encode(to: encoder)
return encoder.value ?? .null
}
}

Expand Down Expand Up @@ -167,6 +174,8 @@ extension _SpecialTreatmentEncoder {
extension _SpecialTreatmentEncoder {
internal func _converted(_ key: CodingKey) -> CodingKey {

var newKey = key

var useMappedKeys = false
if let key = CodingUserInfoKey.useMappedKeys {
useMappedKeys = impl.userInfo[key] as? Bool ?? false
Expand All @@ -175,25 +184,29 @@ extension _SpecialTreatmentEncoder {
if let objectType = impl.cache.cacheType {
if let mappings = objectType.mappingForKey() {
for mapping in mappings {
if mapping.to.stringValue == key.stringValue {
if mapping.to.stringValue == newKey.stringValue {
if useMappedKeys, let first = mapping.from.first {
return _JSONKey.init(stringValue: first, intValue: nil)
newKey = _JSONKey.init(stringValue: first, intValue: nil)
} else {
return mapping.to
newKey = mapping.to
}
}
}
}
}

switch self.options.keyEncodingStrategy {
case .toSnakeCase:
let newKeyString = SmartJSONEncoder.SmartKeyEncodingStrategy._convertToSnakeCase(newKey.stringValue)
return _JSONKey(stringValue: newKeyString, intValue: newKey.intValue)
case .firstLetterLower:
let newKeyString = SmartJSONEncoder.SmartKeyEncodingStrategy._convertFirstLetterToLowercase(newKey.stringValue)
return _JSONKey(stringValue: newKeyString, intValue: newKey.intValue)
case .firstLetterUpper:
let newKeyString = SmartJSONEncoder.SmartKeyEncodingStrategy._convertFirstLetterToUppercase(newKey.stringValue)
return _JSONKey(stringValue: newKeyString, intValue: newKey.intValue)
case .useDefaultKeys:
return key
case .convertToSnakeCase:
let newKeyString = SmartJSONEncoder.KeyEncodingStrategy._convertToSnakeCase(key.stringValue)
return _JSONKey(stringValue: newKeyString, intValue: key.intValue)
case .custom(let converter):
return converter(codingPath + [key])
return newKey
}
}
}
179 changes: 7 additions & 172 deletions SmartCodable/Classes/JSONEncoder/SmartJSONEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,180 +7,18 @@ import Foundation
//===----------------------------------------------------------------------===//

/// `JSONEncoder` facilitates the encoding of `Encodable` values into JSON.
open class SmartJSONEncoder {
// MARK: Options
open class SmartJSONEncoder: JSONEncoder {

/// The formatting of the output JSON data.
public struct OutputFormatting: OptionSet {
/// The format's default value.
public let rawValue: UInt
open var smartKeyEncodingStrategy: SmartKeyEncodingStrategy = .useDefaultKeys

/// Creates an OutputFormatting value with the given raw value.
public init(rawValue: UInt) {
self.rawValue = rawValue
}

/// Produce human-readable JSON with indented output.
public static let prettyPrinted = OutputFormatting(rawValue: 1 << 0)

/// Produce JSON with dictionary keys sorted in lexicographic order.
@available(macOS 10.13, iOS 11.0, watchOS 4.0, tvOS 11.0, *)
public static let sortedKeys = OutputFormatting(rawValue: 1 << 1)

/// By default slashes get escaped ("/" → "\/", "http://apple.com/" → "http:\/\/apple.com\/")
/// for security reasons, allowing outputted JSON to be safely embedded within HTML/XML.
/// In contexts where this escaping is unnecessary, the JSON is known to not be embedded,
/// or is intended only for display, this option avoids this escaping.
public static let withoutEscapingSlashes = OutputFormatting(rawValue: 1 << 3)
}

/// The strategy to use for encoding `Date` values.
public enum DateEncodingStrategy {
/// Defer to `Date` for choosing an encoding. This is the default strategy.
case deferredToDate

/// Encode the `Date` as a UNIX timestamp (as a JSON number).
case secondsSince1970

/// Encode the `Date` as UNIX millisecond timestamp (as a JSON number).
case millisecondsSince1970

/// Encode the `Date` as an ISO-8601-formatted string (in RFC 3339 format).
@available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *)
case iso8601

/// Encode the `Date` as a string formatted by the given formatter.
case formatted(DateFormatter)

/// Encode the `Date` as a custom value encoded by the given closure.
///
/// If the closure fails to encode a value into the given encoder, the encoder will encode an empty automatic container in its place.
case custom((Date, Encoder) throws -> Void)
}

/// The strategy to use for encoding `Data` values.
public enum DataEncodingStrategy {
/// Defer to `Data` for choosing an encoding.
case deferredToData

/// Encoded the `Data` as a Base64-encoded string. This is the default strategy.
case base64

/// Encode the `Data` as a custom value encoded by the given closure.
///
/// If the closure fails to encode a value into the given encoder, the encoder will encode an empty automatic container in its place.
case custom((Data, Encoder) throws -> Void)
}

/// The strategy to use for non-JSON-conforming floating-point values (IEEE 754 infinity and NaN).
public enum NonConformingFloatEncodingStrategy {
/// Throw upon encountering non-conforming values. This is the default strategy.
case `throw`

/// Encode the values using the given representation strings.
case convertToString(positiveInfinity: String, negativeInfinity: String, nan: String)
}

/// The strategy to use for automatically changing the value of keys before encoding.
public enum KeyEncodingStrategy {
/// Use the keys specified by each type. This is the default strategy.
case useDefaultKeys

/// Convert from "camelCaseKeys" to "snake_case_keys" before writing a key to JSON payload.
///
/// Capital characters are determined by testing membership in `CharacterSet.uppercaseLetters` and `CharacterSet.lowercaseLetters` (Unicode General Categories Lu and Lt).
/// The conversion to lower case uses `Locale.system`, also known as the ICU "root" locale. This means the result is consistent regardless of the current user's locale and language preferences.
///
/// Converting from camel case to snake case:
/// 1. Splits words at the boundary of lower-case to upper-case
/// 2. Inserts `_` between words
/// 3. Lowercases the entire string
/// 4. Preserves starting and ending `_`.
///
/// For example, `oneTwoThree` becomes `one_two_three`. `_oneTwoThree_` becomes `_one_two_three_`.
///
/// - Note: Using a key encoding strategy has a nominal performance cost, as each string key has to be converted.
case convertToSnakeCase

/// Provide a custom conversion to the key in the encoded JSON from the keys specified by the encoded types.
/// The full path to the current encoding position is provided for context (in case you need to locate this key within the payload). The returned key is used in place of the last component in the coding path before encoding.
/// If the result of the conversion is a duplicate key, then only one value will be present in the result.
case custom((_ codingPath: [CodingKey]) -> CodingKey)

static func _convertToSnakeCase(_ stringKey: String) -> String {
guard !stringKey.isEmpty else { return stringKey }

var words: [Range<String.Index>] = []
// The general idea of this algorithm is to split words on transition from lower to upper case, then on transition of >1 upper case characters to lowercase
//
// myProperty -> my_property
// myURLProperty -> my_url_property
//
// We assume, per Swift naming conventions, that the first character of the key is lowercase.
var wordStart = stringKey.startIndex
var searchRange = stringKey.index(after: wordStart)..<stringKey.endIndex

// Find next uppercase character
while let upperCaseRange = stringKey.rangeOfCharacter(from: CharacterSet.uppercaseLetters, options: [], range: searchRange) {
let untilUpperCase = wordStart..<upperCaseRange.lowerBound
words.append(untilUpperCase)

// Find next lowercase character
searchRange = upperCaseRange.lowerBound..<searchRange.upperBound
guard let lowerCaseRange = stringKey.rangeOfCharacter(from: CharacterSet.lowercaseLetters, options: [], range: searchRange) else {
// There are no more lower case letters. Just end here.
wordStart = searchRange.lowerBound
break
}

// Is the next lowercase letter more than 1 after the uppercase? If so, we encountered a group of uppercase letters that we should treat as its own word
let nextCharacterAfterCapital = stringKey.index(after: upperCaseRange.lowerBound)
if lowerCaseRange.lowerBound == nextCharacterAfterCapital {
// The next character after capital is a lower case character and therefore not a word boundary.
// Continue searching for the next upper case for the boundary.
wordStart = upperCaseRange.lowerBound
} else {
// There was a range of >1 capital letters. Turn those into a word, stopping at the capital before the lower case character.
let beforeLowerIndex = stringKey.index(before: lowerCaseRange.lowerBound)
words.append(upperCaseRange.lowerBound..<beforeLowerIndex)

// Next word starts at the capital before the lowercase we just found
wordStart = beforeLowerIndex
}
searchRange = lowerCaseRange.upperBound..<searchRange.upperBound
}
words.append(wordStart..<searchRange.upperBound)
let result = words.map({ (range) in
return stringKey[range].lowercased()
}).joined(separator: "_")
return result
}
}

/// The output format to produce. Defaults to `[]`.
open var outputFormatting: OutputFormatting = []

/// The strategy to use in encoding dates. Defaults to `.deferredToDate`.
open var dateEncodingStrategy: DateEncodingStrategy = .deferredToDate

/// The strategy to use in encoding binary data. Defaults to `.base64`.
open var dataEncodingStrategy: DataEncodingStrategy = .base64

/// The strategy to use in encoding non-conforming numbers. Defaults to `.throw`.
open var nonConformingFloatEncodingStrategy: NonConformingFloatEncodingStrategy = .throw

/// The strategy to use for encoding keys. Defaults to `.useDefaultKeys`.
open var keyEncodingStrategy: KeyEncodingStrategy = .useDefaultKeys

/// Contextual user-provided information for use during encoding.
open var userInfo: [CodingUserInfoKey: Any] = [:]


/// Options set on the top-level encoder to pass down the encoding hierarchy.
struct _Options {
let dateEncodingStrategy: DateEncodingStrategy
let dataEncodingStrategy: DataEncodingStrategy
let nonConformingFloatEncodingStrategy: NonConformingFloatEncodingStrategy
let keyEncodingStrategy: KeyEncodingStrategy
let keyEncodingStrategy: SmartKeyEncodingStrategy
let userInfo: [CodingUserInfoKey: Any]
}

Expand All @@ -189,14 +27,10 @@ open class SmartJSONEncoder {
return _Options(dateEncodingStrategy: dateEncodingStrategy,
dataEncodingStrategy: dataEncodingStrategy,
nonConformingFloatEncodingStrategy: nonConformingFloatEncodingStrategy,
keyEncodingStrategy: keyEncodingStrategy,
keyEncodingStrategy: smartKeyEncodingStrategy,
userInfo: userInfo)
}

// MARK: - Constructing a JSON Encoder

/// Initializes `self` with default strategies.
public init() {}

// MARK: - Encoding Values

Expand All @@ -206,7 +40,7 @@ open class SmartJSONEncoder {
/// - returns: A new `Data` value containing the encoded JSON data.
/// - throws: `EncodingError.invalidValue` if a non-conforming floating-point value is encountered during encoding, and the encoding strategy is `.throw`.
/// - throws: An error if any value throws an error during encoding.
open func encode<T: Encodable>(_ value: T) throws -> Data {
open override func encode<T: Encodable>(_ value: T) throws -> Data {
let value: JSONValue = try encodeAsJSONValue(value)
let writer = JSONValue.Writer(options: self.outputFormatting)
let bytes = writer.writeValue(value)
Expand Down Expand Up @@ -254,3 +88,4 @@ extension CodingUserInfoKey {
/// 是否使用映射之后的key
static var useMappedKeys = CodingUserInfoKey.init(rawValue: "Stamrt.useMappedKeys")
}

Loading

0 comments on commit 1d21d70

Please sign in to comment.