mirror of
https://github.com/zhigang1992/JSON.git
synced 2026-01-12 22:39:47 +08:00
Add double
This commit is contained in:
@@ -85,6 +85,15 @@ public struct JSON {
|
||||
}
|
||||
}
|
||||
|
||||
public var double: Double? {
|
||||
get {
|
||||
return self.value as? Double
|
||||
}
|
||||
set {
|
||||
self.value = newValue.flatMap({ $0 as NSObject})
|
||||
}
|
||||
}
|
||||
|
||||
public var string: String? {
|
||||
get {
|
||||
return self.value as? String
|
||||
@@ -120,3 +129,42 @@ public struct JSON {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extension JSON: ExpressibleByDictionaryLiteral {
|
||||
|
||||
public typealias Key = String
|
||||
public typealias Value = Any
|
||||
public init(dictionaryLiteral elements: (JSON.Key, JSON.Value)...) {
|
||||
var dictionary:[Key: Value] = [:]
|
||||
elements.forEach({ dictionary[$0.0] = $0.1 })
|
||||
self.init(value: dictionary as NSDictionary)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extension JSON: ExpressibleByArrayLiteral {
|
||||
|
||||
public typealias Element = Any
|
||||
public init(arrayLiteral elements: JSON.Element...) {
|
||||
self.init(value: elements as NSArray)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extension JSON {
|
||||
|
||||
public func encode() -> Data? {
|
||||
return self.value.flatMap({
|
||||
try? JSONSerialization.data(withJSONObject: $0, options: [])
|
||||
})
|
||||
}
|
||||
|
||||
public var prettyJson: String? {
|
||||
return self.value.flatMap({
|
||||
try? JSONSerialization.data(withJSONObject: $0, options: [.prettyPrinted])
|
||||
}).flatMap({
|
||||
String(data: $0, encoding: .utf8)
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user