Skip to content

Commit 289fa31

Browse files
author
Sven
committed
Initial Commit
0 parents  commit 289fa31

File tree

12 files changed

+741
-0
lines changed

12 files changed

+741
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.DS_Store
2+
/.build
3+
/Packages
4+
/*.xcodeproj
5+
xcuserdata/

.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// swift-tools-version:5.3
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "OrderJSON",
8+
products: [
9+
// Products define the executables and libraries a package produces, and make them visible to other packages.
10+
.library(
11+
name: "OrderJSON",
12+
targets: ["OrderJSON"]),
13+
],
14+
dependencies: [
15+
// Dependencies declare other packages that this package depends on.
16+
// .package(url: /* package url */, from: "1.0.0"),
17+
],
18+
targets: [
19+
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
20+
// Targets can depend on other targets in this package, and on products in packages this package depends on.
21+
.target(
22+
name: "OrderJSON",
23+
dependencies: []),
24+
.testTarget(
25+
name: "OrderJSONTests",
26+
dependencies: ["OrderJSON"]),
27+
]
28+
)

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# OrderJSON
2+
3+
## 简介
4+
OrderJSON 是手动解析 json 字符串, 使得字典 key 与 json key顺序一致。
5+
OrderJSON 并不是一个可以有顺序的字典。
6+
7+
该项目基于[josn--swiftDo](https://github.com/swiftdo/json)基础上,修复空数组和空对象 添加扩展方法 `any()`而来,具体原理参考源项目
8+
参考文章
9+
- [Swift 码了个 JSON 解析器(一)](https://oldbird.run/swift/fp/t3-json1.html)
10+
- [Swift 码了个 JSON 解析器(二)](https://oldbird.run/swift/fp/t3-json2.html)
11+
- [Swift 码了个 JSON 解析器(三)](https://oldbird.run/swift/fp/t3-json3.html)
12+
13+
14+
目前仅支持 Swift Package Manager 集成

Sources/OrderJSON/JSON+Exts.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// JSON+Exts.swift
3+
// json
4+
//
5+
// Created by Sven on 2021/4/20.
6+
//
7+
8+
import Foundation
9+
10+
extension JSON {
11+
// 返回 Any 基础类型类型
12+
func any() -> Any {
13+
switch self {
14+
case .string(let value):
15+
return value
16+
case .object(let value):
17+
var dic: [String: Any] = [:]
18+
value.forEach { (temp) in
19+
let key = temp.key
20+
let value = temp.value.any()
21+
dic[key] = value
22+
}
23+
return dic
24+
case .array(let value):
25+
return value.map{ $0.any() }
26+
case .double(let value):
27+
return value
28+
case .int(let value):
29+
return value
30+
case .bool(let value):
31+
return value
32+
case .null:
33+
return NSNull()
34+
}
35+
}
36+
}

Sources/OrderJSON/JSON.swift

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//
2+
// JSON.swift
3+
// json
4+
//
5+
// Created by mac on 2021/4/9.
6+
// git https://github.com/swiftdo/json
7+
// 根据 json 构造特性手动解析, 仅限于转义压缩json
8+
9+
import Foundation
10+
11+
//data JValue = JString String
12+
// | JNumber Double
13+
// | JBool Bool
14+
// | JNull
15+
// | JObject [(String, JValue)]
16+
// | JArray [JValue]
17+
// | JInteger Integer
18+
// deriving (Eq, Ord, Read)
19+
20+
21+
/// 类型和数据结构定义
22+
public enum JSON {
23+
case object([String: JSON])
24+
case array([JSON])
25+
case string(String)
26+
case double(Double)
27+
case int(Int)
28+
case bool(Bool)
29+
case null
30+
31+
public init(_ value: Int) {
32+
self = .int(value)
33+
}
34+
35+
public init(_ value: Double) {
36+
self = .double(value)
37+
}
38+
39+
public init(_ value: [JSON]) {
40+
self = .array(value)
41+
}
42+
43+
public init(_ value: [String: JSON]) {
44+
self = .object(value)
45+
}
46+
47+
public init(_ value: String) {
48+
self = .string(value)
49+
}
50+
51+
public init(_ value: Bool) {
52+
self = .bool(value)
53+
}
54+
}
55+
56+
extension JSON: CustomStringConvertible {
57+
public var description: String {
58+
switch self {
59+
case let .string(v): return "String(\(v))"
60+
case let .double(v): return "Double(\(v))"
61+
case let .int(v): return "Int(\(v)"
62+
case let .bool(v): return "Bool(\(v))"
63+
case let .array(a): return "Array(\(a.description))"
64+
case let .object(o): return "Object(\(o.description))"
65+
case .null: return "Null"
66+
}
67+
}
68+
}
69+
70+
extension JSON: Equatable {
71+
public static func == (lhs: JSON, rhs: JSON) -> Bool {
72+
switch (lhs, rhs) {
73+
case let (.string(l), .string(r)): return l == r
74+
case let (.double(l), .double(r)): return l == r
75+
case let (.int(l), .int(r)): return l == r
76+
case let (.bool(l), .bool(r)): return l == r
77+
case let (.array(l), .array(r)): return l == r
78+
case let (.object(l), .object(r)): return l == r
79+
case (.null, .null): return true
80+
default: return false
81+
}
82+
}
83+
}

Sources/OrderJSON/OrderJSON.swift

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//
2+
// OrderJSON.swift
3+
//
4+
//
5+
// Created by Sven on 2021/4/20.
6+
//
7+
8+
import Foundation
9+
/// 有序josn
10+
///
11+
/// - Warning: 此处有序并不代表实现了有序字典,只表示字典解析和 json 一致。
12+
struct OrderJSON {
13+
private var input: String
14+
private var json: JSON?
15+
init(_ input: String) {
16+
self.input = input
17+
json = try? JsonParser.parse(text: input)
18+
}
19+
/// 是否为字典
20+
func isDictionary() -> Bool {
21+
if let aJson = json, case .object(_) = aJson {
22+
return true
23+
} else {
24+
return false
25+
}
26+
}
27+
/// 是否为数组
28+
func isArray() -> Bool {
29+
if let aJson = json, case .array(_) = aJson {
30+
return true
31+
} else {
32+
return false
33+
}
34+
}
35+
/// 返回字典 只有是字典
36+
func dictionary() -> [String: Any]? {
37+
guard let result = json, case .object(_) = result else {
38+
return nil
39+
}
40+
41+
let anyValue = result.any()
42+
if let dic = (anyValue as? [String: Any]) {
43+
return dic
44+
}
45+
return nil
46+
}
47+
}

0 commit comments

Comments
 (0)