Skip to content

Commit

Permalink
JAYSON -> JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
muukii committed Feb 25, 2017
1 parent 2ef83ea commit f12414f
Show file tree
Hide file tree
Showing 13 changed files with 258 additions and 255 deletions.
14 changes: 7 additions & 7 deletions JAYSON.playground/Pages/Build.xcplaygroundpage/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
import Foundation
@testable import JAYSON

var jayson = JAYSON()
jayson["id"] = 18737649
jayson["active"] = true
jayson["name"] = "muukii"
var json = JSON()
json["id"] = 18737649
json["active"] = true
json["name"] = "muukii"

jayson["images"] = JAYSON([
json["images"] = JSON([
"large" : "http://...foo",
"medium" : "http://...foo",
"small" : "http://...foo",
])

let data: Data = try jayson.data(options: .prettyPrinted)
let data: Data = try json.data(options: .prettyPrinted)

do {
let data = try jayson.data(options: .prettyPrinted)
let data = try json.data(options: .prettyPrinted)
print(String(data: data, encoding: .utf8)!)
} catch {
print(error)
Expand Down
20 changes: 10 additions & 10 deletions JAYSON.playground/Pages/Dribbble.xcplaygroundpage/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import Foundation

let dataPath = Bundle.main.path(forResource: "Sample", ofType: "json")
let data = Data(referencing: NSData(contentsOfFile: dataPath!)!)
let jayson = try! JAYSON(data: data)
let json = try! JSON(data: data)

let urlDecoder = Decoder<URL> { (jayson) throws -> URL in
URL(string: try jayson.getString())! // You can throw custom error.
let urlDecoder = Decoder<URL> { (json) throws -> URL in
URL(string: try json.getString())! // You can throw custom error.
}

struct Shot {
Expand All @@ -20,21 +20,21 @@ struct Shot {
let normalImageURLString: URL
let teaserImageURLString: URL

init(jayson: JAYSON) throws {
let imagesJayson = try jayson.next("images")
init(json: JSON) throws {
let imagesJayson = try json.next("images")

id = try jayson.next("id").getInt()
title = try jayson.next("title").getString()
width = try jayson.next("width").getInt()
height = try jayson.next("height").getInt()
id = try json.next("id").getInt()
title = try json.next("title").getString()
width = try json.next("width").getInt()
height = try json.next("height").getInt()
hidpiImageURLString = try? imagesJayson.next("hidpi").get(with: urlDecoder)
normalImageURLString = try imagesJayson.next("normal").get(with: urlDecoder)
teaserImageURLString = try imagesJayson.next("teaser").get(with: urlDecoder)
}
}

do {
let shots: [Shot] = try jayson.getArray().map(Shot.init(jayson: ))
let shots: [Shot] = try json.getArray().map(Shot.init(json: ))
print(shots)
} catch {
print(error)
Expand Down
18 changes: 9 additions & 9 deletions JAYSON.playground/Pages/Test.xcplaygroundpage/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,29 @@ import JAYSON

let dataPath = Bundle.main.path(forResource: "test", ofType: "json")
let data = Data(referencing: NSData(contentsOfFile: dataPath!)!)
let jayson = try! JAYSON(data)
let JSON = try! JSON(data)

let urlTransformer = Decoder<URL> { (jayson) throws -> URL in
URL(string: try jayson.getString())!
let urlTransformer = Decoder<URL> { (JSON) throws -> URL in
URL(string: try JSON.getString())!
}

do {

let fooJayson = try jayson
let fooJSON = try JSON
.next("tree1")
.next("tree2")
.next("tree3")
.next(0)
.next("index")

let value = try fooJayson.getString()
let path = fooJayson.currentPath()
let value = try fooJSON.getString()
let path = fooJSON.currentPath()

let url = try jayson
let url = try JSON
.next("url")
.get(with: urlTransformer)

let null = try jayson.next("null")
let null = try JSON.next("null")
null.isNull

do {
Expand All @@ -48,7 +48,7 @@ do {

do {

let fooJayson = try jayson
let fooJSON = try JSON
.next("tree1")
.next("tree2")
.back()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
//: [Previous](@previous)

import Foundation
@testable import JAYSON
@testable import JSON

let dataPath = Bundle.main.path(forResource: "unsplash", ofType: "json")
let inData = Data(referencing: NSData(contentsOfFile: dataPath!)!)
let jayson = try JAYSON(data: inData)
let JSON = try JSON(data: inData)

//print(jayson)
//debugPrint(jayson)
//print(JSON)
//debugPrint(JSON)

do {
let urlString: String = try jayson
let urlString: String = try JSON
.next("shots")
.next(0)
.next("user")
.next("profile_image")
.next("large")
.getString()

let shots = try jayson.next("shots").getArray().map { try $0.next("id").getString() }
let shots = try JSON.next("shots").getArray().map { try $0.next("id").getString() }
print(shots)
} catch {
print(error)
}

let urlString: String? = jayson["shots"][0]["user"]["profile_image"]["large"].string
let urlString: String? = JSON["shots"][0]["user"]["profile_image"]["large"].string

let shots = jayson["shots"].array?.map { $0["id"].string }
let shots = JSON["shots"].array?.map { $0["id"].string }

let large = jayson["shots"][0]["user"]["profile_image"]["large"]
let large = JSON["shots"][0]["user"]["profile_image"]["large"]
print(large.currentPath())


print(shots)


let urlDecoder = Decoder<URL> { (jayson) throws -> URL in
URL(string: try jayson.getString())!
let urlDecoder = Decoder<URL> { (JSON) throws -> URL in
URL(string: try JSON.getString())!
}

try! jayson
try! JSON
.next("shots")
.next(0)
.next("user")
Expand All @@ -49,18 +49,18 @@ try! jayson
.get(with: urlDecoder)


try! jayson
try! JSON
.next("shots")
.next(0)
.next("user")
.next("profile_image")
.next("large")
.get { (jayson) throws -> URL in
URL(string: try jayson.getString())!
.get { (JSON) throws -> URL in
URL(string: try JSON.getString())!
}

do {
let urlString: String = try jayson
let urlString: String = try JSON
.next("shots")
.next(0)
.next("user")
Expand Down
4 changes: 2 additions & 2 deletions Sources/Decoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@

public struct Decoder<T> {

let decode: (JAYSON) throws -> T
let decode: (JSON) throws -> T

public init(_ s: @escaping (JAYSON) throws -> T) {
public init(_ s: @escaping (JSON) throws -> T) {
self.decode = s
}
}
16 changes: 8 additions & 8 deletions Sources/JAYSON+OptionalProperty.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// JAYSON+OptionalProperty.swift
// JSON+OptionalProperty.swift
//
// Copyright (c) 2016 muukii
//
Expand All @@ -22,20 +22,20 @@

import Foundation

extension JAYSON {
extension JSON {

public var dictionary: [String : JAYSON]? {
return (source as? [String : Any])?.reduce([String : JAYSON]()) { dic, element in
public var dictionary: [String : JSON]? {
return (source as? [String : Any])?.reduce([String : JSON]()) { dic, element in
var dic = dic
dic[element.key] = JAYSON(source: element.value, breadcrumb: Breadcrumb(jayson: self, key: element.key))
dic[element.key] = JSON(source: element.value, breadcrumb: Breadcrumb(json: self, key: element.key))
return dic
}
}

public var array: [JAYSON]? {
public var array: [JSON]? {
return (source as? [Any])?
.enumerated()
.map { JAYSON(source: $0.element, breadcrumb: Breadcrumb(jayson: self, index: $0.offset)) }
.map { JSON(source: $0.element, breadcrumb: Breadcrumb(json: self, index: $0.offset)) }
}

public var string: String? {
Expand Down Expand Up @@ -106,7 +106,7 @@ extension JAYSON {
}
}

public func getOrNil<T>(_ s: (JAYSON) throws -> T) -> T? {
public func getOrNil<T>(_ s: (JSON) throws -> T) -> T? {
do {
return try s(self)
} catch {
Expand Down
4 changes: 2 additions & 2 deletions Sources/JAYSON+SourceType.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// JAYSON+SourceType.swift
// JSON+SourceType.swift
//
// Copyright (c) 2016 muukii
//
Expand All @@ -22,7 +22,7 @@

import Foundation

extension JAYSON {
extension JSON {

public enum SourceType {

Expand Down
4 changes: 2 additions & 2 deletions Sources/JAYSON+StateProperty.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// JAYSON+StateProperty.swift
// JSON+StateProperty.swift
//
// Copyright (c) 2016 muukii
//
Expand All @@ -22,7 +22,7 @@

import Foundation

extension JAYSON {
extension JSON {

public var isNull: Bool {
if case .null = sourceType {
Expand Down
32 changes: 16 additions & 16 deletions Sources/JAYSON+StrictGetter.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// JAYSON+StrictGetter.swift
// JSON+StrictGetter.swift
//
// Copyright (c) 2016 muukii
//
Expand All @@ -23,25 +23,25 @@
import Foundation

// Get Swift Value
extension JAYSON {
extension JSON {

public func getDictionary() throws -> [String : JAYSON] {
public func getDictionary() throws -> [String : JSON] {
guard let value = dictionary else {
throw JAYSONError.failedToGetDictionary(source: source, jayson: self)
throw JSONError.failedToGetDictionary(source: source, json: self)
}
return value
}

public func getArray() throws -> [JAYSON] {
public func getArray() throws -> [JSON] {
guard let value = array else {
throw JAYSONError.failedToGetArray(source: source, jayson: self)
throw JSONError.failedToGetArray(source: source, json: self)
}
return value
}

public func getNumber() throws -> NSNumber {
guard let value = number else {
throw JAYSONError.failedToGetNumber(source: source, jayson: self)
throw JSONError.failedToGetNumber(source: source, json: self)
}
return value
}
Expand Down Expand Up @@ -88,14 +88,14 @@ extension JAYSON {

public func getString() throws -> String {
guard let value = string else {
throw JAYSONError.failedToGetString(source: source, jayson: self)
throw JSONError.failedToGetString(source: source, json: self)
}
return value
}

public func getBool() throws -> Bool {
guard let value = source as? Bool else {
throw JAYSONError.failedToGetBool(source: source, jayson: self)
throw JSONError.failedToGetBool(source: source, json: self)
}
return value
}
Expand All @@ -108,23 +108,23 @@ extension JAYSON {
return try getNumber().doubleValue
}

public func get<T>(_ s: (JAYSON) throws -> T) rethrows -> T {
public func get<T>(_ s: (JSON) throws -> T) rethrows -> T {
do {
return try s(self)
} catch let jaysonError as JAYSONError {
throw jaysonError
} catch let jsonError as JSONError {
throw jsonError
} catch {
throw JAYSONError.decodeError(source: source, jayson: self, decodeError: error)
throw JSONError.decodeError(source: source, json: self, decodeError: error)
}
}

public func get<T>(with decoder: Decoder<T>) throws -> T {
do {
return try decoder.decode(self)
} catch let jaysonError as JAYSONError {
throw jaysonError
} catch let jsonError as JSONError {
throw jsonError
} catch {
throw JAYSONError.decodeError(source: source, jayson: self, decodeError: error)
throw JSONError.decodeError(source: source, json: self, decodeError: error)
}
}

Expand Down
Loading

0 comments on commit f12414f

Please sign in to comment.