Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

75 lines (69 sloc) 2.908 kB
//
// MessagePackParser.m
// Fetch TV Remote
//
// Created by Chris Hulbert on 23/06/11.
// Copyright 2011 Digital Five. All rights reserved.
//
#import "MessagePackParser.h"
#include "msgpack_src/msgpack.h"
@implementation MessagePackParser
+(id) createUnpackedObject:(msgpack_object)obj {
switch (obj.type) {
case MSGPACK_OBJECT_BOOLEAN:
return [[NSNumber alloc] initWithBool:obj.via.boolean];
break;
case MSGPACK_OBJECT_POSITIVE_INTEGER:
return [[NSNumber alloc] initWithUnsignedLongLong:obj.via.u64];
break;
case MSGPACK_OBJECT_NEGATIVE_INTEGER:
return [[NSNumber alloc] initWithLongLong:obj.via.i64];
break;
case MSGPACK_OBJECT_DOUBLE:
return [[NSNumber alloc] initWithDouble:obj.via.dec];
break;
case MSGPACK_OBJECT_RAW:
return [[NSData alloc] initWithBytes:obj.via.raw.ptr length:obj.via.raw.size];
// return [[NSString alloc] initWithBytes:obj.via.raw.ptr length:obj.via.raw.size encoding:NSUTF8StringEncoding];
break;
case MSGPACK_OBJECT_ARRAY:
{
NSMutableArray *arr = [[NSMutableArray alloc] initWithCapacity:obj.via.array.size];
msgpack_object* const pend = obj.via.array.ptr + obj.via.array.size;
for(msgpack_object *p= obj.via.array.ptr;p < pend;p++){
id newArrayItem = [self createUnpackedObject:*p];
[arr addObject:newArrayItem];
}
return arr;
}
break;
case MSGPACK_OBJECT_MAP:
{
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity:obj.via.map.size];
msgpack_object_kv* const pend = obj.via.map.ptr + obj.via.map.size;
for(msgpack_object_kv* p = obj.via.map.ptr; p < pend; p++){
NSData *keyData = [self createUnpackedObject:p->key];
id val = [self createUnpackedObject:p->val];
NSString *key = [[NSString alloc] initWithData:keyData
encoding:NSUTF8StringEncoding];
[dict setValue:val forKey:key];
}
return dict;
}
break;
case MSGPACK_OBJECT_NIL:
default:
return [NSNull null]; // Since nsnull is a system singleton, we don't have to worry about ownership of it
break;
}
}
// Parse the given messagepack data into a NSDictionary or NSArray typically
+ (id)parseData:(NSData*)data {
msgpack_unpacked msg;
msgpack_unpacked_init(&msg);
bool success = msgpack_unpack_next(&msg, data.bytes, data.length, NULL); // Parse it into C-land
id results = success ? [self createUnpackedObject:msg.data] : nil; // Convert from C-land to Obj-c-land
msgpack_unpacked_destroy(&msg); // Free the parser
return results;
}
@end
Jump to Line
Something went wrong with that request. Please try again.