https://github.com/alickbass/lucjson
Pure swift JSON Serialization
https://github.com/alickbass/lucjson
json json-parser jsonserializer
Last synced: 2 months ago
JSON representation
Pure swift JSON Serialization
- Host: GitHub
- URL: https://github.com/alickbass/lucjson
- Owner: alickbass
- License: mit
- Created: 2017-03-10T09:51:15.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-03-14T15:10:52.000Z (over 9 years ago)
- Last Synced: 2026-05-02T01:34:26.051Z (2 months ago)
- Topics: json, json-parser, jsonserializer
- Language: Swift
- Size: 33.2 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# LucJSON
Luc JSON is a library that uses [JSONSerialization's](https://github.com/apple/swift-corelibs-foundation/blob/master/Foundation/NSJSONSerialization.swift) implementation, but replaces all the `Any` and uses `JSON` type.
[](https://github.com/Carthage/Carthage)
[](https://codecov.io/gh/alickbass/LucJSON)
[](https://travis-ci.org/alickbass/LucJSON)
## Why remove Any?
The fact that `JSONSerialization` returns `Any` is the biggest lie ever, as [JSON](http://www.json.org) defines explicitly what can be represented as `JSON` and what cannot. That is why `JSON` in this library is the following `enum`:
```swift
enum JSON {
case null
case number(NSNumber)
case bool(Bool)
case string(String)
case object([String: JSON])
case array([JSON])
}
```
**Moreover, there is also performance implications when using `Any`**:
Whenever you do the following:
```swift
let json: Any = //JSON from the JSONSerialization
let object: [String: Any]? = json as? [String: Any]
```
It has to go through the whole object, to make sure that all the keys are `String`. Which is `O(n)` time complexity where `n` is the number of keys in the `JSON` object.
In the `LucJSON` we achieve with the following code:
```swift
let json: JSON = //JSON from the JSON.Serialization
let object: [String: JSON]? = json.object
```
As the `JSON` is `enum` it is now accomplished with a regular case check and there is no need to go through all the keys, as the keys can only be `String` in the `enum`