cJSON数据解析关于数据解析部分,其实这个解析就是个自动机,通过递归或者解析栈进行实现数据的解析/* Utility to jump whitespace and cr/lf *///用于跳过ascii小于32的空白字符static const char *skip(const ...
关于数据解析部分,其实这个解析就是个自动机,通过递归或者解析栈进行实现数据的解析
/* Utility to jump whitespace and cr/lf */ //设置结构的具体类型并且返回下一个将要解读数据的位置 if (!strncmp(value, "null", 4)) { item->type = cJSON_NULL; return value + 4; } if (!strncmp(value, "false", 5)) { item->type = cJSON_False; return value + 5; } if (!strncmp(value, "true", 4)) { item->type = cJSON_True; item->valueint = 1; return value + 4; } if (*value == '\"') { return parse_string(item, value); } if (*value == '-' || (*value >= '0' && *value <= '9')) { return parse_number(item, value); } if (*value == '[') { return parse_array(item, value); } if (*value == '{') { return parse_object(item, value); } ep = value; return 0; /* failure. */}
/* Build an array from input text. */static const char *parse_array(cJSON *item, const char *value){ cJSON *child; if (*value != '[') { ep = value; return 0; } /* not an array! */ item->type = cJSON_Array; value = skip(value + 1); if (*value == ']') return value + 1; /* empty array. */ item->child = child = cJSON_New_Item(); if (!item->child) return 0; /* memory fail */ //解析数组内结构 value = skip(parse_value(child, skip(value))); /* skip any spacing, get the value. */ if (!value) return 0; while (*value == ',') { cJSON *new_item; if (!(new_item = cJSON_New_Item())) return 0; /* memory fail */ child->next = new_item; new_item->prev = child; child = new_item; value = skip(parse_value(child, skip(value + 1))); if (!value) return 0; /* memory fail */ } if (*value == ']') return value + 1; /* end of array */ ep = value; return 0; /* malformed. */}
原标题:cJONS序列化工具解读二(数据解析)
关键词:序列
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。