https://github.com/peheje/JsonSerializerSwift
A simple Json Serializer for Swift
https://github.com/peheje/JsonSerializerSwift
json-serialization swift
Last synced: about 1 year ago
JSON representation
A simple Json Serializer for Swift
- Host: GitHub
- URL: https://github.com/peheje/JsonSerializerSwift
- Owner: peheje
- License: mit
- Created: 2015-10-13T11:19:59.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2020-03-15T08:45:39.000Z (over 6 years ago)
- Last Synced: 2024-08-01T13:30:34.627Z (almost 2 years ago)
- Topics: json-serialization, swift
- Language: Swift
- Homepage:
- Size: 78.1 KB
- Stars: 80
- Watchers: 9
- Forks: 35
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# JsonSerializerSwift
A simple Json Serializer for Swift
**Update 14-06-2017:**
Merged a pull request, now arrays of primitives are no longer enclosed in tuborg clamps {}. Updated tests to reflect this. Might be a breaking change, but it is fixing a bug.
**Update 14-09-2016:**
Works with Swift 3.0. If you work with Swift 2.0 commit 921763ba36dbf1ce710f615d4c2ef838d2b4af3b will work. But I'm not planning on creating separate branches for Swift V2 and V3, so updates from now on will only be added to Swift 3.0 version.
**About**
Parse your Swift object instances to a JSON string. This is a simple one-to-one conversion atm. Pretty print is supported. Properties will be named the same as in your classes as in the JSON string.
Uses reflection. On a MBP 15 Ultimo 2013 I'm getting about 1.5 sec for serializing a simple three-layer-inheritance object with 10000 array objects.
**To use**
Here a simple test case is shown. For more tests see the JsonSerializerSwiftTests file, git clone to run them yourself :)
```swift
//Arrange your model classes
class Object {
var id: Int = 182371823
}
class Animal: Object {
var weight: Double = 2.5
var age: Int = 2
var name: String? = "An animal"
}
class Cat: Animal {
var fur: Bool = true
}
let m = Cat()
//Act
let json = JSONSerializer.toJson(m)
//Assert
let expected = "{\"fur\": true, \"weight\": 2.5, \"age\": 2, \"name\": \"An animal\", \"id\": 182371823}"
stringCompareHelper(json, expected) //returns true
```
Currently supports standard types, optional standard types, arrays, arrays of nullables standard types, array of custom classes, inheritance, composition of custom objects, structs.