-
Notifications
You must be signed in to change notification settings - Fork 237
Expand file tree
/
Copy pathJSONOutput.swift
More file actions
141 lines (123 loc) · 4.7 KB
/
Copy pathJSONOutput.swift
File metadata and controls
141 lines (123 loc) · 4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//
// JSONOutput.swift
// SourceKitten
//
// Created by Thomas Goyne on 9/17/15.
// Copyright © 2015 SourceKitten. All rights reserved.
//
import Foundation
/**
JSON Object to JSON String.
- parameter object: Object to convert to JSON.
- returns: JSON string representation of the input object.
*/
public func toJSON(_ object: Any) -> String {
if let array = object as? [Any], array.isEmpty {
return "[\n\n]"
}
do {
let options: JSONSerialization.WritingOptions
#if os(Linux)
options = [.prettyPrinted, .sortedKeys]
#else
if #available(macOS 10.13, *) {
options = [.prettyPrinted, .sortedKeys]
} else {
options = .prettyPrinted
}
#endif
let prettyJSONData = try JSONSerialization.data(withJSONObject: object, options: options)
if let jsonString = String(data: prettyJSONData, encoding: .utf8) {
return jsonString
}
} catch {}
return ""
}
/**
Convert [String: SourceKitRepresentable] to `NSDictionary`.
- parameter dictionary: [String: SourceKitRepresentable] to convert.
- returns: JSON-serializable value.
*/
public func toNSDictionary(_ dictionary: [String: SourceKitRepresentable]) -> NSDictionary {
var anyDictionary = [String: Any]()
for (key, object) in dictionary {
switch object {
case let object as [SourceKitRepresentable]:
anyDictionary[key] = object.map { toNSDictionary($0 as! [String: SourceKitRepresentable]) }
case let object as [[String: SourceKitRepresentable]]:
anyDictionary[key] = object.map { toNSDictionary($0) }
case let object as [String: SourceKitRepresentable]:
anyDictionary[key] = toNSDictionary(object)
case let object as String:
anyDictionary[key] = object
case let object as Int64:
anyDictionary[key] = NSNumber(value: object)
case let object as Bool:
anyDictionary[key] = NSNumber(value: object)
case let object as Any:
anyDictionary[key] = object
default:
fatalError("Should never happen because we've checked all SourceKitRepresentable types")
}
}
return anyDictionary.bridge()
}
#if !os(Linux)
public func declarationsToJSON(_ decl: [String: [SourceDeclaration]]) -> String {
let keyValueToDictionary: ((String, [SourceDeclaration])) -> [String: Any] = { [$0.0: toOutputDictionary($0.1)] }
let dictionaries: [[String: Any]] = decl.map(keyValueToDictionary).sorted { $0.keys.first! < $1.keys.first! }
return toJSON(dictionaries)
}
private func toOutputDictionary(_ decl: SourceDeclaration) -> [String: Any] {
var dict = [String: Any]()
func set(_ key: SwiftDocKey, _ value: Any?) {
if let value = value {
dict[key.rawValue] = value
}
}
func setA(_ key: SwiftDocKey, _ value: [Any]?) {
if let value = value, !value.isEmpty {
dict[key.rawValue] = value
}
}
set(.kind, decl.type.rawValue)
set(.filePath, decl.location.file)
set(.docFile, decl.location.file)
set(.docLine, Int(decl.location.line))
set(.docColumn, Int(decl.location.column))
set(.name, decl.name)
set(.usr, decl.usr)
set(.parsedDeclaration, decl.declaration)
set(.parsedAnnotatedDeclaration, decl.annotatedDeclaration)
set(.documentationComment, decl.commentBody)
set(.parsedScopeStart, Int(decl.extent.start.line))
set(.parsedScopeEnd, Int(decl.extent.end.line))
set(.swiftDeclaration, decl.swiftDeclaration)
set(.swiftName, decl.swiftName)
set(.alwaysDeprecated, decl.availability?.alwaysDeprecated)
set(.alwaysUnavailable, decl.availability?.alwaysUnavailable)
set(.deprecationMessage, decl.availability?.deprecationMessage)
set(.unavailableMessage, decl.availability?.unavailableMessage)
setA(.docResultDiscussion, decl.documentation?.returnDiscussion.map(toOutputDictionary))
setA(.docParameters, decl.documentation?.parameters.map(toOutputDictionary))
setA(.substructure, decl.children.map(toOutputDictionary))
if decl.commentBody != nil {
set(.fullXMLDocs, "")
}
return dict
}
private func toOutputDictionary(_ decl: [SourceDeclaration]) -> [String: Any] {
return ["key.substructure": decl.map(toOutputDictionary), "key.diagnostic_stage": ""]
}
private func toOutputDictionary(_ param: Parameter) -> [String: Any] {
return ["name": param.name, "discussion": param.discussion.map(toOutputDictionary)]
}
private func toOutputDictionary(_ text: Text) -> [String: Any] {
switch text {
case .para(let str, let kind):
return ["kind": kind ?? "", "Para": str]
case .verbatim(let str):
return ["kind": "", "Verbatim": str]
}
}
#endif