summaryrefslogtreecommitdiff
path: root/SmartDeviceLink-iOS/SmartDeviceLink/SDLJsonDecoder.m
blob: 0ff561df80531ee3800d16875f8948ec11e9c3fa (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
//  SDLJsonDecoder.m
//

#import "SDLJsonDecoder.h"

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


@implementation SDLJsonDecoder

static NSObject<SDLDecoder> *jsonDecoderInstance;

+ (NSObject<SDLDecoder> *)instance {
    if (jsonDecoderInstance == nil) {
        jsonDecoderInstance = [[SDLJsonDecoder alloc] init];
    }
    return jsonDecoderInstance;
}

- (NSDictionary *)decode:(NSData *)data {
    if (data.length == 0) {
        [SDLDebugTool logInfo:@"Warning: Decoding JSON data, no JSON to decode" withType:SDLDebugType_Protocol];
        return nil;
    }

    NSError *error = nil;
    NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

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

    return jsonObject;
}

@end