Parse an object - create a new root, and populate. */
| 1140 | |
| 1141 | /* Parse an object - create a new root, and populate. */ |
| 1142 | CJSON_PUBLIC(cJSON *) cJSON_ParseWithLengthOpts(const char *value, size_t buffer_length, const char **return_parse_end, cJSON_bool require_null_terminated) |
| 1143 | { |
| 1144 | parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } }; |
| 1145 | cJSON *item = NULL; |
| 1146 | |
| 1147 | /* reset error position */ |
| 1148 | global_error.json = NULL; |
| 1149 | global_error.position = 0; |
| 1150 | |
| 1151 | if (value == NULL || 0 == buffer_length) |
| 1152 | { |
| 1153 | goto fail; |
| 1154 | } |
| 1155 | |
| 1156 | buffer.content = (const unsigned char*)value; |
| 1157 | buffer.length = buffer_length; |
| 1158 | buffer.offset = 0; |
| 1159 | buffer.hooks = global_hooks; |
| 1160 | |
| 1161 | item = cJSON_New_Item(&global_hooks); |
| 1162 | if (item == NULL) /* memory fail */ |
| 1163 | { |
| 1164 | goto fail; |
| 1165 | } |
| 1166 | |
| 1167 | if (!parse_value(item, buffer_skip_whitespace(skip_utf8_bom(&buffer)))) |
| 1168 | { |
| 1169 | /* parse failure. ep is set. */ |
| 1170 | goto fail; |
| 1171 | } |
| 1172 | |
| 1173 | /* if we require null-terminated JSON without appended garbage, skip and then check for a null terminator */ |
| 1174 | if (require_null_terminated) |
| 1175 | { |
| 1176 | buffer_skip_whitespace(&buffer); |
| 1177 | if ((buffer.offset >= buffer.length) || buffer_at_offset(&buffer)[0] != '\0') |
| 1178 | { |
| 1179 | goto fail; |
| 1180 | } |
| 1181 | } |
| 1182 | if (return_parse_end) |
| 1183 | { |
| 1184 | *return_parse_end = (const char*)buffer_at_offset(&buffer); |
| 1185 | } |
| 1186 | |
| 1187 | return item; |
| 1188 | |
| 1189 | fail: |
| 1190 | if (item != NULL) |
| 1191 | { |
| 1192 | cJSON_Delete(item); |
| 1193 | } |
| 1194 | |
| 1195 | if (value != NULL) |
| 1196 | { |
| 1197 | error local_error; |
| 1198 | local_error.json = (const unsigned char*)value; |
| 1199 | local_error.position = 0; |
no test coverage detected
searching dependent graphs…