0001 // http://stackoverflow.com/questions/35246542/serialize-stringany-to-json 0002 0003 protocol JSONSerializable{ 0004 func toJSON
JSON.swift:7 extension String : JSONSerializable {JSON.swift:13 extension Int : JSONSerializable {JSON.swift:19 extension Double : JSONSerializable {JSON.swift:25 extension Array : JSONSerializable {JSON.swift:29 if let json_element = element as? JSONSerializable, let string = json_element.toJSON() {JSON.swift:39 extension Dictionary : JSONSerializable {JSON.swift:43 if let json_element = v as? JSONSerializable, let string = json_element.toJSON() {() -> String? 0005 } 0006 0007 extension String : JSONSerializable { 0008 func toJSON() -> String? { 0009 return "\"\(self)\"" 0010 } 0011 } 0012 0013 extension Int : JSONSerializable { 0014 func toJSON() -> String? { 0015 return "\(self)" 0016 } 0017 } 0018 0019 extension Double : JSONSerializable { 0020 func toJSON() -> String? { 0021 return "\(self)" 0022 } 0023 } 0024 0025 extension Array : JSONSerializable { 0026 func toJSON() -> String? { 0027 var out : [String] = [] 0028 for element in self { 0029 if let json_element = element as? JSONSerializable, let string = json_element.toJSON() { 0030 out.append(string) 0031 } else { 0032 return nil 0033 } 0034 } 0035 return "[\(out.joinWithSeparator(", "))]" 0036 } 0037 } 0038 0039 extension Dictionary : JSONSerializable { 0040 func toJSON
JSON.swift:29 if let json_element = element as? JSONSerializable, let string = json_element.toJSON() {JSON.swift:43 if let json_element = v as? JSONSerializable, let string = json_element.toJSON() {() -> String? { 0041 var out : [String] = [] 0042 for (k, v) in self { 0043 if let json_element = v as? JSONSerializable, let string = json_element.toJSON() { 0044 out.append("\"\(k)\": \(string)") 0045 } else { 0046 return nil 0047 } 0048 } 0049 return "{\(out.joinWithSeparator(", "))}" 0050 } 0051 } 0052
View.swift:41 let json = context!.toJSON()