Skip to content

Commit

Permalink
Kitura/Kitura#361 upgraded to compile with 4/12 binaries
Browse files Browse the repository at this point in the history
  • Loading branch information
dfirsht committed Apr 13, 2016
1 parent e57295e commit 6aa99bb
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 66 deletions.
20 changes: 10 additions & 10 deletions Source/LclJSONSerialization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public class LclJSONSerialization {
private static let NULL = NSString(string: "null")


public class func isValidJSONObject(obj: Any) -> Bool {
public class func isValidJSONObject(_ obj: Any) -> Bool {
// TODO: - revisit this once bridging story gets fully figured out
func isValidJSONObjectInternal(obj: Any) -> Bool {
func isValidJSONObjectInternal(_ obj: Any) -> Bool {
// object is Swift.String or NSNull
if obj is String || obj is Int || obj is Bool || obj is NSNull {
return true
Expand Down Expand Up @@ -97,29 +97,29 @@ public class LclJSONSerialization {
return isValidJSONObjectInternal(obj)
}

public class func dataWithJSONObject(obj: Any, options: NSJSONWritingOptions) throws -> NSData
public class func dataWithJSONObject(_ obj: Any, options: NSJSONWritingOptions) throws -> NSData
{
let result = NSMutableData()

try writeJson(obj, options: options) { (str: NSString?) in
if let str = str {
result.appendBytes(str.cStringUsingEncoding(NSUTF8StringEncoding), length: str.lengthOfBytesUsingEncoding(NSUTF8StringEncoding))
result.appendBytes(str.cStringUsingEncoding(NSUTF8StringEncoding)!, length: str.lengthOfBytesUsingEncoding(NSUTF8StringEncoding))
}
}

return result
}

/* Helper function to enable writing to NSData as well as NSStream */
private static func writeJson(obj: Any, options opt: NSJSONWritingOptions, writer: (NSString?) -> Void) throws {
private static func writeJson(_ obj: Any, options opt: NSJSONWritingOptions, writer: (NSString?) -> Void) throws {
let prettyPrint = opt.rawValue & NSJSONWritingOptions.PrettyPrinted.rawValue != 0
let padding: NSString? = prettyPrint ? NSString(string: "") : nil

try writeJsonValue(obj, padding: padding, writer: writer)
}

/* Write out a JSON value (simple value, object, or array) */
private static func writeJsonValue(obj: Any, padding: NSString?, writer: (NSString?) -> Void) throws {
private static func writeJsonValue(_ obj: Any, padding: NSString?, writer: (NSString?) -> Void) throws {
if obj is String {
writer("\"")
writer((obj as! String).bridge())
Expand Down Expand Up @@ -153,7 +153,7 @@ public class LclJSONSerialization {
}

/* Write out a dictionary as a JSON object */
private static func writeJsonObject(pairs: Array<Any>, padding: NSString?, writer: (NSString?) -> Void) throws {
private static func writeJsonObject(_ pairs: Array<Any>, padding: NSString?, writer: (NSString?) -> Void) throws {
let (nestedPadding, startOfLine, endOfLine) = setupPadding(padding)
let nameValueSeparator = NSString(string: padding != nil ? ": " : ":")

Expand Down Expand Up @@ -186,7 +186,7 @@ public class LclJSONSerialization {
}

/* Write out an array as a JSON Array */
private static func writeJsonArray(obj: Array<Any>, padding: NSString?, writer: (NSString?) -> Void) throws {
private static func writeJsonArray(_ obj: Array<Any>, padding: NSString?, writer: (NSString?) -> Void) throws {
let (nestedPadding, startOfLine, endOfLine) = setupPadding(padding)
writer("[")

Expand All @@ -209,7 +209,7 @@ public class LclJSONSerialization {
Note: if padding is nil, then all padding, newlines etc., are suppressed
*/
private static func setupPadding(padding: NSString?) -> (NSString?, NSString?, NSString?) {
private static func setupPadding(_ padding: NSString?) -> (NSString?, NSString?, NSString?) {
let nestedPadding: NSString?
let startOfLine: NSString?
let endOfLine: NSString?
Expand All @@ -229,7 +229,7 @@ public class LclJSONSerialization {
return (nestedPadding, startOfLine, endOfLine)
}

private static func createWriteError(reason: String) -> NSError {
private static func createWriteError(_ reason: String) -> NSError {
let userInfo: [String: Any] = [NSLocalizedDescriptionKey: JSON_WRITE_ERROR,
NSLocalizedFailureReasonErrorKey: reason]
return NSError(domain: LclErrorDomain, code: 1, userInfo: userInfo)
Expand Down
85 changes: 85 additions & 0 deletions Source/NSExtension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//
// NSExtension.swift
// SwiftyJSON
//
// Created by Daniel Firsht on 4/13/16.
//
//

import Foundation

#if os(Linux)
extension NSNumber {
convenience init(value: Bool) {
self.init(bool: value)
}

convenience init(value: Double) {
self.init(double: value)
}

convenience init(value: Float) {
self.init(float: value)
}

convenience init(value: Int) {
self.init(integer: value)
}

convenience init(value: UInt) {
self.init(unsignedLong: value)
}

convenience init(value: Int8) {
self.init(char: value)
}

convenience init(value: UInt8) {
self.init(unsignedChar: value)
}

convenience init(value: Int16) {
self.init(short: value)
}

convenience init(value: UInt16) {
self.init(unsignedShort: value)
}

convenience init(value: Int32) {
self.init(int: value)
}

convenience init(value: UInt32) {
self.init(unsignedInt: value)
}

convenience init(value: Int64) {
self.init(longLong: value)
}

convenience init(value: UInt64) {
self.init(unsignedLongLong: value)
}

var intValue:Int {return self.longValue}

var uintValue:UInt {return self.unsignedLongValue}

var int8Value:Int8 {return self.charValue}

var uint8Value:UInt8 {return self.unsignedCharValue}

var int16Value:Int16 {return self.shortValue}

var uint16Value:UInt16 {return self.unsignedShortValue}

var int32Value:Int32 {return self.intValue}

var uint32Value:UInt32 {return self.unsignedIntValue}

var int64Value:Int64 {return self.longLongValue}

var uint64Value:UInt64 {return self.unsignedLongLongValue}
}
#endif
Loading

0 comments on commit 6aa99bb

Please sign in to comment.