Search code examples
iphonejsoniosipadtouchjson

Problems serializing a dictionary with TouchJson


i'm developing a small app for the ipad and i'm trying to serialize a dictionary to NSData to save in the disk. I'm using with framework TouchJson. And example of my example structure:

{
line =     {
    78986928 =         (
        "NSPoint: {442, 266}",
        (...)
        "NSPoint: {370, 634}"
    );
};

}

The structure of my dictionary is: an dictionary with dictionaries inside. This dictionaries have a string (ID) and an NSMutableArray with an NSValue.

The line of code is that i'm using is:

NSData *jsonData = [[CJSONSerializer serializer] serializeObject:templates error:&error];

The error that the variable error give me is:

2011-03-23 10:12:12.957 GestureFramework[286:207] Error Domain=TODO_DOMAIN Code=-1 "Could not serialize object '{
line =     {
    78986928 =         (
        "NSPoint: {442, 266}",
        (...)
        "NSPoint: {370, 634}"
    );
};
}'" UserInfo=0x4e27aa0 {NSLocalizedDescription=Could not serialize object '{
    line =     {
        78986928 =         (
            "NSPoint: {442, 266}",
            (...)
            "NSPoint: {370, 634}"
        );
    };
}'}

Thnx in advance


Solution

  • TouchJSON supports serializing the following types:

    • NSNull
    • NSNumber
    • NSString
    • NSArray
    • NSDictionary
    • NSData

    If you'd like to serialize another type you'll need to implement -(NSData*)JSONDataRepresentation (either on a subclass or category).

    Here's an example that I used for NSDate:

    @interface NSDate (JSONDataRepresentation)
    - (NSData*)JSONDataRepresentation; 
    @end
    

    .

    @implementation NSDate (JSONDataRepresentation)
    - (NSData*)JSONDataRepresentation 
    {
        return [@"\"didn't want to waste the space to do the real conversion\"" dataUsingEncoding: NSUTF8StringEncoding]; 
    }
    @end