I am trying to decode data from Firestore
to json format but got that error.
This is model class
class UserModel : Codable{
var email: String?
var name: String?
var roles: [Roles]?
init(email: String, name: String, roles: [Roles]) {
self.email = email
self.name = name
self.roles = roles
}
}
This is Roles struct
struct Roles: Codable {
var role: String?
enum CodingKeys: String, CodingKey{
case role = "student"
}
}
I have checked some answers from stackoverflow but nothing help me.
This is how I decode it:
extension DocumentSnapshot {
func decode<T: Decodable>(as objectType: T.Type, includingId: Bool = true) throws -> T {
var documentJson = data()
if includingId {
documentJson?["id"] = documentID
}
let documentData = try JSONSerialization.data(withJSONObject: documentJson ?? "null", options: [])
let decodedObject = try JSONDecoder().decode(objectType, from: documentData)
return decodedObject
}
}
This is my Json file
{
name: "pisal",
email: "email@gmail.com",
roles: [
myrole : "student"
]
}