jsmn.h 3.3 KB

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