Skip to content

Commit 9b91b51

Browse files
committed
Add support for decoding PascalCased keywords
1 parent ecf1507 commit 9b91b51

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

Sources/ServiceStack/Utils.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,13 @@ public class JsonConfig {
251251
}
252252
return encoder
253253
}
254+
255+
public static var keywords = [
256+
"class", "deinit", "description", "enum", "extension", "func", "import", "init", "let", "protocol", "static",
257+
"struct", "subscript", "typealias", "associatedtype", "var", "break", "case", "continue", "default", "do",
258+
"else", "fallthrough", "if", "in", "for", "return", "switch", "where", "while", "dynamicType", "is", "new",
259+
"super", "self", "didSet", "get", "infix", "inout", "left", "mutating", "none", "nonmutating", "operator",
260+
"override", "postfix", "precedence", "prefix", "right", "set", "unowned", "weak", "willSet"]
254261

255262
public static func createDecoder() -> JSONDecoder {
256263
let decoder = JSONDecoder()
@@ -262,10 +269,13 @@ public class JsonConfig {
262269
}
263270
//remove _ prefix from @propertyWrapper properties
264271
let keyString = lastKey.stringValue
265-
// print(keyString)
266272
if keyString.starts(with: "_") {
267273
return AnyKey(stringValue: String(keyString.dropFirst())) ?? lastKey
268274
}
275+
if keywords.contains(keyString) {
276+
let useKey = keyString.prefix(1).uppercased() + keyString.dropFirst()
277+
return AnyKey(stringValue:useKey) ?? lastKey
278+
}
269279
return lastKey
270280
}
271281
decoder.dateDecodingStrategy = .custom({ (decoder) -> Date in

Tests/ServiceStackTests/JsonTests.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,22 @@ class JsonTests: XCTestCase {
8383

8484
XCTAssertEqual("http://test.servicestack.net/json/reply/TestDateTime?dateTime=" + "/Date(978310861000-0000)/".urlEncode()!, url)
8585
}
86+
87+
func test_can_Deserialize_DTO_with_keywords() {
88+
let dto = Keywords()
89+
dto.name = "name"
90+
dto.Description = "description"
91+
dto.Is = "is"
92+
93+
let json = toJson(dto)
94+
XCTAssertEqual(json,"{\"name\":\"name\",\"Is\":\"is\",\"Description\":\"description\"}")
95+
96+
let defaultJson = "{\"name\":\"name\",\"is\":\"is\",\"description\":\"description\"}"
97+
let defaultDto = fromJson(Keywords.self, defaultJson)!
98+
XCTAssertEqual(defaultDto.name, dto.name)
99+
XCTAssertEqual(defaultDto.Description, dto.Description)
100+
XCTAssertEqual(defaultDto.Is, dto.Is)
101+
}
86102
}
87103

88104
public class TestDateTime: IReturn, Codable {
@@ -91,3 +107,9 @@ public class TestDateTime: IReturn, Codable {
91107
public required init() {}
92108
public var dateTime: Date?
93109
}
110+
111+
public class Keywords : Codable {
112+
public var name: String?
113+
public var Description: String?
114+
public var Is: String?
115+
}

0 commit comments

Comments
 (0)