Skip to content

Commit

Permalink
output prittyPrinted optional default false
Browse files Browse the repository at this point in the history
  • Loading branch information
evermeer committed Dec 6, 2016
1 parent 7a399bc commit 762b73a
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 25 deletions.
2 changes: 1 addition & 1 deletion EVReflection.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'")
}
}
2 changes: 1 addition & 1 deletion EVReflection/EVReflectionTests/EVReflectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions EVReflection/pod/EVArrayExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]"
}

/**
Expand Down
4 changes: 2 additions & 2 deletions EVReflection/pod/EVObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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)
}
}

Expand Down
8 changes: 4 additions & 4 deletions EVReflection/pod/EVReflectable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 "{}"
Expand All @@ -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()
Expand Down
21 changes: 12 additions & 9 deletions EVReflection/pod/EVReflection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) ?? ""
}

Expand All @@ -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()
}


Expand All @@ -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))
}

/**
Expand All @@ -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))"
Expand Down
4 changes: 2 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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.
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

0 comments on commit 762b73a

Please sign in to comment.