Swiftpack.co - Package - tris-foundation/json

JSON

JSON Encoder & Decoder for Swift 4.

Package.swift

.package(url: "https://github.com/tris-foundation/json.git", .branch("master"))

Usage

Convenience API

let bytes = JSON.encode(Model())
let model = JSON.decode(Model.self, from: bytes)

Streaming API

let output = OutputByteStream()
JSON.encode(Model(), to: output)

let input = InputByteStream(output.bytes)
let model = JSON.decode(Model.self, from: input)

Encoder / Decoder + Streaming API

let bytes = JSONEncoder().encode(Model())
JSONEncoder().encode(Model(), to: stream)
...

JSON Value

public struct JSON {
    public enum Value {
        case null
        case bool(Bool)
        case number(Number)
        case string(String)
        case array([JSON.Value])
        case object([String : JSON.Value])

        public enum Number {
            case int(Int)
            case uint(UInt)
            case double(Double)
        }
    }
}

Performance

{"message":"Hello, World!"}

JSON.JSONEncoder: 934 644 tasks/sec
Foundation.JSONEncoder: 92 619 tasks/sec

JSON.JSONDecoder: 236 062 tasks/sec
Foundation.JSONDecoder: 226 515 tasks/sec

{"message":"\u3053\u3093\u306B\u3061\u306F\u4E16\u754C\uFF01"}

JSON.JSONDecoder: 179 440 tasks/sec
Foundation.JSONDecoder: 88 614 tasks/sec