Build-in CoreStoreObject to JSON encoding #223

Open
opened 2025-12-29 15:26:57 +01:00 by adam · 2 comments
Owner

Originally created by @DimaZava on GitHub (Jul 17, 2018).

Originally created by @DimaZava on GitHub (Jul 17, 2018).
adam added the enhancement label 2025-12-29 15:26:57 +01:00
Author
Owner

@HaiyuanZh commented on GitHub (Mar 23, 2019):

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
    }
}
@HaiyuanZh commented on GitHub (Mar 23, 2019): 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 } } ```
Author
Owner

@Saklad5 commented on GitHub (May 6, 2020):

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.

@Saklad5 commented on GitHub (May 6, 2020): 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.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#223