Skip to content

Commit 0632869

Browse files
committed
JSON should also allow key to contain dots.
1 parent b2e7f7a commit 0632869

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

KakaJSON/Extension/Dictionary+KJ.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ extension Dictionary where Key == String {
7676
}
7777

7878
private func _value(stringKey: String) -> Any? {
79+
if let value = self[stringKey] {
80+
return value
81+
}
82+
7983
let subkeys = stringKey.split(separator: ".")
8084
var value: Any? = self
8185
for subKey in subkeys {

KakaJSONTests/JSON_To_Model/JTM_05_KeyMapping.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,31 @@ class JTM_05_KeyMapping: XCTestCase {
234234
XCTAssert(dog.toy?.price == toy.price)
235235
}
236236

237+
func testComplex_JSONKeyWithDot() {
238+
struct Team: Convertible {
239+
var name: String?
240+
var captainName: String?
241+
242+
func kj_modelKey(from property: Property) -> ModelPropertyKey {
243+
switch property.name {
244+
case "captainName": return "captain.name"
245+
default: return property.name
246+
}
247+
}
248+
}
249+
250+
let teamName = "V"
251+
let captainName = "Quentin"
252+
253+
let json: [String: Any] = [
254+
"name": teamName,
255+
"captain.name": captainName,
256+
]
257+
let team = json.kj.model(Team.self)
258+
XCTAssert(team.name == teamName)
259+
XCTAssert(team.captainName == captainName)
260+
}
261+
237262
func testConfig1() {
238263
// Global Config
239264
ConvertibleConfig.setModelKey { property in

0 commit comments

Comments
 (0)