I want to decode json responses of a websocket "notification" where the type of notification is within the json response.
JSON example:
{
"jsonrpc": "2.0",
"method": "Application.OnVolumeChanged",
"params": {
"data": {
"muted": false,
"volume": 88.6131134033203125
},
"sender": "xbmc"
}
}
This is what I currently have:
func notificationMessage(text: String) {
do {
if let jsonData = text.data(using: .utf8),
let json = try JSONSerialization.jsonObject(with: jsonData) as? [String: Any],
let method = json["method"] as? String,
let methodName = method.split(separator: ".").last?.description {
let decoder = JSONDecoder()
let object = try decoder.decode(OnVolumeChanged.self, from: jsonData)
print(object)
}
} catch {
print("Error deserializing JSON: \(error)")
}
}
Now I somehow want to use methodName
instead of OnVolumeChanged.self
.
But I don't feel like making a huge switch case on the methodName since I can get like hundreds of diferent methods
I have tried NSClassFromString(methodName)
but this is giving me AnyClass?
which is not a concrete type.
Is there a way to get a class type from string?