0001 // JSONSerializer.swift 0002 // 0003 // The MIT License (MIT) 0004 // 0005 // Copyright (c) 2015 Zewo 0006 // 0007 // Permission is hereby granted, free of charge, to any person obtaining a copy 0008 // of this software and associated documentation files (the "Software"), to deal 0009 // in the Software without restriction, including without limitation the rights 0010 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 0011 // copies of the Software, and to permit persons to whom the Software is 0012 // furnished to do so, subject to the following conditions: 0013 // 0014 // The above copyright notice and this permission notice shall be included in all 0015 // copies or substantial portions of the Software. 0016 // 0017 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 0018 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 0019 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 0020 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 0021 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 0022 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 0023 // SOFTWARE. 0024 // 0025 // This file has been modified from its original project Swift-JsonSerializer 0026 0027 public protocol JSONSerializer{ 0028 func serialize
JSON.swift:230 public func serialize(serializer: JSONSerializer) -> String {JSONSerializer.swift:31 public class DefaultJSONSerializer: JSONSerializer {(JSONValue: JSON) -> String 0029 } 0030 0031 public class DefaultJSONSerializer
JSON.swift:231 return serializer.serialize(self): JSONSerializer { 0032 public init
JSON.swift:237 return serialize(DefaultJSONSerializer())JSONSerializer.swift:82 public final class PrettyJSONSerializer: DefaultJSONSerializer {() {} 0033 0034 public func serialize(JSONValue: JSON) -> String { 0035 switch JSONValue { 0036 case .NullValue: return "null" 0037 case .BooleanValue(let b): return b ? "true" : "false" 0038 case .NumberValue(let n): return serializeNumber(n) 0039 case .StringValue(let s): return escapeAsJSONString(s) 0040 case .ArrayValue(let a): return serializeArray(a) 0041 case .ObjectValue(let o): return serializeObject(o) 0042 } 0043 } 0044 0045 func serializeNumber
JSON.swift:237 return serialize(DefaultJSONSerializer())(n: Double) -> String { 0046 if n == Double(Int64(n)) { 0047 return Int64(n).description 0048 } else { 0049 return n.description 0050 } 0051 } 0052 0053 func serializeArray
JSONSerializer.swift:38 case .NumberValue(let n): return serializeNumber(n)(a: [JSON]) -> String { 0054 var s = "[" 0055 0056 for i in 0 ..< a.count { 0057 s += a[i].serialize(self) 0058 0059 if i != (a.count - 1) { 0060 s += "," 0061 } 0062 } 0063 0064 return s + "]" 0065 } 0066 0067 func serializeObject
JSONSerializer.swift:40 case .ArrayValue(let a): return serializeArray(a)(o: [String: JSON]) -> String { 0068 var s = "{" 0069 var i = 0 0070 0071 for entry in o { 0072 s += "\(escapeAsJSONString(entry.0)):\(entry.1.serialize(self))" 0073 if i++ != (o.count - 1) { 0074 s += "," 0075 } 0076 } 0077 0078 return s + "}" 0079 } 0080 } 0081 0082 public final class PrettyJSONSerializer
JSONSerializer.swift:41 case .ObjectValue(let o): return serializeObject(o): DefaultJSONSerializer { 0083 var indentLevel
JSON.swift:243 return serialize(PrettyJSONSerializer())= 0 0084 0085 override public func serializeArray(a: [JSON]) -> String { 0086 var s = "[" 0087 indentLevel++ 0088 0089 for i in 0 ..< a.count { 0090 s += "\n" 0091 s += indent() 0092 s += a[i].serialize(self) 0093 0094 if i != (a.count - 1) { 0095 s += "," 0096 } 0097 } 0098 0099 indentLevel-- 0100 return s + "\n" + indent() + "]" 0101 } 0102 0103 override public func serializeObject(o: [String: JSON]) -> String { 0104 var s = "{" 0105 indentLevel++ 0106 var i = 0 0107 0108 var keys = Array(o.keys) 0109 keys.sortInPlace() 0110 0111 for key in keys { 0112 s += "\n" 0113 s += indent() 0114 s += "\(escapeAsJSONString(key)): \(o[key]!.serialize(self))" 0115 0116 if i++ != (o.count - 1) { 0117 s += "," 0118 } 0119 } 0120 0121 indentLevel-- 0122 return s + "\n" + indent() + "}" 0123 } 0124 0125 func indent
JSONSerializer.swift:87 indentLevel++JSONSerializer.swift:99 indentLevel--JSONSerializer.swift:105 indentLevel++JSONSerializer.swift:121 indentLevel--JSONSerializer.swift:128 for _ in 0 ..< indentLevel {() -> String { 0126 var s = "" 0127 0128 for _ in 0 ..< indentLevel { 0129 s += " " 0130 } 0131 0132 return s 0133 } 0134 }
JSONSerializer.swift:91 s += indent()JSONSerializer.swift:100 return s + "\n" + indent() + "]"JSONSerializer.swift:113 s += indent()JSONSerializer.swift:122 return s + "\n" + indent() + "}"