JsonDB icon indicating copy to clipboard operation
JsonDB copied to clipboard

如何保存 NSCoder 对象?

Open wells1013 opened this issue 9 years ago • 3 comments
trafficstars

如题

wells1013 avatar Nov 25 '15 02:11 wells1013

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.

pierredavidbelanger avatar Nov 26 '15 15:11 pierredavidbelanger

///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

wells1013 avatar Nov 27 '15 06:11 wells1013

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 :)

pierredavidbelanger avatar Nov 27 '15 12:11 pierredavidbelanger