




#import "ACMessage.h"

#define ACNotNilStr(str) ([str isKindOfClass:[NSString class]] ? str : @"")

@implementation ACMessage

- (NSString *)json
{
    NSMutableDictionary *subDic = [NSMutableDictionary new];
    [subDic setObject:[NSString stringWithFormat:@"%d",self.responseCode] forKey:@"code"];
    if (self.responseDic) {
        [subDic setObject:self.responseDic forKey:@"data"];
    }
    NSDictionary *fullDic = @{@"callbackId": ACNotNilStr(self.callbackIdStr),
                          @"responseData":subDic,
                          };
    NSString *jsonStr = nil;
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:fullDic
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:&error];
    if (jsonData) {
        jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    }
    return jsonStr;
}

-(NSString *)ObjectToJsonString:(id)object
{
    if (object == nil) {
        return nil;
    }
    NSString *jsonString = nil;
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:object
                                                       options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
                                                         error:&error];
    if (jsonData) {
        jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    }
    return jsonString;
}

@end
