CoreStore
CoreStore copied to clipboard
Build-in CoreStoreObject to JSON encoding
might try this:
extension CoreStoreObject {
func toDict() -> Dictionary<String, Any> {
var dict = Dictionary<String, Any>()
self.cs_toRaw().entity.attributesByName.forEach { (field, value) in
dict[field] = self.cs_toRaw().getValue(forKvcKey: field)
}
return dict
}
}
If this is ever done, it should be in the form of automatic conformance to Encodable. However, given that encoding inverse relationships would lead to infinite recursion, I think the entire concept of encoding single objects is flawed.
Personally, I think it would make more sense to implement an NSAtomicStore subclass for JSON, along the lines of the existing XML store. I don’t think that needs to be part of CoreStore to work, either.