iOS/Sources/Shared/Common/Extensions/KeyedDecodingContainer+JSON.swift
Zac West 4d9a530637
Reorganize files in repo, pull out build settings from pbxproj (#1140)
This is somewhat in prep of being able to make the project file generated, but also just organizes things into more concrete directory structures.

This pulls out _all_ of the build settings from the root level, and most from the target level, into xcconfigs.

The new directory structure looks like:

- Sources
  - App
    - (everything from HomeAssistant/)
  - WatchApp
  - Shared
  - MacBridge
  - Extensions
    - Intents
    - NotificationContent
    - NotificationService
    - Share
    - Today
    - Watch
    - Widgets
- Tests
  - App
  - UI
  - Shared

Somewhat intentionally, the file structure under these is not yet standardized/organized.

The project targets are now:

- App
- WatchApp
- Shared-iOS
- Shared-watchOS
- MacBridge
- Tests-App
- Tests-UI
- Tests-Shared
- Extension-Intents
- Extension-NotificationContent
- Extension-NotificationService
- Extension-Share
- Extension-Today
- Extension-Widget
- WatchExtension-Watch

This does not yet clean up resources vs. sources, nor does it handle some of the "it's in Sources/App but it's part of Shared" crossover directory issues.
2020-10-03 00:15:04 -07:00

172 lines
6.0 KiB
Swift

//
// KeyedDecodingContainer+JSON.swift
// HomeAssistant
//
// Created by Robert Trencheny on 4/9/19.
// Copyright © 2019 Robbie Trencheny. All rights reserved.
//
import Foundation
// https://stackoverflow.com/a/46049763/486182
//
// JSONCodingKeys.swift
//
// original https://gist.github.com/loudmouth/332e8d89d8de2c1eaf81875cfcd22e24
import CoreGraphics
public struct JSONCodingKeys: CodingKey {
public var stringValue: String
public init(stringValue: String) {
self.stringValue = stringValue
}
public var intValue: Int?
public init?(intValue: Int) {
self.init(stringValue: "\(intValue)")
self.intValue = intValue
}
}
public extension KeyedDecodingContainer {
func decode(_ type: [String: Any].Type, forKey key: K) throws -> [String: Any] {
let container = try self.nestedContainer(keyedBy: JSONCodingKeys.self, forKey: key)
return try container.decode(type)
}
func decode(_ type: [Any].Type, forKey key: K) throws -> [Any] {
var container = try self.nestedUnkeyedContainer(forKey: key)
return try container.decode(type)
}
func decode(_ type: [String: Any].Type) throws -> [String: Any] {
var dictionary = [String: Any]()
for key in allKeys {
if let boolValue = try? decode(Bool.self, forKey: key) {
dictionary[key.stringValue] = boolValue
} else if let stringValue = try? decode(String.self, forKey: key) {
dictionary[key.stringValue] = stringValue
} else if let intValue = try? decode(Int.self, forKey: key) {
dictionary[key.stringValue] = intValue
} else if let doubleValue = try? decode(Double.self, forKey: key) {
dictionary[key.stringValue] = doubleValue
} else if let nestedDictionary = try? decode([String: Any].self, forKey: key) {
dictionary[key.stringValue] = nestedDictionary
} else if let nestedArray = try? decode([Any].self, forKey: key) {
dictionary[key.stringValue] = nestedArray
}
}
return dictionary
}
}
public extension UnkeyedDecodingContainer {
mutating func decode(_ type: [Any].Type) throws -> [Any] {
var array: [Any] = []
while isAtEnd == false {
if let value = try? decode(Bool.self) {
array.append(value)
} else if let value = try? decode(Double.self) {
array.append(value)
} else if let value = try? decode(String.self) {
array.append(value)
} else if let nestedDictionary = try? decode([String: Any].self) {
array.append(nestedDictionary)
} else if let nestedArray = try? decode([Any].self) {
array.append(nestedArray)
}
}
return array
}
mutating func decode(_ type: [String: Any].Type) throws -> [String: Any] {
let nestedContainer = try self.nestedContainer(keyedBy: JSONCodingKeys.self)
return try nestedContainer.decode(type)
}
}
public extension KeyedEncodingContainerProtocol where Key == JSONCodingKeys {
mutating func encode(_ value: [String: Any]) throws {
try value.forEach({ (key, value) in
let key = JSONCodingKeys(stringValue: key)
switch value {
case let value as Bool:
try encode(value, forKey: key)
case let value as Int:
try encode(value, forKey: key)
case let value as String:
try encode(value, forKey: key)
case let value as Double:
try encode(value, forKey: key)
case let value as CGFloat:
try encode(value, forKey: key)
case let value as [String: Any]:
try encode(value, forKey: key)
case let value as [Any]:
try encode(value, forKey: key)
// swiftlint:disable:next syntactic_sugar
case Optional<Any>.none:
try encodeNil(forKey: key)
default:
let err = EncodingError.Context(codingPath: codingPath + [key], debugDescription: "Invalid JSON value")
throw EncodingError.invalidValue(value, err)
}
})
}
}
public extension KeyedEncodingContainerProtocol {
mutating func encode(_ value: [String: Any]?, forKey key: Key) throws {
if value != nil {
var container = self.nestedContainer(keyedBy: JSONCodingKeys.self, forKey: key)
try container.encode(value!)
}
}
mutating func encode(_ value: [Any]?, forKey key: Key) throws {
if value != nil {
var container = self.nestedUnkeyedContainer(forKey: key)
try container.encode(value!)
}
}
}
public extension UnkeyedEncodingContainer {
mutating func encode(_ value: [Any]) throws {
try value.enumerated().forEach({ (index, value) in
switch value {
case let value as Bool:
try encode(value)
case let value as Int:
try encode(value)
case let value as String:
try encode(value)
case let value as Double:
try encode(value)
case let value as CGFloat:
try encode(value)
case let value as [String: Any]:
try encode(value)
case let value as [Any]:
try encode(value)
// swiftlint:disable:next syntactic_sugar
case Optional<Any>.none:
try encodeNil()
default:
let keys = JSONCodingKeys(intValue: index).map({ [ $0 ] }) ?? []
let err = EncodingError.Context(codingPath: codingPath + keys, debugDescription: "Invalid JSON value")
throw EncodingError.invalidValue(value, err)
}
})
}
mutating func encode(_ value: [String: Any]) throws {
var nestedContainer = self.nestedContainer(keyedBy: JSONCodingKeys.self)
try nestedContainer.encode(value)
}
}