1/*
2 *  Copyright 2014 The WebRTC Project Authors. All rights reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11#import "RTCICECandidate+JSON.h"
12
13#import "RTCLogging.h"
14
15static NSString const *kRTCICECandidateTypeKey = @"type";
16static NSString const *kRTCICECandidateTypeValue = @"candidate";
17static NSString const *kRTCICECandidateMidKey = @"id";
18static NSString const *kRTCICECandidateMLineIndexKey = @"label";
19static NSString const *kRTCICECandidateSdpKey = @"candidate";
20
21@implementation RTCICECandidate (JSON)
22
23+ (RTCICECandidate *)candidateFromJSONDictionary:(NSDictionary *)dictionary {
24  NSString *mid = dictionary[kRTCICECandidateMidKey];
25  NSString *sdp = dictionary[kRTCICECandidateSdpKey];
26  NSNumber *num = dictionary[kRTCICECandidateMLineIndexKey];
27  NSInteger mLineIndex = [num integerValue];
28  return [[RTCICECandidate alloc] initWithMid:mid index:mLineIndex sdp:sdp];
29}
30
31- (NSData *)JSONData {
32  NSDictionary *json = @{
33    kRTCICECandidateTypeKey : kRTCICECandidateTypeValue,
34    kRTCICECandidateMLineIndexKey : @(self.sdpMLineIndex),
35    kRTCICECandidateMidKey : self.sdpMid,
36    kRTCICECandidateSdpKey : self.sdp
37  };
38  NSError *error = nil;
39  NSData *data =
40      [NSJSONSerialization dataWithJSONObject:json
41                                      options:NSJSONWritingPrettyPrinted
42                                        error:&error];
43  if (error) {
44    RTCLogError(@"Error serializing JSON: %@", error);
45    return nil;
46  }
47  return data;
48}
49
50@end
51