example_jsmn.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * File : example_jsmn.c
  3. * This file is the example code of jsmn package.
  4. * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2018-06-09 never the first version
  23. */
  24. #include <rtthread.h>
  25. #include <finsh.h>
  26. #include <string.h>
  27. #include "jsmn.h"
  28. const char *JSON_STRING =
  29. "{\"file\":\"example_jsmn.c\", \
  30. \"description\":\"this is an example.\",\
  31. \"verson\":v0.1.0,\
  32. \"date\":2018.6.9}";
  33. static int fetch_key(const char *JSON_STRING, jsmntok_t *t, const char *str)
  34. {
  35. int length;
  36. length = strlen(str);
  37. if ((t->type == JSMN_STRING) && (length == (t->end - t->start)))
  38. {
  39. return strncmp(JSON_STRING + t->start, str, length);
  40. }
  41. else
  42. {
  43. return -1;
  44. }
  45. }
  46. int jsmn_exam(void)
  47. {
  48. int i;
  49. int res;
  50. jsmn_parser p;
  51. jsmntok_t t[128] = {0};
  52. jsmn_init(&p);
  53. res = jsmn_parse(&p, JSON_STRING, strlen(JSON_STRING), t, sizeof(t) / sizeof(t[0]));
  54. if (res < 0)
  55. {
  56. rt_kprintf("Failed to parse JSON: %d\n", res);
  57. return -1;
  58. }
  59. if (res < 1 || t[0].type != JSMN_OBJECT)
  60. {
  61. rt_kprintf("Object expected\n");
  62. return -1;
  63. }
  64. for (i = 1; i < res; i++)
  65. {
  66. if (fetch_key(JSON_STRING, &t[i], "file") == 0)
  67. {
  68. rt_kprintf("file: %.*s\n", t[i+1].end - t[i+1].start, JSON_STRING + t[i+1].start);
  69. i++;
  70. }
  71. else if (fetch_key(JSON_STRING, &t[i], "description") == 0)
  72. {
  73. rt_kprintf("description: %.*s\n", t[i+1].end - t[i+1].start, JSON_STRING + t[i+1].start);
  74. i++;
  75. }
  76. else if (fetch_key(JSON_STRING, &t[i], "verson") == 0)
  77. {
  78. rt_kprintf("verson: %.*s\n", t[i+1].end - t[i+1].start, JSON_STRING + t[i+1].start);
  79. i++;
  80. }
  81. else if (fetch_key(JSON_STRING, &t[i], "date") == 0)
  82. {
  83. rt_kprintf("date: %.*s\n", t[i+1].end - t[i+1].start, JSON_STRING + t[i+1].start);
  84. i++;
  85. }
  86. else
  87. {
  88. rt_kprintf("un: %.*s\n", t[i].end - t[i].start, JSON_STRING + t[i].start);
  89. }
  90. }
  91. return 0;
  92. }
  93. MSH_CMD_EXPORT(jsmn_exam, jsmn example);