Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void Sq_DecodeJSONTable(HSQUIRRELVM v, cJSON *Item) {
- if(!Item) return;
- while(Item) {
- if(Item->string)
- sq_pushstring(v, Item->string, -1);
- switch(Item->type) {
- case cJSON_False:
- sq_pushbool(v, SQFalse);
- break;
- case cJSON_True:
- sq_pushbool(v, SQTrue);
- break;
- case cJSON_NULL:
- sq_pushnull(v);
- break;
- case cJSON_Number:
- sq_pushinteger(v, Item->valueint);
- break;
- case cJSON_String:
- sq_pushstring(v, Item->valuestring, -1);
- break;
- case cJSON_Array:
- sq_newarray(v, 0);
- Sq_DecodeJSONTable(v, Item->child);
- break;
- case cJSON_Object:
- sq_newtable(v);
- Sq_DecodeJSONTable(v, Item->child);
- break;
- }
- if(Item->string)
- sq_newslot(v,-3,SQFalse);
- else
- sq_arrayappend(v, -2);
- Item = Item->next;
- }
- }
- SQInteger Sq_DecodeJSON(HSQUIRRELVM v) {
- // more security than compilestring()
- const SQChar *Str;
- sq_getstring(v, 2, &Str);
- cJSON *Root = cJSON_Parse(Str);
- if(!Root || !Root->child) return 0;
- sq_newtable(v);
- Sq_DecodeJSONTable(v, Root->child);
- cJSON_Delete(Root);
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement