Created
March 10, 2018 18:11
-
-
Save rweichler/cc1d6281818600dad75d7c8b25c73108 to your computer and use it in GitHub Desktop.
convert cJSON thing to nsarray
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import <Foundation/Foundation.h> | |
| #import <cJSON.h> | |
| cJSON * jjjj_apt_list_parse(FILE *f, int *errlineno, int flags); | |
| id json_to_id(cJSON *json) | |
| { | |
| if(json == NULL) { | |
| return nil; | |
| } | |
| cJSON *child; | |
| switch(json->type) { | |
| case cJSON_False: | |
| return [NSNumber numberWithBool:false]; | |
| case cJSON_True: | |
| return [NSNumber numberWithBool:true]; | |
| case cJSON_Number: | |
| return [NSNumber numberWithDouble:json->valuedouble]; | |
| case cJSON_String: | |
| return [NSString stringWithUTF8String:json->valuestring]; | |
| case cJSON_Array:; | |
| NSMutableArray *arr = NSMutableArray.array; | |
| child = json->child; | |
| while(child) { | |
| id v = json_to_id(child); | |
| if(v) { | |
| [arr addObject:json_to_id(child)]; | |
| } | |
| child = child->next; | |
| } | |
| return arr; | |
| case cJSON_Object:; | |
| NSMutableDictionary *dict = NSMutableDictionary.dictionary; | |
| child = json->child; | |
| while(child) { | |
| NSString *k = [NSString stringWithUTF8String:child->string]; | |
| id v = json_to_id(child); | |
| if(k && v) { | |
| [dict setObject:v forKey:k]; | |
| } | |
| child = child->next; | |
| } | |
| return dict; | |
| default: | |
| return nil; | |
| } | |
| } | |
| id packages_to_id(const char *path) | |
| { | |
| FILE *f = fopen(path, "r"); | |
| cJSON *json = jjjj_apt_list_parse(f, NULL, 0); | |
| id obj = json_to_id(json); | |
| cJSON_Delete(json); | |
| return obj; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment