jsmn.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2010 Serge Zaitsev
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * [License text continues...]
  14. */
  15. #ifndef JSMN_H
  16. #define JSMN_H
  17. #include <stddef.h>
  18. #ifdef __cplusplus
  19. extern "C"
  20. {
  21. #endif
  22. #ifdef JSMN_STATIC
  23. #define JSMN_API static
  24. #else
  25. #define JSMN_API extern
  26. #endif
  27. /**
  28. * JSON type identifier. Basic types are:
  29. * o Object
  30. * o Array
  31. * o String
  32. * o Other primitive: number, boolean (true/false) or null
  33. */
  34. typedef enum
  35. {
  36. JSMN_UNDEFINED = 0,
  37. JSMN_OBJECT = 1 << 0,
  38. JSMN_ARRAY = 1 << 1,
  39. JSMN_STRING = 1 << 2,
  40. JSMN_PRIMITIVE = 1 << 3
  41. } jsmntype_t;
  42. enum jsmnerr
  43. {
  44. /* Not enough tokens were provided */
  45. JSMN_ERROR_NOMEM = -1,
  46. /* Invalid character inside JSON string */
  47. JSMN_ERROR_INVAL = -2,
  48. /* The string is not a full JSON packet, more bytes expected */
  49. JSMN_ERROR_PART = -3
  50. };
  51. /**
  52. * JSON token description.
  53. * type type (object, array, string etc.)
  54. * start start position in JSON data string
  55. * end end position in JSON data string
  56. */
  57. typedef struct
  58. {
  59. jsmntype_t type;
  60. int start;
  61. int end;
  62. int size;
  63. #ifdef JSMN_PARENT_LINKS
  64. int parent;
  65. #endif
  66. } jsmntok_t;
  67. /**
  68. * JSON parser. Contains an array of token blocks available. Also stores
  69. * the string being parsed now and current position in that string.
  70. */
  71. typedef struct
  72. {
  73. unsigned int pos; /* offset in the JSON string */
  74. unsigned int toknext; /* next token to allocate */
  75. int toksuper; /* superior token node, e.g. parent object or array */
  76. } jsmn_parser;
  77. /**
  78. * Create JSON parser over an array of tokens
  79. */
  80. JSMN_API void jsmn_init(jsmn_parser *parser);
  81. /**
  82. * Run JSON parser. It parses a JSON data string into and array of tokens, each
  83. * describing a single JSON object.
  84. */
  85. JSMN_API int jsmn_parse(jsmn_parser *parser, const char *js, const size_t len,
  86. jsmntok_t *tokens, const unsigned int num_tokens);
  87. #ifndef JSMN_HEADER
  88. /* Implementation has been moved to jsmn.c */
  89. #endif /* JSMN_HEADER */
  90. #ifdef __cplusplus
  91. }
  92. #endif
  93. #endif /* JSMN_H */
  94. /* Custom Helper Functions */
  95. #ifndef JB_JSMN_EDIT
  96. #define JB_JSMN_EDIT
  97. /* Added in by JBlanked on 2024-10-16 for use in Flipper Zero SDK*/
  98. #include <string.h>
  99. #include <stdint.h>
  100. #include <stdlib.h>
  101. #include <stdio.h>
  102. #include <furi.h>
  103. // Helper function to create a JSON object
  104. char *jsmn(const char *key, const char *value);
  105. // Helper function to compare JSON keys
  106. int jsoneq(const char *json, jsmntok_t *tok, const char *s);
  107. // Return the value of the key in the JSON data
  108. char *get_json_value(char *key, char *json_data, uint32_t max_tokens);
  109. // Revised get_json_array_value function
  110. char *get_json_array_value(char *key, uint32_t index, char *json_data, uint32_t max_tokens);
  111. // Revised get_json_array_values function with correct token skipping
  112. char **get_json_array_values(char *key, char *json_data, uint32_t max_tokens, int *num_values);
  113. #endif /* JB_JSMN_EDIT */