Skip to content

Instantly share code, notes, and snippets.

@nuieskater
Last active May 13, 2018 14:14
Show Gist options
  • Save nuieskater/aeb5b91679184f41b94927dfb03eb747 to your computer and use it in GitHub Desktop.
Save nuieskater/aeb5b91679184f41b94927dfb03eb747 to your computer and use it in GitHub Desktop.
Json data to your Xcode app
import Foundation
struct Welcome: Codable {
let greeting: String
let instructions: [InstructionElement]
}
enum InstructionElement: Codable {
case instructionClass(InstructionClass)
case string(String)
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if let x = try? container.decode(String.self) {
self = .string(x)
return
}
if let x = try? container.decode(InstructionClass.self) {
self = .instructionClass(x)
return
}
throw DecodingError.typeMismatch(InstructionElement.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for InstructionElement"))
}
func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case .instructionClass(let x):
try container.encode(x)
case .string(let x):
try container.encode(x)
}
}
}
struct InstructionClass: Codable {
let the1: The1
let the21: The21
let the22: The22
let the23: The1
let the20, the27: The13
let the4: The14
let the28, the6: The13
let the24: The22
let the13: The13
let the30: The22
let the2, the3, the26: The13
let the14: The14
let the31: The13
let the5: The14
let the19, the15: The13
let the7: The7
let the17: The13
let the25: The25
let the29, the8: The13
enum CodingKeys: String, CodingKey {
case the1 = "1"
case the21 = "21"
case the22 = "22"
case the23 = "23"
case the20 = "20"
case the27 = "27"
case the4 = "4"
case the28 = "28"
case the6 = "6"
case the24 = "24"
case the13 = "13"
case the30 = "30"
case the2 = "2"
case the3 = "3"
case the26 = "26"
case the14 = "14"
case the31 = "31"
case the5 = "5"
case the19 = "19"
case the15 = "15"
case the7 = "7"
case the17 = "17"
case the25 = "25"
case the29 = "29"
case the8 = "8"
}
}
struct The1: Codable {
let pairingID: Int
let primaryCurrency, secondaryCurrency: String
let change: Double
let lastPrice: Int
let volume24Hours: Double
let orderbook: The1_Orderbook
enum CodingKeys: String, CodingKey {
case pairingID = "pairing_id"
case primaryCurrency = "primary_currency"
case secondaryCurrency = "secondary_currency"
case change
case lastPrice = "last_price"
case volume24Hours = "volume_24hours"
case orderbook
}
}
struct The1_Orderbook: Codable {
let bids, asks: PurpleAsks
}
struct PurpleAsks: Codable {
let total: Int
let volume: Double
let highbid: Int
}
struct The13: Codable {
let pairingID: Int
let primaryCurrency: PrimaryCurrency
let secondaryCurrency: String
let change, lastPrice, volume24Hours: Double
let orderbook: The13_Orderbook
enum CodingKeys: String, CodingKey {
case pairingID = "pairing_id"
case primaryCurrency = "primary_currency"
case secondaryCurrency = "secondary_currency"
case change
case lastPrice = "last_price"
case volume24Hours = "volume_24hours"
case orderbook
}
}
struct The13_Orderbook: Codable {
let bids, asks: FluffyAsks
}
struct FluffyAsks: Codable {
let total: Int
let volume, highbid: Double
}
enum PrimaryCurrency: String, Codable {
case btc = "BTC"
case thb = "THB"
}
struct The14: Codable {
let pairingID: Int
let primaryCurrency, secondaryCurrency: String
let change: Int
let lastPrice, volume24Hours: Double
let orderbook: The13_Orderbook
enum CodingKeys: String, CodingKey {
case pairingID = "pairing_id"
case primaryCurrency = "primary_currency"
case secondaryCurrency = "secondary_currency"
case change
case lastPrice = "last_price"
case volume24Hours = "volume_24hours"
case orderbook
}
}
struct The21: Codable {
let pairingID: Int
let primaryCurrency, secondaryCurrency: String
let change: Double
let lastPrice: Int
let volume24Hours: Double
let orderbook: The21_Orderbook
enum CodingKeys: String, CodingKey {
case pairingID = "pairing_id"
case primaryCurrency = "primary_currency"
case secondaryCurrency = "secondary_currency"
case change
case lastPrice = "last_price"
case volume24Hours = "volume_24hours"
case orderbook
}
}
struct The21_Orderbook: Codable {
let bids: PurpleAsks
let asks: FluffyAsks
}
struct The22: Codable {
let pairingID: Int
let primaryCurrency, secondaryCurrency: String
let change, lastPrice, volume24Hours: Double
let orderbook: The21_Orderbook
enum CodingKeys: String, CodingKey {
case pairingID = "pairing_id"
case primaryCurrency = "primary_currency"
case secondaryCurrency = "secondary_currency"
case change
case lastPrice = "last_price"
case volume24Hours = "volume_24hours"
case orderbook
}
}
struct The25: Codable {
let pairingID: Int
let primaryCurrency, secondaryCurrency: String
let change: Double
let lastPrice: Int
let volume24Hours: Double
let orderbook: The13_Orderbook
enum CodingKeys: String, CodingKey {
case pairingID = "pairing_id"
case primaryCurrency = "primary_currency"
case secondaryCurrency = "secondary_currency"
case change
case lastPrice = "last_price"
case volume24Hours = "volume_24hours"
case orderbook
}
}
struct The7: Codable {
let pairingID: Int
let primaryCurrency, secondaryCurrency: String
let change: Int
let lastPrice: Double
let volume24Hours: Int
let orderbook: The13_Orderbook
enum CodingKeys: String, CodingKey {
case pairingID = "pairing_id"
case primaryCurrency = "primary_currency"
case secondaryCurrency = "secondary_currency"
case change
case lastPrice = "last_price"
case volume24Hours = "volume_24hours"
case orderbook
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment