From 762b73aa1cfcc4eb1d4b844006904667cb772026 Mon Sep 17 00:00:00 2001 From: Edwin Vermeer Date: Tue, 6 Dec 2016 11:39:19 +0100 Subject: [PATCH] output prittyPrinted optional default false --- EVReflection.podspec | 2 +- .../EVReflectionConversionOptionsTest.swift | 4 ++-- .../EVReflectionPropertyMappingTest.swift | 4 ++-- .../EVReflectionTests/EVReflectionTests.swift | 2 +- EVReflection/pod/EVArrayExtension.swift | 4 ++-- EVReflection/pod/EVObject.swift | 4 ++-- EVReflection/pod/EVReflectable.swift | 8 +++---- EVReflection/pod/EVReflection.swift | 21 +++++++++++-------- LICENSE | 4 ++-- 9 files changed, 28 insertions(+), 25 deletions(-) diff --git a/EVReflection.podspec b/EVReflection.podspec index 0d59046c..a5156b95 100644 --- a/EVReflection.podspec +++ b/EVReflection.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| # s.name = "EVReflection" -s.version = "3.4.2" +s.version = "3.5.0" s.summary = "iOS: Swift helper library with reflection functions" s.description = "Swift helper library with reflection functions including support for NSCoding, Printable, Hashable, Equatable and JSON" s.homepage = "https://github.com/evermeer/EVReflection" diff --git a/EVReflection/EVReflectionTests/EVReflectionConversionOptionsTest.swift b/EVReflection/EVReflectionTests/EVReflectionConversionOptionsTest.swift index 3cd6a302..f5107dca 100644 --- a/EVReflection/EVReflectionTests/EVReflectionConversionOptionsTest.swift +++ b/EVReflection/EVReflectionTests/EVReflectionConversionOptionsTest.swift @@ -46,10 +46,10 @@ class EVReflectionConversionOptionsTest: XCTestCase { a.value7 = [String]() let json = a.toJsonString() print("json = \(json)") - XCTAssertEqual(json, "{\n \"value4\" : 4,\n \"value1\" : \"test1\",\n \"value6\" : [\n \"arrayElement\"\n ]\n}", "Incorrect json") + XCTAssertEqual(json, "{\"value4\":4,\"value1\":\"test1\",\"value6\":[\"arrayElement\"]}", "Incorrect json") let json2 = a.toJsonString(.None) print("json = \(json2)") - XCTAssertEqual(json2, "{\n \"value1\" : \"test1\",\n \"value5\" : null,\n \"value2\" : \"\",\n \"value6\" : [\n \"arrayElement\"\n ],\n \"value3\" : null,\n \"value7\" : [\n\n ],\n \"value4\" : 4,\n \"value8\" : null\n}", "Incorrect json") + XCTAssertEqual(json2, "{\"value1\":\"test1\",\"value5\":null,\"value2\":\"\",\"value6\":[\"arrayElement\"],\"value3\":null,\"value7\":[],\"value4\":4,\"value8\":null}", "Incorrect json") let b = TestObjectSkipValues(json: json2) let json3 = b.toJsonString() XCTAssertEqual(json, json3, "Json should be the same") diff --git a/EVReflection/EVReflectionTests/EVReflectionPropertyMappingTest.swift b/EVReflection/EVReflectionTests/EVReflectionPropertyMappingTest.swift index e24eac9c..0b4b8377 100644 --- a/EVReflection/EVReflectionTests/EVReflectionPropertyMappingTest.swift +++ b/EVReflection/EVReflectionTests/EVReflectionPropertyMappingTest.swift @@ -39,9 +39,9 @@ class EVReflectionPropertyMappingTests: XCTestCase { let t = TestObject6(json: json) XCTAssertTrue(t.isGreat, "This mapping should make true from 'Sure'") let s = t.toJsonString(.PropertyConverter) // So no PropertyMapping, SkipPropertyValue or KeyCleanup - XCTAssertEqual(s, "{\n \"isGreat\" : \"Sure\"\n}", "The json should contain 'Sure'") + XCTAssertEqual(s, "{\"isGreat\":\"Sure\"}", "The json should contain 'Sure'") t.isGreat = false let s2 = t.toJsonString([.DefaultSerialize, .KeyCleanup]) - XCTAssertEqual(s2, "{\n \"is_great\" : \"Nah\"\n}", "The json should contain 'Nah'") + XCTAssertEqual(s2, "{\"is_great\":\"Nah\"}", "The json should contain 'Nah'") } } diff --git a/EVReflection/EVReflectionTests/EVReflectionTests.swift b/EVReflection/EVReflectionTests/EVReflectionTests.swift index fa47f80e..5899e528 100644 --- a/EVReflection/EVReflectionTests/EVReflectionTests.swift +++ b/EVReflection/EVReflectionTests/EVReflectionTests.swift @@ -343,7 +343,7 @@ class EVReflectionTests: XCTestCase { a.openId = "value" let json = a.toJsonString() print(json) - XCTAssertEqual(json, "{\n \"openId\" : \"value\"\n}", "Incorrect serialization to json") + XCTAssertEqual(json, "{\"openId\":\"value\"}", "Incorrect serialization to json") } func testLazy() { diff --git a/EVReflection/pod/EVArrayExtension.swift b/EVReflection/pod/EVArrayExtension.swift index 9ce9d31a..eedcd78d 100644 --- a/EVReflection/pod/EVArrayExtension.swift +++ b/EVReflection/pod/EVArrayExtension.swift @@ -132,8 +132,8 @@ public extension Array where Element: EVReflectable { - returns: The json string */ - public func toJsonString(_ conversionOptions: ConversionOptions = .DefaultSerialize) -> String { - return "[\n" + self.map({($0).toJsonString(conversionOptions)}).joined(separator: ", \n") + "\n]" + public func toJsonString(_ conversionOptions: ConversionOptions = .DefaultSerialize, prettyPrinted: Bool = false) -> String { + return "[\n" + self.map({($0).toJsonString(conversionOptions, prettyPrinted: prettyPrinted)}).joined(separator: ", \n") + "\n]" } /** diff --git a/EVReflection/pod/EVObject.swift b/EVReflection/pod/EVObject.swift index d3ee77dd..ba5a0c3d 100644 --- a/EVReflection/pod/EVObject.swift +++ b/EVReflection/pod/EVObject.swift @@ -62,7 +62,7 @@ open class EVObject: NSObject, NSCoding, EVReflectable { */ open override var description: String { get { - return EVReflection.description(self) + return EVReflection.description(self, prettyPrinted: true) } } @@ -73,7 +73,7 @@ open class EVObject: NSObject, NSCoding, EVReflectable { */ open override var debugDescription: String { get { - return EVReflection.description(self) + return EVReflection.description(self, prettyPrinted: true) } } diff --git a/EVReflection/pod/EVReflectable.swift b/EVReflection/pod/EVReflectable.swift index b0dd9230..9a7ed3d8 100644 --- a/EVReflection/pod/EVReflectable.swift +++ b/EVReflection/pod/EVReflectable.swift @@ -340,9 +340,9 @@ extension EVReflectable { - returns: The json string */ - public func toJsonString(_ conversionOptions: ConversionOptions = .DefaultSerialize) -> String { + public func toJsonString(_ conversionOptions: ConversionOptions = .DefaultSerialize, prettyPrinted: Bool = false) -> String { if let obj = self as? NSObject { - return EVReflection.toJsonString(obj, conversionOptions: conversionOptions) + return EVReflection.toJsonString(obj, conversionOptions: conversionOptions, prettyPrinted: prettyPrinted) } print("ERROR: You should only extend object with EVReflectable that are derived from NSObject!") return "{}" @@ -355,9 +355,9 @@ extension EVReflectable { - returns: The json Data */ - public func toJsonData(_ conversionOptions: ConversionOptions = .DefaultSerialize) -> Data { + public func toJsonData(_ conversionOptions: ConversionOptions = .DefaultSerialize, prettyPrinted: Bool = false) -> Data { if let obj = self as? NSObject { - return EVReflection.toJsonData(obj, conversionOptions: conversionOptions) + return EVReflection.toJsonData(obj, conversionOptions: conversionOptions, prettyPrinted: prettyPrinted) } print("ERROR: You should only extend object with EVReflectable that are derived from NSObject!") return Data() diff --git a/EVReflection/pod/EVReflection.swift b/EVReflection/pod/EVReflection.swift index 3d7eed33..3e70e275 100644 --- a/EVReflection/pod/EVReflection.swift +++ b/EVReflection/pod/EVReflection.swift @@ -280,8 +280,8 @@ final public class EVReflection { - returns: The string representation of the object */ - public class func toJsonString(_ theObject: NSObject, conversionOptions: ConversionOptions = .DefaultSerialize) -> String { - let data = toJsonData(theObject, conversionOptions: conversionOptions) + public class func toJsonString(_ theObject: NSObject, conversionOptions: ConversionOptions = .DefaultSerialize, prettyPrinted: Bool = false) -> String { + let data = toJsonData(theObject, conversionOptions: conversionOptions, prettyPrinted: prettyPrinted) return String(data: data, encoding: .utf8) ?? "" } @@ -293,13 +293,16 @@ final public class EVReflection { - returns: The Data representation of the object */ - public class func toJsonData(_ theObject: NSObject, conversionOptions: ConversionOptions = .DefaultSerialize) -> Data { + public class func toJsonData(_ theObject: NSObject, conversionOptions: ConversionOptions = .DefaultSerialize, prettyPrinted: Bool = false) -> Data { var (dict, _) = EVReflection.toDictionary(theObject, conversionOptions: conversionOptions) dict = convertDictionaryForJsonSerialization(dict, theObject: theObject) do { - return try JSONSerialization.data(withJSONObject: dict, options: .prettyPrinted) + if prettyPrinted { + return try JSONSerialization.data(withJSONObject: dict, options: .prettyPrinted) + } + return try JSONSerialization.data(withJSONObject: dict, options: []) } catch { } - return Data() + return Data() } @@ -311,8 +314,8 @@ final public class EVReflection { - parameter theObject: The object that will be loged */ - public class func logObject(_ theObject: EVReflectable) { - NSLog(description(theObject)) + public class func logObject(_ theObject: EVReflectable, prettyPrinted: Bool = true) { + NSLog(description(theObject, prettyPrinted: prettyPrinted)) } /** @@ -323,9 +326,9 @@ final public class EVReflection { - returns: The string representation of the object */ - public class func description(_ theObject: EVReflectable, conversionOptions: ConversionOptions = .DefaultSerialize) -> String { + public class func description(_ theObject: EVReflectable, conversionOptions: ConversionOptions = .DefaultSerialize, prettyPrinted: Bool = true) -> String { if let obj = theObject as? NSObject { - return "\(swiftStringFromClass(obj)) = \(theObject.toJsonString())" + return "\(swiftStringFromClass(obj)) = \(theObject.toJsonString(prettyPrinted: prettyPrinted))" } print("ERROR: \(String(reflecting: theObject)) should have NSObject as it's base type.") return "\(String(reflecting: theObject))" diff --git a/LICENSE b/LICENSE index f2b0660c..98fe3a9c 100644 --- a/LICENSE +++ b/LICENSE @@ -11,7 +11,7 @@ notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. -* Neither the name of Mirabeau B.V. nor the +* Neither the name of EVICT B.V. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. @@ -24,4 +24,4 @@ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.