An open API service indexing awesome lists of open source software.

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

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)")
}
```