summaryrefslogtreecommitdiff
path: root/SmartDeviceLink/SDLJsonEncoder.m
blob: dbb2374d9922dda539e201338b7ec71c5e67ada3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//  SDLJsonEncoder.m
//

#import "SDLJsonEncoder.h"

#import "SDLDebugTool.h"
#import "SDLNames.h"


@implementation SDLJsonEncoder

static NSObject<SDLEncoder> *jsonEncoderInstance;

+ (NSObject<SDLEncoder> *)instance {
    if (jsonEncoderInstance == nil) {
        jsonEncoderInstance = [[SDLJsonEncoder alloc] init];
    }
    return jsonEncoderInstance;
}

- (NSData *)encodeDictionary:(NSDictionary *)dict {
    if (dict == nil) {
        [SDLDebugTool logInfo:@"Warning: Encoding dictionary to JSON, no dictionary passed" withType:SDLDebugType_Protocol];
        return nil;
    }

    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:kNilOptions error:&error];

    if (error != nil) {
        [SDLDebugTool logInfo:[NSString stringWithFormat:@"Error encoding JSON data: %@", error] withType:SDLDebugType_Protocol];
        return nil;
    }

    return jsonData;
}

@end