Files
Nextcloud-Cookbook-iOS/Nextcloud Cookbook iOS Client/Util/JsonAny.swift
2024-05-05 10:33:33 +02:00

36 lines
1.1 KiB
Swift

//
// JsonAny.swift
// Nextcloud Cookbook iOS Client
//
// Created by Vincent Meilinger on 05.05.24.
//
import Foundation
struct JSONAny: Codable {
let value: Any
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if let intVal = try? container.decode(Int.self) {
value = intVal
} else if let stringVal = try? container.decode(String.self) {
value = stringVal
} else {
throw DecodingError.typeMismatch(JSONAny.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Unsupported type for JSONAny"))
}
}
func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch value {
case let intValue as Int:
try container.encode(intValue)
case let stringValue as String:
try container.encode(stringValue)
default:
throw EncodingError.invalidValue(value, EncodingError.Context(codingPath: encoder.codingPath, debugDescription: "Unsupported type for JSONAny"))
}
}
}