JSONUtilities
JSONUtilities copied to clipboard
Handle types that are both RawRepresentable and JSONPrimitiveConvertible
The use case is you can already have an enum that conform to JSONPrimitiveConvertible. But if you need the enum to have a raw type like String, then you get an ambiguous call because it's both RawRepresentable and JSONPrimitiveConvertable. So adding these methods resolves that ambiguity.
eg:
public enum NotificationType: String, JSONPrimitiveConvertible {
case info, warning, success, error
public static func from(jsonValue: String) -> Self? {
self.init(rawValue: jsonValue.trimmingWhitespace.lowercased())
}
}
let x: NotificationType = properties.json(atKeyPath: "status") ?? .info
Now works with this PR.
Unfortunately function bodies are duplicated exactly from the JSONPrimitiveConvertable functions as I couldn't find a way to reference that exact method unambiguously.
@Jon889 could we have some unit tests for these new cases?