@@ -103,34 +103,35 @@ public struct Lookup: Swift.CustomStringConvertible, Swift.CustomDebugStringConv
103
103
}
104
104
}
105
105
106
- private init ( data: Data , options opt: JSONSerialization . ReadingOptions = [ ] ) throws {
107
- let object : Any = try JSONSerialization . jsonObject ( with: data, options: opt)
108
- self . init ( jsonObject: object)
106
+ private init ( data: Data , options opt: JSONSerialization . ReadingOptions = [ ] ) {
107
+ do {
108
+ let object : Any = try JSONSerialization . jsonObject ( with: data, options: opt)
109
+ self . init ( jsonObject: object)
110
+ } catch let error as NSError {
111
+ // Code=3840 "JSON text did not start with array or object and option to allow fragments not set. around line 1, column 0."
112
+ if error. code == 3840 , let str = String ( data: data, encoding: . utf8) { // try to initialize using a string
113
+ self . init ( jsonObject: str)
114
+ } else {
115
+ self . init ( jsonObject: NSNull ( ) )
116
+ }
117
+ }
109
118
}
110
119
111
- private init ( jsonString: String ) throws {
120
+ private init ( jsonString: String ) {
112
121
guard let stringData = jsonString. data ( using: . utf8) else {
113
122
self . init ( jsonObject: NSNull ( ) )
114
123
return
115
124
}
116
- try self . init ( data: stringData)
125
+ self . init ( data: stringData)
117
126
}
118
127
119
- /// Support JSON-Data, String of JSON, Array, Dictionary, Struct object and Class object
128
+ /// Support ` JSON-Data`, ` String-Data`, `String of JSON`, ` Array`, ` Dictionary`, ` Struct object` and ` Class object`
120
129
public init ( _ object: Any ) {
121
130
switch object {
122
131
case let str as String :
123
- do {
124
- try self . init ( jsonString: str)
125
- } catch {
126
- self . init ( jsonObject: str)
127
- }
132
+ self . init ( jsonString: str)
128
133
case let data as Data :
129
- do {
130
- try self . init ( data: data)
131
- } catch {
132
- self . init ( jsonObject: NSNull ( ) )
133
- }
134
+ self . init ( data: data)
134
135
default :
135
136
self . init ( jsonObject: object)
136
137
}
0 commit comments