jsmn.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2010 Serge Zaitsev
  5. *
  6. * [License text continues...]
  7. */
  8. #include <jsmn/jsmn.h>
  9. /**
  10. * Allocates a fresh unused token from the token pool.
  11. */
  12. static jsmntok_t *jsmn_alloc_token(jsmn_parser *parser, jsmntok_t *tokens,
  13. const size_t num_tokens)
  14. {
  15. jsmntok_t *tok;
  16. if (parser->toknext >= num_tokens)
  17. {
  18. return NULL;
  19. }
  20. tok = &tokens[parser->toknext++];
  21. tok->start = tok->end = -1;
  22. tok->size = 0;
  23. #ifdef JSMN_PARENT_LINKS
  24. tok->parent = -1;
  25. #endif
  26. return tok;
  27. }
  28. /**
  29. * Fills token type and boundaries.
  30. */
  31. static void jsmn_fill_token(jsmntok_t *token, const jsmntype_t type,
  32. const int start, const int end)
  33. {
  34. token->type = type;
  35. token->start = start;
  36. token->end = end;
  37. token->size = 0;
  38. }
  39. /**
  40. * Fills next available token with JSON primitive.
  41. */
  42. static int jsmn_parse_primitive(jsmn_parser *parser, const char *js,
  43. const size_t len, jsmntok_t *tokens,
  44. const size_t num_tokens)
  45. {
  46. jsmntok_t *token;
  47. int start;
  48. start = parser->pos;
  49. for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++)
  50. {
  51. switch (js[parser->pos])
  52. {
  53. #ifndef JSMN_STRICT
  54. /* In strict mode primitive must be followed by "," or "}" or "]" */
  55. case ':':
  56. #endif
  57. case '\t':
  58. case '\r':
  59. case '\n':
  60. case ' ':
  61. case ',':
  62. case ']':
  63. case '}':
  64. goto found;
  65. default:
  66. /* to quiet a warning from gcc*/
  67. break;
  68. }
  69. if (js[parser->pos] < 32 || js[parser->pos] >= 127)
  70. {
  71. parser->pos = start;
  72. return JSMN_ERROR_INVAL;
  73. }
  74. }
  75. #ifdef JSMN_STRICT
  76. /* In strict mode primitive must be followed by a comma/object/array */
  77. parser->pos = start;
  78. return JSMN_ERROR_PART;
  79. #endif
  80. found:
  81. if (tokens == NULL)
  82. {
  83. parser->pos--;
  84. return 0;
  85. }
  86. token = jsmn_alloc_token(parser, tokens, num_tokens);
  87. if (token == NULL)
  88. {
  89. parser->pos = start;
  90. return JSMN_ERROR_NOMEM;
  91. }
  92. jsmn_fill_token(token, JSMN_PRIMITIVE, start, parser->pos);
  93. #ifdef JSMN_PARENT_LINKS
  94. token->parent = parser->toksuper;
  95. #endif
  96. parser->pos--;
  97. return 0;
  98. }
  99. /**
  100. * Fills next token with JSON string.
  101. */
  102. static int jsmn_parse_string(jsmn_parser *parser, const char *js,
  103. const size_t len, jsmntok_t *tokens,
  104. const size_t num_tokens)
  105. {
  106. jsmntok_t *token;
  107. int start = parser->pos;
  108. /* Skip starting quote */
  109. parser->pos++;
  110. for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++)
  111. {
  112. char c = js[parser->pos];
  113. /* Quote: end of string */
  114. if (c == '\"')
  115. {
  116. if (tokens == NULL)
  117. {
  118. return 0;
  119. }
  120. token = jsmn_alloc_token(parser, tokens, num_tokens);
  121. if (token == NULL)
  122. {
  123. parser->pos = start;
  124. return JSMN_ERROR_NOMEM;
  125. }
  126. jsmn_fill_token(token, JSMN_STRING, start + 1, parser->pos);
  127. #ifdef JSMN_PARENT_LINKS
  128. token->parent = parser->toksuper;
  129. #endif
  130. return 0;
  131. }
  132. /* Backslash: Quoted symbol expected */
  133. if (c == '\\' && parser->pos + 1 < len)
  134. {
  135. int i;
  136. parser->pos++;
  137. switch (js[parser->pos])
  138. {
  139. /* Allowed escaped symbols */
  140. case '\"':
  141. case '/':
  142. case '\\':
  143. case 'b':
  144. case 'f':
  145. case 'r':
  146. case 'n':
  147. case 't':
  148. break;
  149. /* Allows escaped symbol \uXXXX */
  150. case 'u':
  151. parser->pos++;
  152. for (i = 0; i < 4 && parser->pos < len && js[parser->pos] != '\0'; i++)
  153. {
  154. /* If it isn't a hex character we have an error */
  155. if (!((js[parser->pos] >= 48 && js[parser->pos] <= 57) || /* 0-9 */
  156. (js[parser->pos] >= 65 && js[parser->pos] <= 70) || /* A-F */
  157. (js[parser->pos] >= 97 && js[parser->pos] <= 102)))
  158. { /* a-f */
  159. parser->pos = start;
  160. return JSMN_ERROR_INVAL;
  161. }
  162. parser->pos++;
  163. }
  164. parser->pos--;
  165. break;
  166. /* Unexpected symbol */
  167. default:
  168. parser->pos = start;
  169. return JSMN_ERROR_INVAL;
  170. }
  171. }
  172. }
  173. parser->pos = start;
  174. return JSMN_ERROR_PART;
  175. }
  176. /**
  177. * Create JSON parser over an array of tokens
  178. */
  179. void jsmn_init(jsmn_parser *parser)
  180. {
  181. parser->pos = 0;
  182. parser->toknext = 0;
  183. parser->toksuper = -1;
  184. }
  185. /**
  186. * Parse JSON string and fill tokens.
  187. */
  188. int jsmn_parse(jsmn_parser *parser, const char *js, const size_t len,
  189. jsmntok_t *tokens, const unsigned int num_tokens)
  190. {
  191. int r;
  192. int i;
  193. jsmntok_t *token;
  194. int count = parser->toknext;
  195. for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++)
  196. {
  197. char c;
  198. jsmntype_t type;
  199. c = js[parser->pos];
  200. switch (c)
  201. {
  202. case '{':
  203. case '[':
  204. count++;
  205. if (tokens == NULL)
  206. {
  207. break;
  208. }
  209. token = jsmn_alloc_token(parser, tokens, num_tokens);
  210. if (token == NULL)
  211. {
  212. return JSMN_ERROR_NOMEM;
  213. }
  214. if (parser->toksuper != -1)
  215. {
  216. jsmntok_t *t = &tokens[parser->toksuper];
  217. #ifdef JSMN_STRICT
  218. /* In strict mode an object or array can't become a key */
  219. if (t->type == JSMN_OBJECT)
  220. {
  221. return JSMN_ERROR_INVAL;
  222. }
  223. #endif
  224. t->size++;
  225. #ifdef JSMN_PARENT_LINKS
  226. token->parent = parser->toksuper;
  227. #endif
  228. }
  229. token->type = (c == '{' ? JSMN_OBJECT : JSMN_ARRAY);
  230. token->start = parser->pos;
  231. parser->toksuper = parser->toknext - 1;
  232. break;
  233. case '}':
  234. case ']':
  235. if (tokens == NULL)
  236. {
  237. break;
  238. }
  239. type = (c == '}' ? JSMN_OBJECT : JSMN_ARRAY);
  240. #ifdef JSMN_PARENT_LINKS
  241. if (parser->toknext < 1)
  242. {
  243. return JSMN_ERROR_INVAL;
  244. }
  245. token = &tokens[parser->toknext - 1];
  246. for (;;)
  247. {
  248. if (token->start != -1 && token->end == -1)
  249. {
  250. if (token->type != type)
  251. {
  252. return JSMN_ERROR_INVAL;
  253. }
  254. token->end = parser->pos + 1;
  255. parser->toksuper = token->parent;
  256. break;
  257. }
  258. if (token->parent == -1)
  259. {
  260. if (token->type != type || parser->toksuper == -1)
  261. {
  262. return JSMN_ERROR_INVAL;
  263. }
  264. break;
  265. }
  266. token = &tokens[token->parent];
  267. }
  268. #else
  269. for (i = parser->toknext - 1; i >= 0; i--)
  270. {
  271. token = &tokens[i];
  272. if (token->start != -1 && token->end == -1)
  273. {
  274. if (token->type != type)
  275. {
  276. return JSMN_ERROR_INVAL;
  277. }
  278. parser->toksuper = -1;
  279. token->end = parser->pos + 1;
  280. break;
  281. }
  282. }
  283. /* Error if unmatched closing bracket */
  284. if (i == -1)
  285. {
  286. return JSMN_ERROR_INVAL;
  287. }
  288. for (; i >= 0; i--)
  289. {
  290. token = &tokens[i];
  291. if (token->start != -1 && token->end == -1)
  292. {
  293. parser->toksuper = i;
  294. break;
  295. }
  296. }
  297. #endif
  298. break;
  299. case '\"':
  300. r = jsmn_parse_string(parser, js, len, tokens, num_tokens);
  301. if (r < 0)
  302. {
  303. return r;
  304. }
  305. count++;
  306. if (parser->toksuper != -1 && tokens != NULL)
  307. {
  308. tokens[parser->toksuper].size++;
  309. }
  310. break;
  311. case '\t':
  312. case '\r':
  313. case '\n':
  314. case ' ':
  315. break;
  316. case ':':
  317. parser->toksuper = parser->toknext - 1;
  318. break;
  319. case ',':
  320. if (tokens != NULL && parser->toksuper != -1 &&
  321. tokens[parser->toksuper].type != JSMN_ARRAY &&
  322. tokens[parser->toksuper].type != JSMN_OBJECT)
  323. {
  324. #ifdef JSMN_PARENT_LINKS
  325. parser->toksuper = tokens[parser->toksuper].parent;
  326. #else
  327. for (i = parser->toknext - 1; i >= 0; i--)
  328. {
  329. if (tokens[i].type == JSMN_ARRAY || tokens[i].type == JSMN_OBJECT)
  330. {
  331. if (tokens[i].start != -1 && tokens[i].end == -1)
  332. {
  333. parser->toksuper = i;
  334. break;
  335. }
  336. }
  337. }
  338. #endif
  339. }
  340. break;
  341. #ifdef JSMN_STRICT
  342. /* In strict mode primitives are: numbers and booleans */
  343. case '-':
  344. case '0':
  345. case '1':
  346. case '2':
  347. case '3':
  348. case '4':
  349. case '5':
  350. case '6':
  351. case '7':
  352. case '8':
  353. case '9':
  354. case 't':
  355. case 'f':
  356. case 'n':
  357. /* And they must not be keys of the object */
  358. if (tokens != NULL && parser->toksuper != -1)
  359. {
  360. const jsmntok_t *t = &tokens[parser->toksuper];
  361. if (t->type == JSMN_OBJECT ||
  362. (t->type == JSMN_STRING && t->size != 0))
  363. {
  364. return JSMN_ERROR_INVAL;
  365. }
  366. }
  367. #else
  368. /* In non-strict mode every unquoted value is a primitive */
  369. default:
  370. #endif
  371. r = jsmn_parse_primitive(parser, js, len, tokens, num_tokens);
  372. if (r < 0)
  373. {
  374. return r;
  375. }
  376. count++;
  377. if (parser->toksuper != -1 && tokens != NULL)
  378. {
  379. tokens[parser->toksuper].size++;
  380. }
  381. break;
  382. #ifdef JSMN_STRICT
  383. /* Unexpected char in strict mode */
  384. default:
  385. return JSMN_ERROR_INVAL;
  386. #endif
  387. }
  388. }
  389. if (tokens != NULL)
  390. {
  391. for (i = parser->toknext - 1; i >= 0; i--)
  392. {
  393. /* Unmatched opened object or array */
  394. if (tokens[i].start != -1 && tokens[i].end == -1)
  395. {
  396. return JSMN_ERROR_PART;
  397. }
  398. }
  399. }
  400. return count;
  401. }
  402. // Helper function to create a JSON object
  403. char *get_json(const char *key, const char *value)
  404. {
  405. int length = strlen(key) + strlen(value) + 8; // Calculate required length
  406. char *result = (char *)malloc(length * sizeof(char)); // Allocate memory
  407. if (result == NULL)
  408. {
  409. return NULL; // Handle memory allocation failure
  410. }
  411. snprintf(result, length, "{\"%s\":\"%s\"}", key, value);
  412. return result; // Caller is responsible for freeing this memory
  413. }
  414. // Helper function to compare JSON keys
  415. int jsoneq(const char *json, jsmntok_t *tok, const char *s)
  416. {
  417. if (tok->type == JSMN_STRING && (int)strlen(s) == tok->end - tok->start &&
  418. strncmp(json + tok->start, s, tok->end - tok->start) == 0)
  419. {
  420. return 0;
  421. }
  422. return -1;
  423. }
  424. // Return the value of the key in the JSON data
  425. char *get_json_value(char *key, const char *json_data)
  426. {
  427. // Parse the JSON feed
  428. if (json_data != NULL)
  429. {
  430. jsmn_parser parser;
  431. jsmn_init(&parser);
  432. uint32_t max_tokens = json_token_count(json_data);
  433. // Allocate tokens array on the heap
  434. jsmntok_t *tokens = malloc(sizeof(jsmntok_t) * max_tokens);
  435. if (tokens == NULL)
  436. {
  437. FURI_LOG_E("JSMM.H", "Failed to allocate memory for JSON tokens.");
  438. return NULL;
  439. }
  440. int ret = jsmn_parse(&parser, json_data, strlen(json_data), tokens, max_tokens);
  441. if (ret < 0)
  442. {
  443. // Handle parsing errors
  444. FURI_LOG_E("JSMM.H", "Failed to parse JSON: %d", ret);
  445. free(tokens);
  446. return NULL;
  447. }
  448. // Ensure that the root element is an object
  449. if (ret < 1 || tokens[0].type != JSMN_OBJECT)
  450. {
  451. FURI_LOG_E("JSMM.H", "Root element is not an object.");
  452. free(tokens);
  453. return NULL;
  454. }
  455. // Loop through the tokens to find the key
  456. for (int i = 1; i < ret; i++)
  457. {
  458. if (jsoneq(json_data, &tokens[i], key) == 0)
  459. {
  460. // We found the key. Now, return the associated value.
  461. int length = tokens[i + 1].end - tokens[i + 1].start;
  462. char *value = malloc(length + 1);
  463. if (value == NULL)
  464. {
  465. FURI_LOG_E("JSMM.H", "Failed to allocate memory for value.");
  466. free(tokens);
  467. return NULL;
  468. }
  469. strncpy(value, json_data + tokens[i + 1].start, length);
  470. value[length] = '\0'; // Null-terminate the string
  471. free(tokens); // Free the token array
  472. return value; // Return the extracted value
  473. }
  474. }
  475. // Free the token array if key was not found
  476. free(tokens);
  477. }
  478. else
  479. {
  480. FURI_LOG_E("JSMM.H", "JSON data is NULL");
  481. }
  482. char warning[128];
  483. snprintf(warning, sizeof(warning), "Failed to find the key \"%s\" in the JSON.", key);
  484. FURI_LOG_E("JSMM.H", warning);
  485. return NULL; // Return NULL if something goes wrong
  486. }
  487. // Helper function to skip a token and all its descendants.
  488. // Returns the index of the next token after skipping this one.
  489. // On error or out of bounds, returns -1.
  490. static int skip_token(const jsmntok_t *tokens, int start, int total)
  491. {
  492. if (start < 0 || start >= total)
  493. return -1;
  494. int i = start;
  495. if (tokens[i].type == JSMN_OBJECT)
  496. {
  497. // For an object: size is number of key-value pairs
  498. int pairs = tokens[i].size;
  499. i++; // move to first key-value pair
  500. for (int p = 0; p < pairs; p++)
  501. {
  502. // skip key (primitive/string)
  503. i++;
  504. if (i >= total)
  505. return -1;
  506. // skip value (which could be object/array and must be skipped recursively)
  507. i = skip_token(tokens, i, total);
  508. if (i == -1)
  509. return -1;
  510. }
  511. return i; // i is now just past the object
  512. }
  513. else if (tokens[i].type == JSMN_ARRAY)
  514. {
  515. // For an array: size is number of elements
  516. int elems = tokens[i].size;
  517. i++; // move to first element
  518. for (int e = 0; e < elems; e++)
  519. {
  520. i = skip_token(tokens, i, total);
  521. if (i == -1)
  522. return -1;
  523. }
  524. return i; // i is now just past the array
  525. }
  526. else
  527. {
  528. // Primitive or string token, just skip it
  529. return i + 1;
  530. }
  531. }
  532. // Revised get_json_array_value
  533. char *get_json_array_value(char *key, uint32_t index, const char *json_data)
  534. {
  535. // Always extract the full array each time from the original json_data
  536. char *array_str = get_json_value(key, json_data);
  537. if (array_str == NULL)
  538. {
  539. FURI_LOG_E("JSMM.H", "Failed to get array for key: %s", key);
  540. return NULL;
  541. }
  542. uint32_t max_tokens = json_token_count(array_str);
  543. jsmn_parser parser;
  544. jsmn_init(&parser);
  545. jsmntok_t *tokens = malloc(sizeof(jsmntok_t) * max_tokens);
  546. if (tokens == NULL)
  547. {
  548. FURI_LOG_E("JSMM.H", "Failed to allocate memory for JSON tokens.");
  549. free(array_str);
  550. return NULL;
  551. }
  552. int ret = jsmn_parse(&parser, array_str, strlen(array_str), tokens, max_tokens);
  553. if (ret < 0)
  554. {
  555. FURI_LOG_E("JSMM.H", "Failed to parse JSON array: %d", ret);
  556. free(tokens);
  557. free(array_str);
  558. return NULL;
  559. }
  560. if (ret < 1 || tokens[0].type != JSMN_ARRAY)
  561. {
  562. FURI_LOG_E("JSMM.H", "Value for key '%s' is not an array.", key);
  563. free(tokens);
  564. free(array_str);
  565. return NULL;
  566. }
  567. if (index >= (uint32_t)tokens[0].size)
  568. {
  569. FURI_LOG_E("JSMM.H", "Index %lu out of bounds for array with size %u.", index, tokens[0].size);
  570. free(tokens);
  571. free(array_str);
  572. return NULL;
  573. }
  574. // Find the index-th element: start from token[1], which is the first element
  575. int elem_token = 1;
  576. for (uint32_t i = 0; i < index; i++)
  577. {
  578. elem_token = skip_token(tokens, elem_token, ret);
  579. if (elem_token == -1 || elem_token >= ret)
  580. {
  581. FURI_LOG_E("JSMM.H", "Error skipping tokens to reach element %lu.", i);
  582. free(tokens);
  583. free(array_str);
  584. return NULL;
  585. }
  586. }
  587. // Now elem_token should point to the token of the requested element
  588. jsmntok_t element = tokens[elem_token];
  589. int length = element.end - element.start;
  590. char *value = malloc(length + 1);
  591. if (!value)
  592. {
  593. FURI_LOG_E("JSMM.H", "Failed to allocate memory for array element.");
  594. free(tokens);
  595. free(array_str);
  596. return NULL;
  597. }
  598. strncpy(value, array_str + element.start, length);
  599. value[length] = '\0';
  600. free(tokens);
  601. free(array_str);
  602. return value;
  603. }
  604. // Revised get_json_array_values function with correct token skipping
  605. char **get_json_array_values(char *key, char *json_data, int *num_values)
  606. {
  607. // Retrieve the array string for the given key
  608. char *array_str = get_json_value(key, json_data);
  609. if (array_str == NULL)
  610. {
  611. FURI_LOG_E("JSMM.H", "Failed to get array for key: %s", key);
  612. return NULL;
  613. }
  614. uint32_t max_tokens = json_token_count(array_str);
  615. // Initialize the JSON parser
  616. jsmn_parser parser;
  617. jsmn_init(&parser);
  618. // Allocate memory for JSON tokens
  619. jsmntok_t *tokens = malloc(sizeof(jsmntok_t) * max_tokens); // Allocate on the heap
  620. if (tokens == NULL)
  621. {
  622. FURI_LOG_E("JSMM.H", "Failed to allocate memory for JSON tokens.");
  623. free(array_str);
  624. return NULL;
  625. }
  626. // Parse the JSON array
  627. int ret = jsmn_parse(&parser, array_str, strlen(array_str), tokens, max_tokens);
  628. if (ret < 0)
  629. {
  630. FURI_LOG_E("JSMM.H", "Failed to parse JSON array: %d", ret);
  631. free(tokens);
  632. free(array_str);
  633. return NULL;
  634. }
  635. // Ensure the root element is an array
  636. if (tokens[0].type != JSMN_ARRAY)
  637. {
  638. FURI_LOG_E("JSMM.H", "Value for key '%s' is not an array.", key);
  639. free(tokens);
  640. free(array_str);
  641. return NULL;
  642. }
  643. // Allocate memory for the array of values (maximum possible)
  644. int array_size = tokens[0].size;
  645. char **values = malloc(array_size * sizeof(char *));
  646. if (values == NULL)
  647. {
  648. FURI_LOG_E("JSMM.H", "Failed to allocate memory for array of values.");
  649. free(tokens);
  650. free(array_str);
  651. return NULL;
  652. }
  653. int actual_num_values = 0;
  654. // Traverse the array and extract all object values
  655. int current_token = 1; // Start after the array token
  656. for (int i = 0; i < array_size; i++)
  657. {
  658. if (current_token >= ret)
  659. {
  660. FURI_LOG_E("JSMM.H", "Unexpected end of tokens while traversing array.");
  661. break;
  662. }
  663. jsmntok_t element = tokens[current_token];
  664. if (element.type != JSMN_OBJECT)
  665. {
  666. FURI_LOG_E("JSMM.H", "Array element %d is not an object, skipping.", i);
  667. // Skip this element
  668. current_token += 1;
  669. continue;
  670. }
  671. int length = element.end - element.start;
  672. // Allocate a new string for the value and copy the data
  673. char *value = malloc(length + 1);
  674. if (value == NULL)
  675. {
  676. FURI_LOG_E("JSMM.H", "Failed to allocate memory for array element.");
  677. for (int j = 0; j < actual_num_values; j++)
  678. {
  679. free(values[j]);
  680. }
  681. free(values);
  682. free(tokens);
  683. free(array_str);
  684. return NULL;
  685. }
  686. strncpy(value, array_str + element.start, length);
  687. value[length] = '\0'; // Null-terminate the string
  688. values[actual_num_values] = value;
  689. actual_num_values++;
  690. // Skip all tokens related to this object to avoid misparsing
  691. current_token += 1 + (2 * element.size); // Each key-value pair consumes two tokens
  692. }
  693. *num_values = actual_num_values;
  694. // Reallocate the values array to actual_num_values if necessary
  695. if (actual_num_values < array_size)
  696. {
  697. char **reduced_values = realloc(values, actual_num_values * sizeof(char *));
  698. if (reduced_values != NULL)
  699. {
  700. values = reduced_values;
  701. }
  702. // Free the remaining values
  703. for (int i = actual_num_values; i < array_size; i++)
  704. {
  705. free(values[i]);
  706. }
  707. }
  708. // Clean up
  709. free(tokens);
  710. free(array_str);
  711. return values;
  712. }
  713. int json_token_count(const char *json)
  714. {
  715. if (json == NULL)
  716. {
  717. return JSMN_ERROR_INVAL;
  718. }
  719. jsmn_parser parser;
  720. jsmn_init(&parser);
  721. // Pass NULL for tokens and 0 for num_tokens to get the token count only
  722. int ret = jsmn_parse(&parser, json, strlen(json), NULL, 0);
  723. return ret; // If ret >= 0, it represents the number of tokens needed.
  724. }