https://github.com/yageek/ygsonencoder
:dart: Swift reimplementation of JSONEncoder
https://github.com/yageek/ygsonencoder
codable json swift
Last synced: about 2 months ago
JSON representation
:dart: Swift reimplementation of JSONEncoder
- Host: GitHub
- URL: https://github.com/yageek/ygsonencoder
- Owner: yageek
- License: mit
- Created: 2019-11-07T20:35:40.000Z (over 6 years ago)
- Default Branch: develop
- Last Pushed: 2020-06-22T11:22:53.000Z (about 6 years ago)
- Last Synced: 2025-01-26T02:28:41.847Z (over 1 year ago)
- Topics: codable, json, swift
- Language: Swift
- Homepage:
- Size: 21.5 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# YGSONEncoder
A reimplementation of JSONEncoder to have `sortedKeys` on pre-iOS 11 (not based on `JSONSerialization`).
Code largely inspired by:
- [apple/swift](https://github.com/apple/swift) for snakeCaseConversion
- [apple/swift-corelibs-foundation](https://github.com/apple/swift-corelibs-foundation) for pretty printing
## Current features
- [X] `DateEncodingStrategy`
- [X] `DataEncodingStrategy`
- [X] `OutputFormatting`
- [ ] `KeyEncodingStrategy`
- [ ] `JSONEncoder.NonConformingFloatEncodingStrategy`
- [ ] Class inheritance encoding
## Usage
```swift
struct TestUnsortedStruct: Codable {
let z: String
let b: String
let r: String
let c: String
}
let element = TestUnsortedStruct(z: "1", b: "2", r: "3", c: "4")
let encoder = YGSONEncoder()
encoder.outputFormatting = [.sortedKeys]
do {
let data = try encoder.encode(element)
} catch let error {
print("Error: \(error)")
}
```