JSONValue
支持索引和模式匹配的简单 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 10.0+ MacOS 10.12+
支持的语言
Swift 5.0+
安装
CocoaPods
platform :ios, '10.0'
use_frameworks!
pod 'JSONValueRX'
Swift Package Manager (SPM)
dependencies: [
.package(url: "https://github.com/rexmas/JSONValue.git", from: "7.0.0")
]
构建 Xcode 项目
swift package generate-xcodeproj
子脚注
支持 .
索引
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))
等价
print(JSONValue.number(.int(1)) == JSONValue.number(.int(1))) // true
可哈希化
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
可编解码
从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解码到结构
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从字符串、数据编码/解码
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
编码/解码从/to .string
贡献
- 如果发现错误,请打开一个问题。
- Branch项目并提交pull request进行贡献。
- 确保Linux测试是最新的
swift test --generate-linuxmain
许可证
MIT许可协议(MIT)
版权所有(c)2015-至今 Rex
兹此授予任何人免费获取本软件及相关文档(以下简称“软件”)副本的权利,无论其目的如何,在以下条件下,可无限制地使用、复制、修改、合并、发布、分发、再许可和/或出售软件的副本,并许可获得软件的人进行上述操作:
上述版权声明和许可声明应包含在软件的所有副本或主要部分中。
软件按“原样”提供,不做任何形式的明示或暗示的保证,包括适销性、特定目的的适用性和非侵权性保证。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任负责,无论该责任源于合同、侵权或其他法律行为,无论该责任与软件或对软件的使用或其他行为有关。