json.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2019 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef GRUB_JSON_JSON_H
  19. #define GRUB_JSON_JSON_H 1
  20. #include <grub/types.h>
  21. enum grub_json_type
  22. {
  23. /* Unordered collection of key-value pairs. */
  24. GRUB_JSON_OBJECT,
  25. /* Ordered list of zero or more values. */
  26. GRUB_JSON_ARRAY,
  27. /* Zero or more Unicode characters. */
  28. GRUB_JSON_STRING,
  29. /* Number, boolean or empty value. */
  30. GRUB_JSON_PRIMITIVE,
  31. /* Invalid token. */
  32. GRUB_JSON_UNDEFINED,
  33. };
  34. typedef enum grub_json_type grub_json_type_t;
  35. /* Forward-declaration to avoid including jsmn.h. */
  36. struct jsmntok;
  37. struct grub_json
  38. {
  39. struct jsmntok *tokens;
  40. char *string;
  41. grub_size_t idx;
  42. };
  43. typedef struct grub_json grub_json_t;
  44. /*
  45. * Parse a JSON-encoded string. Note that the string passed to
  46. * this function will get modified on subsequent calls to
  47. * grub_json_get*(). Returns the root object of the parsed JSON
  48. * object, which needs to be free'd via grub_json_free(). Callers
  49. * must ensure that the string outlives the returned root object,
  50. * and that child objects must not be used after the root object
  51. * has been free'd.
  52. */
  53. extern grub_err_t EXPORT_FUNC(grub_json_parse) (grub_json_t **out,
  54. char *string,
  55. grub_size_t string_len);
  56. /*
  57. * Free the structure and its contents. The string passed to
  58. * grub_json_parse() will not be free'd.
  59. */
  60. extern void EXPORT_FUNC(grub_json_free) (grub_json_t *json);
  61. /*
  62. * Get the child count of a valid grub_json_t instance. Children
  63. * are present for arrays, objects (dicts) and keys of a dict.
  64. */
  65. extern grub_err_t EXPORT_FUNC(grub_json_getsize) (grub_size_t *out,
  66. const grub_json_t *json);
  67. /* Get the type of a valid grub_json_t instance. */
  68. extern grub_err_t EXPORT_FUNC(grub_json_gettype) (grub_json_type_t *out,
  69. const grub_json_t *json);
  70. /*
  71. * Get n'th child of a valid object, array or key. Will return an
  72. * error if no such child exists. The result does not need to be
  73. * free'd.
  74. */
  75. extern grub_err_t EXPORT_FUNC(grub_json_getchild) (grub_json_t *out,
  76. const grub_json_t *parent,
  77. grub_size_t n);
  78. /*
  79. * Get value of key from a valid grub_json_t instance. The result
  80. * does not need to be free'd.
  81. */
  82. extern grub_err_t EXPORT_FUNC(grub_json_getvalue) (grub_json_t *out,
  83. const grub_json_t *parent,
  84. const char *key);
  85. /*
  86. * Get the string representation of a valid grub_json_t instance.
  87. * If a key is given and parent is a JSON object, this function
  88. * will return the string value of a child mapping to the key.
  89. * If no key is given, it will return the string value of the
  90. * parent itself.
  91. */
  92. extern grub_err_t EXPORT_FUNC(grub_json_getstring) (const char **out,
  93. const grub_json_t *parent,
  94. const char *key);
  95. /*
  96. * Get the uint64 representation of a valid grub_json_t instance.
  97. * Returns an error if the value pointed to by `parent` cannot be
  98. * converted to an uint64. See grub_json_getstring() for details
  99. * on the key parameter.
  100. */
  101. extern grub_err_t EXPORT_FUNC(grub_json_getuint64) (grub_uint64_t *out,
  102. const grub_json_t *parent,
  103. const char *key);
  104. /*
  105. * Get the int64 representation of a valid grub_json_t instance.
  106. * Returns an error if the value pointed to by `parent` cannot be
  107. * converted to an int64. See grub_json_getstring() for
  108. * details on the key parameter.
  109. */
  110. extern grub_err_t EXPORT_FUNC(grub_json_getint64) (grub_int64_t *out,
  111. const grub_json_t *parent,
  112. const char *key);
  113. /*
  114. * Unescape escaped characters and Unicode sequences in the
  115. * given JSON-encoded string. Returns a newly allocated string
  116. * passed back via the `out` parameter that has a length of
  117. * `*outlen`.
  118. *
  119. * See https://datatracker.ietf.org/doc/html/rfc8259#section-7 for more
  120. * information on escaping in JSON.
  121. */
  122. extern grub_err_t EXPORT_FUNC(grub_json_unescape) (char **out, grub_size_t *outlen,
  123. const char *in, grub_size_t inlen);
  124. #endif