Skip to content

Commit cb6270b

Browse files
committed
Update MD
1 parent 07cdb01 commit cb6270b

File tree

4 files changed

+68
-14
lines changed

4 files changed

+68
-14
lines changed

CHANGELOG.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,28 @@
22

33
---
44

5-
## [1.1.0](https://github.com/kakaopensource/KakaJSON/releases/tag/1.1.0) (2019-08-27)
5+
## new
6+
- Merged pull requests
7+
- [use isUppercase to check if the character is uppercase](https://github.com/kakaopensource/KakaJSON/pull/20)
8+
- [A key should be able to contain dot](https://github.com/kakaopensource/KakaJSON/pull/21)
9+
- [use enum as namespace, as Apple does in Combine](https://github.com/kakaopensource/KakaJSON/pull/23)
10+
- [return early if possible](https://github.com/kakaopensource/KakaJSON/pull/24)
11+
- [add test testComplex_JSONKeyWithDot_bestMatch](https://github.com/kakaopensource/KakaJSON/pull/25)
12+
- [[big pr]: refactor project structure](https://github.com/kakaopensource/KakaJSON/pull/27)
13+
14+
15+
16+
## [1.1.0](https://github.com/kakaopensource/KakaJSON/releases/tag/1.1.0) (2019-08-28)
617
- Remove locks in ConvertibleConfig
718
- Simplify apis
819
- Support carthage
20+
- Keep default model value for optimization
921
- Merged pull requests
1022
- [simplify optional.kj_value](https://github.com/kakaopensource/KakaJSON/pull/16)
1123
- [replace symbol ~= with pattern keyword](https://github.com/kakaopensource/KakaJSON/pull/17)
1224

25+
26+
1327
## [1.0.0](https://github.com/kakaopensource/KakaJSON/releases/tag/1.0.0) (2019-08-23)
14-
- First public release
28+
29+
- First public release

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ XCTAssert(person.books?[1].name == "Data Structure And Algorithm")
460460
XCTAssert(person.dogs?["dog0"]?.name == "Larry")
461461
```
462462

463-
### Nested Model2
463+
### Nested Model 2
464464
```swift
465465
struct Book: Convertible, Hashable {
466466
var name: String = ""
@@ -488,7 +488,7 @@ XCTAssert(book?.name == "Fast C++")
488488
XCTAssert(book?.price == 666.6)
489489
```
490490

491-
### Nested Model3
491+
### Nested Model 3
492492
```swift
493493
struct Car: Convertible {
494494
var name: String = ""

Sources/KakaJSON/Convert/Values.swift

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ public enum Values {
2323
case is NumberValue.Type: return _number(v, type)
2424
case is StringValue.Type: return _string(v, type)
2525
case let ct as Convertible.Type: return _model(v, ct, defaultValue)
26-
case is DateValue.Type: return _date(v)
2726
case is ArrayValue.Type: return _array(v, type)
27+
case is DateValue.Type: return _date(v)
2828
case is DictionaryValue.Type: return _dictionary(v, type)
2929
case is URLValue.Type: return _url(v)
3030
case is DataValue.Type: return _data(v, type)
@@ -43,8 +43,8 @@ public enum Values {
4343
switch v {
4444
case let num as NumberValue: return _JSONValue(from: num)
4545
case let model as Convertible: return model.kj_JSONObject()
46-
case let date as Date: return date.timeIntervalSince1970
4746
case let array as [Any]: return _JSONValue(from: array)
47+
case let date as Date: return date.timeIntervalSince1970
4848
case let dict as [String: Any]: return _JSONValue(from: dict)
4949
case let url as URL: return url.absoluteString
5050
case let set as SetValue: return _JSONValue(from: set)
@@ -170,22 +170,22 @@ private extension Values {
170170
guard let str = _numberString(value) else { return nil }
171171
guard let decimal = Decimal(string: str) else { return nil }
172172

173-
// decimal
174-
if type is Decimal.Type { return decimal }
175-
176173
// digit
177174
if let digitType = type as? DigitValue.Type {
178-
guard let double = Double("\(decimal)") else { return nil }
179-
let number = NSNumber(value: double)
180-
return digitType.init(truncating: number)
175+
return Double("\(decimal)")
176+
.flatMap { NSNumber(value: $0) }
177+
.flatMap { digitType.init(truncating: $0) }
181178
}
182179

183180
// decimal number
184181
if type is NSDecimalNumber.Type {
185182
return NSDecimalNumber(decimal: decimal)
186183
}
187184

188-
// number
185+
// decimal
186+
if type is Decimal.Type { return decimal }
187+
188+
// other
189189
return Double("\(decimal)").flatMap { NSNumber(value: $0) }
190190
}
191191

@@ -411,4 +411,3 @@ postfix func ~! (_ value: Any) -> Any? {
411411
postfix func ~! (_ value: Any?) -> Any? {
412412
return (value as OptionalValue).kj_value
413413
}
414-

Tests/KakaJSONTests/JSON_To_Model/JTM_06_CustomValue.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,45 @@ class JTM_06_CustomValue: XCTestCase {
138138
XCTAssert(book1?.price == Double(books[1].price))
139139
}
140140

141+
// MARK: - NestedArray
142+
func testNestedArray() {
143+
struct Dog: Convertible {
144+
var name: String = ""
145+
var weight: Double = 0.0
146+
}
147+
148+
struct Person: Convertible {
149+
var name: String = ""
150+
var pet: [[Dog]]?
151+
func kj_modelValue(from jsonValue: Any?,
152+
_ property: Property) -> Any? {
153+
if property.name != "pet" { return jsonValue }
154+
return (jsonValue as? [[[String: Any]]])?.map {
155+
$0.kj.modelArray(Dog.self)
156+
}
157+
}
158+
}
159+
160+
let name = "Jack"
161+
let dog = (name: "Wang", weight: 109.5)
162+
163+
let json: [String: Any] = [
164+
"name": name,
165+
"pet": [
166+
[["name": dog.name, "weight": dog.weight]],
167+
[["name": dog.name, "weight": dog.weight]]
168+
]
169+
]
170+
171+
let person = json.kj.model(Person.self)
172+
XCTAssert(person.name == name)
173+
174+
XCTAssert(person.pet?[0][0].name == dog.name)
175+
XCTAssert(person.pet?[0][0].weight == dog.weight)
176+
XCTAssert(person.pet?[1][0].name == dog.name)
177+
XCTAssert(person.pet?[1][0].weight == dog.weight)
178+
}
179+
141180
// MARK: - Other
142181
func testOther1() {
143182
struct Student: Convertible {
@@ -189,6 +228,7 @@ class JTM_06_CustomValue: XCTestCase {
189228
"testDate": testDate,
190229
"testAny": testAny,
191230
"testAnyArray": testAnyArray,
231+
"testNestedArray": testNestedArray,
192232
"testOther1": testOther1,
193233
"testOther2": testOther2
194234
]

0 commit comments

Comments
 (0)