Parse an object - create a new root, and populate. */
| 1104 | |
| 1105 | /* Parse an object - create a new root, and populate. */ |
| 1106 | CJSON_PUBLIC(cJSON *) cJSON_ParseWithLengthOpts(const char *value, size_t buffer_length, const char **return_parse_end, cJSON_bool require_null_terminated) |
| 1107 | { |
| 1108 | parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } }; |
| 1109 | cJSON *item = NULL; |
| 1110 | |
| 1111 | /* reset error position */ |
| 1112 | global_error.json = NULL; |
| 1113 | global_error.position = 0; |
| 1114 | |
| 1115 | if (value == NULL || 0 == buffer_length) |
| 1116 | { |
| 1117 | goto fail; |
| 1118 | } |
| 1119 | |
| 1120 | buffer.content = (const unsigned char*)value; |
| 1121 | buffer.length = buffer_length; |
| 1122 | buffer.offset = 0; |
| 1123 | buffer.hooks = global_hooks; |
| 1124 | |
| 1125 | item = cJSON_New_Item(&global_hooks); |
| 1126 | if (item == NULL) /* memory fail */ |
| 1127 | { |
| 1128 | goto fail; |
| 1129 | } |
| 1130 | |
| 1131 | if (!parse_value(item, buffer_skip_whitespace(skip_utf8_bom(&buffer)))) |
| 1132 | { |
| 1133 | /* parse failure. ep is set. */ |
| 1134 | goto fail; |
| 1135 | } |
| 1136 | |
| 1137 | /* if we require null-terminated JSON without appended garbage, skip and then check for a null terminator */ |
| 1138 | if (require_null_terminated) |
| 1139 | { |
| 1140 | buffer_skip_whitespace(&buffer); |
| 1141 | if ((buffer.offset >= buffer.length) || buffer_at_offset(&buffer)[0] != '\0') |
| 1142 | { |
| 1143 | goto fail; |
| 1144 | } |
| 1145 | } |
| 1146 | if (return_parse_end) |
| 1147 | { |
| 1148 | *return_parse_end = (const char*)buffer_at_offset(&buffer); |
| 1149 | } |
| 1150 | |
| 1151 | return item; |
| 1152 | |
| 1153 | fail: |
| 1154 | if (item != NULL) |
| 1155 | { |
| 1156 | cJSON_Delete(item); |
| 1157 | } |
| 1158 | |
| 1159 | if (value != NULL) |
| 1160 | { |
| 1161 | error local_error; |
| 1162 | local_error.json = (const unsigned char*)value; |
| 1163 | local_error.position = 0; |
no test coverage detected
searching dependent graphs…