jsmn.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  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 *jsmn(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, uint32_t max_tokens)
  426. {
  427. // Parse the JSON feed
  428. if (json_data != NULL)
  429. {
  430. jsmn_parser parser;
  431. jsmn_init(&parser);
  432. // Allocate tokens array on the heap
  433. jsmntok_t *tokens = malloc(sizeof(jsmntok_t) * max_tokens);
  434. if (tokens == NULL)
  435. {
  436. FURI_LOG_E("JSMM.H", "Failed to allocate memory for JSON tokens.");
  437. return NULL;
  438. }
  439. int ret = jsmn_parse(&parser, json_data, strlen(json_data), tokens, max_tokens);
  440. if (ret < 0)
  441. {
  442. // Handle parsing errors
  443. FURI_LOG_E("JSMM.H", "Failed to parse JSON: %d", ret);
  444. free(tokens);
  445. return NULL;
  446. }
  447. // Ensure that the root element is an object
  448. if (ret < 1 || tokens[0].type != JSMN_OBJECT)
  449. {
  450. FURI_LOG_E("JSMM.H", "Root element is not an object.");
  451. free(tokens);
  452. return NULL;
  453. }
  454. // Loop through the tokens to find the key
  455. for (int i = 1; i < ret; i++)
  456. {
  457. if (jsoneq(json_data, &tokens[i], key) == 0)
  458. {
  459. // We found the key. Now, return the associated value.
  460. int length = tokens[i + 1].end - tokens[i + 1].start;
  461. char *value = malloc(length + 1);
  462. if (value == NULL)
  463. {
  464. FURI_LOG_E("JSMM.H", "Failed to allocate memory for value.");
  465. free(tokens);
  466. return NULL;
  467. }
  468. strncpy(value, json_data + tokens[i + 1].start, length);
  469. value[length] = '\0'; // Null-terminate the string
  470. free(tokens); // Free the token array
  471. return value; // Return the extracted value
  472. }
  473. }
  474. // Free the token array if key was not found
  475. free(tokens);
  476. }
  477. else
  478. {
  479. FURI_LOG_E("JSMM.H", "JSON data is NULL");
  480. }
  481. FURI_LOG_E("JSMM.H", "Failed to find the key in the JSON.");
  482. return NULL; // Return NULL if something goes wrong
  483. }
  484. // Helper function to skip a token and all its descendants.
  485. // Returns the index of the next token after skipping this one.
  486. // On error or out of bounds, returns -1.
  487. static int skip_token(const jsmntok_t *tokens, int start, int total)
  488. {
  489. if (start < 0 || start >= total)
  490. return -1;
  491. int i = start;
  492. if (tokens[i].type == JSMN_OBJECT)
  493. {
  494. // For an object: size is number of key-value pairs
  495. int pairs = tokens[i].size;
  496. i++; // move to first key-value pair
  497. for (int p = 0; p < pairs; p++)
  498. {
  499. // skip key (primitive/string)
  500. i++;
  501. if (i >= total)
  502. return -1;
  503. // skip value (which could be object/array and must be skipped recursively)
  504. i = skip_token(tokens, i, total);
  505. if (i == -1)
  506. return -1;
  507. }
  508. return i; // i is now just past the object
  509. }
  510. else if (tokens[i].type == JSMN_ARRAY)
  511. {
  512. // For an array: size is number of elements
  513. int elems = tokens[i].size;
  514. i++; // move to first element
  515. for (int e = 0; e < elems; e++)
  516. {
  517. i = skip_token(tokens, i, total);
  518. if (i == -1)
  519. return -1;
  520. }
  521. return i; // i is now just past the array
  522. }
  523. else
  524. {
  525. // Primitive or string token, just skip it
  526. return i + 1;
  527. }
  528. }
  529. // Revised get_json_array_value
  530. char *get_json_array_value(char *key, uint32_t index, const char *json_data, uint32_t max_tokens)
  531. {
  532. // Always extract the full array each time from the original json_data
  533. char *array_str = get_json_value(key, json_data, max_tokens);
  534. if (array_str == NULL)
  535. {
  536. FURI_LOG_E("JSMM.H", "Failed to get array for key: %s", key);
  537. return NULL;
  538. }
  539. jsmn_parser parser;
  540. jsmn_init(&parser);
  541. jsmntok_t *tokens = malloc(sizeof(jsmntok_t) * max_tokens);
  542. if (tokens == NULL)
  543. {
  544. FURI_LOG_E("JSMM.H", "Failed to allocate memory for JSON tokens.");
  545. free(array_str);
  546. return NULL;
  547. }
  548. int ret = jsmn_parse(&parser, array_str, strlen(array_str), tokens, max_tokens);
  549. if (ret < 0)
  550. {
  551. FURI_LOG_E("JSMM.H", "Failed to parse JSON array: %d", ret);
  552. free(tokens);
  553. free(array_str);
  554. return NULL;
  555. }
  556. if (ret < 1 || tokens[0].type != JSMN_ARRAY)
  557. {
  558. FURI_LOG_E("JSMM.H", "Value for key '%s' is not an array.", key);
  559. free(tokens);
  560. free(array_str);
  561. return NULL;
  562. }
  563. if (index >= (uint32_t)tokens[0].size)
  564. {
  565. FURI_LOG_E("JSMM.H", "Index %lu out of bounds for array with size %u.", index, tokens[0].size);
  566. free(tokens);
  567. free(array_str);
  568. return NULL;
  569. }
  570. // Find the index-th element: start from token[1], which is the first element
  571. int elem_token = 1;
  572. for (uint32_t i = 0; i < index; i++)
  573. {
  574. elem_token = skip_token(tokens, elem_token, ret);
  575. if (elem_token == -1 || elem_token >= ret)
  576. {
  577. FURI_LOG_E("JSMM.H", "Error skipping tokens to reach element %lu.", i);
  578. free(tokens);
  579. free(array_str);
  580. return NULL;
  581. }
  582. }
  583. // Now elem_token should point to the token of the requested element
  584. jsmntok_t element = tokens[elem_token];
  585. int length = element.end - element.start;
  586. char *value = malloc(length + 1);
  587. if (!value)
  588. {
  589. FURI_LOG_E("JSMM.H", "Failed to allocate memory for array element.");
  590. free(tokens);
  591. free(array_str);
  592. return NULL;
  593. }
  594. strncpy(value, array_str + element.start, length);
  595. value[length] = '\0';
  596. free(tokens);
  597. free(array_str);
  598. return value;
  599. }
  600. // Revised get_json_array_values function with correct token skipping
  601. char **get_json_array_values(char *key, char *json_data, uint32_t max_tokens, int *num_values)
  602. {
  603. // Retrieve the array string for the given key
  604. char *array_str = get_json_value(key, json_data, max_tokens);
  605. if (array_str == NULL)
  606. {
  607. FURI_LOG_E("JSMM.H", "Failed to get array for key: %s", key);
  608. return NULL;
  609. }
  610. // Initialize the JSON parser
  611. jsmn_parser parser;
  612. jsmn_init(&parser);
  613. // Allocate memory for JSON tokens
  614. jsmntok_t *tokens = malloc(sizeof(jsmntok_t) * max_tokens); // Allocate on the heap
  615. if (tokens == NULL)
  616. {
  617. FURI_LOG_E("JSMM.H", "Failed to allocate memory for JSON tokens.");
  618. free(array_str);
  619. return NULL;
  620. }
  621. // Parse the JSON array
  622. int ret = jsmn_parse(&parser, array_str, strlen(array_str), tokens, max_tokens);
  623. if (ret < 0)
  624. {
  625. FURI_LOG_E("JSMM.H", "Failed to parse JSON array: %d", ret);
  626. free(tokens);
  627. free(array_str);
  628. return NULL;
  629. }
  630. // Ensure the root element is an array
  631. if (tokens[0].type != JSMN_ARRAY)
  632. {
  633. FURI_LOG_E("JSMM.H", "Value for key '%s' is not an array.", key);
  634. free(tokens);
  635. free(array_str);
  636. return NULL;
  637. }
  638. // Allocate memory for the array of values (maximum possible)
  639. int array_size = tokens[0].size;
  640. char **values = malloc(array_size * sizeof(char *));
  641. if (values == NULL)
  642. {
  643. FURI_LOG_E("JSMM.H", "Failed to allocate memory for array of values.");
  644. free(tokens);
  645. free(array_str);
  646. return NULL;
  647. }
  648. int actual_num_values = 0;
  649. // Traverse the array and extract all object values
  650. int current_token = 1; // Start after the array token
  651. for (int i = 0; i < array_size; i++)
  652. {
  653. if (current_token >= ret)
  654. {
  655. FURI_LOG_E("JSMM.H", "Unexpected end of tokens while traversing array.");
  656. break;
  657. }
  658. jsmntok_t element = tokens[current_token];
  659. if (element.type != JSMN_OBJECT)
  660. {
  661. FURI_LOG_E("JSMM.H", "Array element %d is not an object, skipping.", i);
  662. // Skip this element
  663. current_token += 1;
  664. continue;
  665. }
  666. int length = element.end - element.start;
  667. // Allocate a new string for the value and copy the data
  668. char *value = malloc(length + 1);
  669. if (value == NULL)
  670. {
  671. FURI_LOG_E("JSMM.H", "Failed to allocate memory for array element.");
  672. for (int j = 0; j < actual_num_values; j++)
  673. {
  674. free(values[j]);
  675. }
  676. free(values);
  677. free(tokens);
  678. free(array_str);
  679. return NULL;
  680. }
  681. strncpy(value, array_str + element.start, length);
  682. value[length] = '\0'; // Null-terminate the string
  683. values[actual_num_values] = value;
  684. actual_num_values++;
  685. // Skip all tokens related to this object to avoid misparsing
  686. current_token += 1 + (2 * element.size); // Each key-value pair consumes two tokens
  687. }
  688. *num_values = actual_num_values;
  689. // Reallocate the values array to actual_num_values if necessary
  690. if (actual_num_values < array_size)
  691. {
  692. char **reduced_values = realloc(values, actual_num_values * sizeof(char *));
  693. if (reduced_values != NULL)
  694. {
  695. values = reduced_values;
  696. }
  697. // Free the remaining values
  698. for (int i = actual_num_values; i < array_size; i++)
  699. {
  700. free(values[i]);
  701. }
  702. }
  703. // Clean up
  704. free(tokens);
  705. free(array_str);
  706. return values;
  707. }
  708. int json_token_count(const char *json)
  709. {
  710. if (json == NULL)
  711. {
  712. return JSMN_ERROR_INVAL;
  713. }
  714. jsmn_parser parser;
  715. jsmn_init(&parser);
  716. // Pass NULL for tokens and 0 for num_tokens to get the token count only
  717. int ret = jsmn_parse(&parser, json, strlen(json), NULL, 0);
  718. return ret; // If ret >= 0, it represents the number of tokens needed.
  719. }