Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
rokgregoric committed Apr 25, 2018
0 parents commit 04cbb10
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Source/Info-iOS.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>${EXECUTABLE_NAME}</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>${PRODUCT_NAME}</string>
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>${CURRENT_PROJECT_VERSION}</string>
<key>NSPrincipalClass</key>
<string></string>
</dict>
</plist>
128 changes: 128 additions & 0 deletions Source/JSON.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
//
// JSON.swift
//
// Created by Rok Gregoric on 12/04/2018.
// Copyright © 2018 Rok Gregoric. All rights reserved.
//

import Foundation

public class JSON {
var obj: Any?

fileprivate init(obj: Any?) {
self.obj = obj
}

public convenience init(_ obj: Any?) {
if let data = obj as? Data {
self.init(data: data)
} else {
self.init(obj: obj)
}
}

public convenience init(data: Data, options opt: JSONSerialization.ReadingOptions = []) {
self.init(obj: try? JSONSerialization.jsonObject(with: data, options: opt))
}

public convenience init(parseJSON string: String) {
self.init(string.data(using: .utf8))
}

public subscript(_ key: String) -> JSON {
return JSON((obj as? NSDictionary)?[key])
}

public var object: Any? { return obj }

public var arrayObject: [Any]? { return obj as? [Any] }
public var array: [JSON]? { return arrayObject?.map { JSON($0) } }
public var arrayValue: [JSON] { return array ?? [] }

public var dictionaryObject: [String: Any]? { return obj as? [String: Any] }
public var dictionary: [String: JSON]? {
guard let dic = dictionaryObject else { return nil }
var d = [String: JSON](minimumCapacity: dic.count)
for (key, value) in dic {
d[key] = JSON(value)
}
return d
}
public var dictionaryValue: [String: JSON] { return dictionary ?? [:] }

public var string: String? { return obj as? String }
public var stringValue: String { return string ?? "" }

public var number: NSNumber? { return obj as? NSNumber }
public var numberValue: NSNumber { return number ?? NumberFormatter().number(from: stringValue) ?? 0 }

public var bool: Bool? { return obj as? Bool }
public var boolValue: Bool { return bool ?? Bool(stringValue) ?? false }

public var double: Double? { return obj as? Double }
public var doubleValue: Double { return double ?? Double(stringValue) ?? 0 }

public var float: Float? { return obj as? Float }
public var floatValue: Float { return float ?? Float(stringValue) ?? 0 }

public var int: Int? { return obj as? Int }
public var intValue: Int { return int ?? Int(stringValue) ?? 0 }

public var uInt: UInt? { return obj as? UInt }
public var uIntValue: UInt { return uInt ?? UInt(stringValue) ?? 0 }

public var int8: Int8? { return obj as? Int8 }
public var int8Value: Int8 { return int8 ?? Int8(stringValue) ?? 0 }

public var uInt8: UInt8? { return obj as? UInt8 }
public var uInt8Value: UInt8 { return uInt8 ?? UInt8(stringValue) ?? 0 }

public var int16: Int16? { return obj as? Int16 }
public var int16Value: Int16 { return int16 ?? Int16(stringValue) ?? 0 }

public var uInt16: UInt16? { return obj as? UInt16 }
public var uInt16Value: UInt16 { return uInt16 ?? UInt16(stringValue) ?? 0 }

public var int32: Int32? { return obj as? Int32 }
public var int32Value: Int32 { return int32 ?? Int32(stringValue) ?? 0 }

public var uInt32: UInt32? { return obj as? UInt32 }
public var uInt32Value: UInt32 { return uInt32 ?? UInt32(stringValue) ?? 0 }

public var int64: Int64? { return obj as? Int64 }
public var int64Value: Int64 { return int64 ?? Int64(stringValue) ?? 0 }

public var uInt64: UInt64? { return obj as? UInt64 }
public var uInt64Value: UInt64 { return uInt64 ?? UInt64(stringValue) ?? 0 }

public var url: URL? {
guard let str = string else { return nil }

// Check for existing percent escapes first to prevent double-escaping of % character
if str.range(of: "%[0-9A-Fa-f]{2}", options: .regularExpression, range: nil, locale: nil) != nil {
return URL(string: str)
}
return str.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed).flatMap { URL(string: $0) }
}

public func rawString(_ encoding: String.Encoding = .utf8, options opt: JSONSerialization.WritingOptions = .prettyPrinted) -> String? {
guard let obj = obj else { return nil }
guard JSONSerialization.isValidJSONObject(obj) else { return nil }
let data = try? JSONSerialization.data(withJSONObject: obj, options: opt)
return data.flatMap { String(data: $0, encoding: .utf8) }
}
}

// MARK: - Printable, DebugPrintable

extension JSON: Swift.CustomStringConvertible, Swift.CustomDebugStringConvertible {
public var description: String {
return rawString(options: .prettyPrinted) ?? "unknown"
}

public var debugDescription: String {
return description
}
}

30 changes: 30 additions & 0 deletions Source/SwifterJSON.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// SwifterJSON.h
//
// Copyright (c) 2018 Rok Gregoric
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

@import Foundation;

//! Project version number for SwifterJSON.
FOUNDATION_EXPORT double SwifterJSONVersionNumber;

//! Project version string for SwifterJSON.
FOUNDATION_EXPORT const unsigned char SwifterJSONVersionString[];

16 changes: 16 additions & 0 deletions SwifterJSON.podspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Pod::Spec.new do |s|
s.name = "SwifterJSON"
s.version = "0.9.0"
s.summary = "SwifterJSON makes it easy to deal with JSON data in Swift"
s.homepage = "https://github.com/rokgregoric/SwifterJSON"
s.license = { :type => "MIT" }
s.authors = { "rokgregoric" => "rok.gregoric@gmail.com" }

s.requires_arc = true
s.osx.deployment_target = "10.9"
s.ios.deployment_target = "8.0"
s.watchos.deployment_target = "2.0"
s.tvos.deployment_target = "9.0"
s.source = { :git => "https://github.com/rokgregoric/SwifterJSON.git", :tag => s.version }
s.source_files = "Source/*.swift"
end

0 comments on commit 04cbb10

Please sign in to comment.