How to serialize a class in IOS sdk (Objective-c)?

2020-02-29 01:34发布

How to serialize the following class in objective-c so that it can be used with SBJson?

I get "JSON serialisation not supported for Animal" error when I use this code.

Can someone point out where I am going wrong?

The contents of Animal.h file is as below

#import <UIKit/UIKit.h>

@interface Animal : NSObject<NSCoding> {
    NSString *name;
    NSString *description;
    NSString *imageURL;
}

@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *description;
@property (nonatomic, retain) NSString *imageURL;

-(id)initWithName:(NSString *)n description:(NSString *)d url:(NSString *)u;

@end

The contents of Animal.m file is as below

#import "Animal.h"

@implementation Animal
@synthesize name, description, imageURL;

-(id)initWithName:(NSString *)n description:(NSString *)d url:(NSString *)u {
    self.name = n;
    self.description = d;
    self.imageURL = u;
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if(self = [super initWithCoder:aDecoder])
    {
        name = [[aDecoder decodeObjectForKey:@"name"] retain];
        description = [[aDecoder decodeObjectForKey:@"description"] retain];
        imageURL = [[aDecoder decodeObjectForKey:@"imageURL"] retain];
    }
    return [self initWithName:name description:description url:imageURL];
}

- (void)encodeWithCoder:(NSCoder *)encoder
{
    [super encodeWithCoder:encoder];
    [encoder encodeObject:name forKey:@"name"];
    [encoder encodeObject:description forKey:@"description"];
    [encoder encodeObject:imageURL forKey:@"imageURL"];
}    

@end

7条回答
ゆ 、 Hurt°
2楼-- · 2020-02-29 01:46

Check the newly introduced NSJSONSerialization class:

NSJSONSerialization class

查看更多
Bombasti
3楼-- · 2020-02-29 01:51

To actually answer your question on how to do it using SBJson: Implement the proxyForJson method. Unless you are serializing an NSArray or NSDictionary you must override this method for serialization to work.

You can see that this is the case by looking at the SBJson source code (in SBJsonWriter.m):

- (NSData*)dataWithObject:(id)object {    
...   
    if ([object isKindOfClass:[NSDictionary class]])
                ok = [streamWriter writeObject:object];

            else if ([object isKindOfClass:[NSArray class]])
                ok = [streamWriter writeArray:object];

            else if ([object respondsToSelector:@selector(proxyForJson)])
                return [self dataWithObject:[object proxyForJson]];
            else {
                self.error = @"Not valid type for JSON";
                return nil;
            }
    ...
    }
}

Implement proxyForJson in Animal.m like this (not tested):

- (NSDictionary*) proxyForJson
{
return [NSDictionary dictionaryWithObjectsAndKeys:self.name, @"name",
                                                  self.description, @"description",
                                                  self.imageURL, @"imageURL", 
                                                  nil];
}
查看更多
forever°为你锁心
4楼-- · 2020-02-29 01:53

I think you can check out this if it helps you: Make a Custom Class Serializable

查看更多
倾城 Initia
6楼-- · 2020-02-29 02:06

This open source project JSONCoding makes the whole process pretty simple, using the new sdk class in conjunction with the NSCoding protocol.

查看更多
我想做一个坏孩纸
7楼-- · 2020-02-29 02:06

See also https://github.com/jsonmodel/jsonmodel

Magical Data Modeling Framework for JSON - allows rapid creation of smart data models. You can use it in your iOS, macOS, watchOS and tvOS apps.

This is also the chosen library for the Objective-c Swagger client.

查看更多
登录 后发表回答