Skip to content
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

add support for snake to camel #485

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions Source/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public struct DeserializeOptions: OptionSet {
public let rawValue: Int

public static let caseInsensitive = DeserializeOptions(rawValue: 1 << 0)

public static let snakeToCamel = DeserializeOptions(rawValue: 1 << 1)

public static let defaultOptions: DeserializeOptions = []

Expand Down
68 changes: 57 additions & 11 deletions Source/ExtendCustomModelType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,65 @@ public protocol _ExtendCustomModelType: _Transformable {
mutating func willStartMapping()
mutating func mapping(mapper: HelpingMapper)
mutating func didFinishMapping()
func snakeToCamel() -> Bool
}

extension _ExtendCustomModelType {

public mutating func willStartMapping() {}
public mutating func mapping(mapper: HelpingMapper) {}
public mutating func didFinishMapping() {}
public func snakeToCamel() -> Bool {
return HandyJSONConfiguration.deserializeOptions.contains(.snakeToCamel)
}
}

fileprivate func convertKeyIfNeeded(dict: [String: Any]) -> [String: Any] {
if HandyJSONConfiguration.deserializeOptions.contains(.caseInsensitive) {
var newDict = [String: Any]()
fileprivate func convertKeyIfNeeded(dict: [String: Any], snakeToCamel: Bool) -> [String: Any] {
var newDict = [String: Any]()
if snakeToCamel {
dict.forEach({ (kvPair) in
let (key, value) = kvPair
newDict[key.lowercased()] = value
newDict[deserializeSnakeToCamel(input: key)] = value
})
return newDict
} else {
newDict = dict
}
return dict
if HandyJSONConfiguration.deserializeOptions.contains(.caseInsensitive) {
var _newDict = newDict
newDict.forEach({ (kvPair) in
let (key, value) = kvPair
_newDict[key.lowercased()] = value
})
return _newDict
}
return newDict
}

fileprivate func deserializeSnakeToCamel(input: String) -> String {
var result = ""
var capitalizeNext = false
for char in input {
if char == "_" {
capitalizeNext = true
} else if capitalizeNext {
result.append(char.uppercased())
capitalizeNext = false
} else {
result.append(char)
}
}
return result
}

fileprivate func serializeCamelToSnake(input: String) -> String {
var result = ""
for (index,character) in input.enumerated() {
if character.isUppercase && index > 0 {
result.append("_")
}
result.append(character.lowercased())
}
return result
}

fileprivate func getRawValueFrom(dict: [String: Any], property: PropertyInfo, mapper: HelpingMapper) -> Any? {
Expand Down Expand Up @@ -90,15 +130,21 @@ fileprivate func readAllChildrenFrom(mirror: Mirror) -> [(String, Any)] {
return result
}

fileprivate func merge(children: [(String, Any)], propertyInfos: [PropertyInfo]) -> [String: (Any, PropertyInfo?)] {
fileprivate func merge(children: [(String, Any)], propertyInfos: [PropertyInfo], snakeToCamel: Bool) -> [String: (Any, PropertyInfo?)] {
var infoDict = [String: PropertyInfo]()
propertyInfos.forEach { (info) in
infoDict[info.key] = info
}

var result = [String: (Any, PropertyInfo?)]()
children.forEach { (child) in
result[child.0] = (child.1, infoDict[child.0])
let key: String
if snakeToCamel {
key = serializeCamelToSnake(input: child.0)
} else {
key = child.0
}
result[key] = (child.1, infoDict[child.0])
}
return result
}
Expand Down Expand Up @@ -149,7 +195,7 @@ extension _ExtendCustomModelType {
InternalLogger.logVerbose("instance start at: ", Int(bitPattern: rawPointer))

// process dictionary
let _dict = convertKeyIfNeeded(dict: dict)
let _dict = convertKeyIfNeeded(dict: dict, snakeToCamel: instance.snakeToCamel())

let instanceIsNsObject = instance.isNSObjectType()
let bridgedPropertyList = instance.getBridgedPropertyList()
Expand Down Expand Up @@ -220,7 +266,7 @@ extension _ExtendCustomModelType {

mutableObject.mapping(mapper: mapper)

let requiredInfo = merge(children: children, propertyInfos: propertyInfos)
let requiredInfo = merge(children: children, propertyInfos: propertyInfos, snakeToCamel: mutableObject.snakeToCamel())

return _serializeModelObject(instance: mutableObject, properties: requiredInfo, mapper: mapper) as Any
default:
Expand Down
13 changes: 13 additions & 0 deletions Tests/HandyJSONTests/BasicTypesInClassTestsFromJSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,19 @@ class BasicTypesInClassTestsFromJSON: XCTestCase {
XCTAssertEqual(mappedObject?.boolImplicitlyUnwrapped, value)
HandyJSONConfiguration.deserializeOptions = .defaultOptions
}

func testSnakeToCamelMappingFromJSON() {
HandyJSONConfiguration.deserializeOptions = .snakeToCamel

let value: Bool = true
let JSONString = "{\"bool_optional\" : \(value)}"

let mappedObject = BasicTypesInClass.deserialize(from: JSONString)

XCTAssertNotNil(mappedObject)
XCTAssertEqual(mappedObject?.boolOptional, value)
HandyJSONConfiguration.deserializeOptions = .defaultOptions
}

func testUpdateExistModel() {
let basicObject = BasicTypesInClass()
Expand Down
21 changes: 21 additions & 0 deletions Tests/HandyJSONTests/OtherFeaturesTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -404,4 +404,25 @@ class StructObjectTest: XCTestCase {
let a = A.deserialize(from: jsonString)!
XCTAssertEqual(a.upperName, "HANDYJSON")
}

func testSnakeToCamelForStruct() {
struct A: HandyJSON {
var snakeKey: String?

func snakeToCamel() -> Bool {
return true
}
}

struct B: HandyJSON {
var camelKey: String?
}

let jsonString1 = "{\"snake_key\":\"HandyJson\"}"
let jsonString2 = "{\"camelKey\":\"HandyJson\"}"
let a = A.deserialize(from: jsonString1)!
let b = B.deserialize(from: jsonString2)!
XCTAssertEqual(a.snakeKey, "HandyJson")
XCTAssertEqual(b.camelKey, "HandyJson")
}
}