Recipe decoding fixes

This commit is contained in:
VincentMeilinger
2024-05-05 10:33:33 +02:00
parent 6ae9926c41
commit b66ef63b6a
8 changed files with 98 additions and 18 deletions

View File

@@ -0,0 +1,35 @@
//
// 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"))
}
}
}