JsonDB
JsonDB copied to clipboard
如何保存 NSCoder 对象?
如题
I do not reed chinese ( english is not even my primary language :))
But from the keywords I can understand, I guess your question is:
Do JsonDB support storing NSCoder ?
The answer is: no and yes.
No, because NSCoder is only an abstract class. There is no way for JsonDB to know what you want to do with it (JsonDB do not know which -decode method to call).
And Yes, because you only have to decode it (into a NSJSONSerialization compatible object) and it will work.
More generally, if [NSJSONSerialization isValidJSONObject:yourObject], than JsonDB can store it.
///in JDBDatabase
- (id)JSONMutableObjectWithData:(NSData *)data error:(NSError **)error {
NSDictionary *dic = [self JSONObjectWithData:data error:error];
return [NSMutableDictionary dictionaryWithDictionary:dic];
}
- (id)JSONObjectWithData:(NSData *)data error:(NSError **)error {
return [NSKeyedUnarchiver unarchiveObjectWithData:data];
}
- (NSData *)dataWithJSONObject:(id)object error:(NSError **)error {
return [NSKeyedArchiver archivedDataWithRootObject:object];
}
I guess you just save NSData to database, NSDictionary suport archive, it will good
Indeed, the document is parsed to be stored into a flat form, and also the original document is converted to NSData to be saved.
The object document needs to be NSJSONSerialization compatible, but do not needs to be stored in JSON.
It would indeed take less space to archive it, I wonder why I did not do this in the first place.
This would break existing installation thought, I will need to think about how I do this.
Thank you for the suggestion :)