CocoaPods Compatible Build Status

JSON值

简单的 JSON 表示形式,支持下标和模式匹配。JSONValue 使用 JSON 的代数数据类型表示形式,以实现类型安全和模式匹配。

enum JSONValue: CustomStringConvertible, Hashable {
    case array([JSONValue])
    case object([String: JSONValue])
    case number(JSONNumber)
    case string(String)
    case bool(Bool)
    case null
}

public enum JSONNumber: Hashable {
    case int(Int64)
    case fraction(Double)
}

要求

支持的平台

iOS 12.0+ MacOS 10.13+

支持的语言

Swift 5.9+

安装

CocoaPods

platform :ios, '12.0'
use_frameworks!

pod 'JSONValueRX'

Swift Package Manager (SPM)

dependencies: [
    .package(url: "https://github.com/rexmas/JSONValue.git", from: "8.0.0")
]

下标

支持 . 索引

let dict = [ "fis" : [ "h" : "food" ]]
var jsonVal = try! JSONValue(object: dict)

print(jsonVal["fis.h"]) // Optional(JSONString(food))

jsonVal["awe.some"] = try! JSONValue(object: "cool")
print(jsonVal["awe.some"]) // Optional(JSONString(cool))

支持键中的 .

let dict = [ "fis.h" : "food" ]
var jsonVal = try! JSONValue(object: dict)

print(jsonVal["fis.h"]) // Optional(JSONString(food))

jsonVal[["awe.some"]] = try! JSONValue(object: "cool")
print(jsonVal["awe.some"]) // Optional(string(cool))

可Equatable

print(JSONValue.number(.int(1)) == JSONValue.number(.int(1))) // true

可Hashable

extension JSONValue: Hashable

反转的键/值对不会冲突。

let hashable1 = try! JSONValue(object: ["warp" : "drive"])
let hashable2 = try! JSONValue(object: ["drive" : "warp"])

print(hashable1.hashValue) // -7189088994080390660
print(hashable2.hashValue) // -215843780535174243

可Codable

从 JSON 解码为 JSONValue

let jsonString = """
{
    "_id": "5d140a3fb5bbd5eaa41b512e",
    "guid": "9b0f3717-2f21-4a81-8902-92d2278a92f0",
    "isActive": false,
    "age": 30
}
"""
let jsonValue = try! JSONDecoder().decode(JSONValue.self, from: jsonString.data(using: .utf8)!)

将 JSONValue 编码为 JSON

let jsonValue = JSONValue.object(["_id" : .string("5d140a3fb5bbd5eaa41b512e")])
let jsonData = try! JSONEncoder().encode(jsonValue)

从 JSONValue 解码为 Struct

let jsonValue = JSONValue.array([
    .object([
        "_id": .string("5d140a3fb5bbd5eaa41b512e"),
        "guid": .string("9b0f3717-2f21-4a81-8902-92d2278a92f0"),
        "isActive": .bool(false),
        "age": .number(.int(30)),
        "name": .object([
        "first": .string("Rosales"),
        "last": .string("Mcintosh")
        ]),
        "company": JSONValue.null,
        "latitude": .string("-58.182284"),
        "longitude": .string("-159.420718"),
        "tags": .array([
        .string("aute"),
        .string("aute")
        ])
    ])
])

struct Output: Decodable, Equatable {
    let _id: String
    let guid: String
    let isActive: Bool
    let age: Int
    let name: [String: String]
    let company: String?
    let latitude: String
    let longitude: String
    let tags: [String]
}

let output: Array<Output> = try! jsonValue.decode()

在没有 Codable 的情况下从 String, Data 编码/解码

public func encode() throws -> Data
public static func decode(_ data: Data) throws -> JSONValue
public static func decode(_ string: String) throws -> JSONValue

在没有 Codable 的情况下自定义编码/解码

public protocol JSONDecodable {
    associatedtype ConversionType = Self
    static func fromJSON(_ x: JSONValue) -> ConversionType?
}

public protocol JSONEncodable {
    associatedtype ConversionType
    static func toJSON(_ x: ConversionType) -> JSONValue
}

public protocol JSONable: JSONDecodable, JSONEncodable { }

以下支持 JSONable 以实现开箱即用的编码/解码。

NSNull
Double
Bool
Int (and varieties -> Int64, Int32, ...)
UInt (and varieties -> UInt64, UInt32, ...)
Date
NSDate
Array
String
Dictionary

Date 使用内置的 ISO 编码/解码到/从 .string

贡献

许可

请参阅 LICENSE 文件。