-
Notifications
You must be signed in to change notification settings - Fork 677
Collapse file tree
Files
Search this repository
/
Copy pathREADME.md
More file actions
More file actions
Latest commit
697 lines (500 loc) · 19.1 KB
/
README.md
File metadata and controls
697 lines (500 loc) · 19.1 KB
Edit and raw actions
OlderNewer
1
# HandyJSON
2
3
***To deal with crash on iOS 15 beta3 please try version 5.0.4-beta***
4
5
HandyJSON is a framework written in Swift which to make converting model objects( **pure classes/structs** ) to and from JSON easy on iOS.
6
7
Compared with others, the most significant feature of HandyJSON is that it does not require the objects inherit from NSObject(**not using KVC but reflection**), neither implements a 'mapping' function(**writing value to memory directly to achieve property assignment**).
8
9
HandyJSON is totally depend on the memory layout rules infered from Swift runtime code. We are watching it and will follow every bit if it changes.
10
11
[](https://travis-ci.org/alibaba/HandyJSON)
12
[](https://github.com/Carthage/Carthage)
13
[](http://cocoadocs.org/docsets/HandyJSON)
14
[](http://cocoadocs.org/docsets/HandyJSON)
15
[](https://codecov.io/gh/alibaba/HandyJSON/branch/master)
16
17
## [中文文档](./README_cn.md)
18
19
## 交流群
20
21
群号: 581331250
22
23

24
25
## Sample Code
26
27
### Deserialization
28
29
```swift
30
class BasicTypes: HandyJSON {
31
var int: Int = 2
32
var doubleOptional: Double?
33
var stringImplicitlyUnwrapped: String!
34
35
required init() {}
36
}
37
38
let jsonString = "{\"doubleOptional\":1.1,\"stringImplicitlyUnwrapped\":\"hello\",\"int\":1}"
39
if let object = BasicTypes.deserialize(from: jsonString) {
40
print(object.int)
41
print(object.doubleOptional!)
42
print(object.stringImplicitlyUnwrapped)
43
}
44
```
45
46
### Serialization
47
48
```swift
49
50
let object = BasicTypes()
51
object.int = 1
52
object.doubleOptional = 1.1
53
object.stringImplicitlyUnwrapped = “hello"
54
55
print(object.toJSON()!) // serialize to dictionary
56
print(object.toJSONString()!) // serialize to JSON string
57
print(object.toJSONString(prettyPrint: true)!) // serialize to pretty JSON string
58
```
59
60
# Content
61
62
- [Features](#features)
63
- [Requirements](#requirements)
64
- [Installation](#installation)
65
- [Cocoapods](#cocoapods)
66
- [Carthage](#carthage)
67
- [Manually](#manually)
68
- [Deserialization](#deserialization)
69
- [The Basics](#the-basics)
70
- [Support Struct](#support-struct)
71
- [Support Enum Property](#support-enum-property)
72
- [Optional/ImplicitlyUnwrappedOptional/Collections/...](#optionalimplicitlyunwrappedoptionalcollections)
73
- [Designated Path](#designated-path)
74
- [Composition Object](#composition-object)
75
- [Inheritance Object](#inheritance-object)
76
- [JSON Array](#json-array)
77
- [Mapping From Dictionary](#mapping-from-dictionary)
78
- [Custom Mapping](#custom-mapping)
79
- [Date/Data/URL/Decimal/Color](#datedataurldecimalcolor)
80
- [Exclude Property](#exclude-property)
81
- [Update Existing Model](#update-existing-model)
82
- [Supported Property Type](#supported-property-type)
83
- [Serialization](#serialization)
84
- [The Basics](#the-basics)
85
- [Mapping And Excluding](#mapping-and-excluding)
86
- [FAQ](#faq)
87
- [To Do](#to-do)
88
89
# Features
90
91
* Serialize/Deserialize Object/JSON to/From JSON/Object
92
93
* Naturally use object property name for mapping, no need to specify a mapping relationship
94
95
* Support almost all types in Swift, including enum
96
97
* Support struct
98
99
* Custom transformations
100
101
* Type-Adaption, such as string json field maps to int property, int json field maps to string property