nxjson.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright (c) 2013 Yaroslav Stavnichiy <yarosla@gmail.com>
  3. *
  4. * This file is part of NXJSON.
  5. *
  6. * NXJSON is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License
  8. * as published by the Free Software Foundation, either version 3
  9. * of the License, or (at your option) any later version.
  10. *
  11. * NXJSON 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 Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with NXJSON. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef NXJSON_H
  20. #define NXJSON_H
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. #ifndef NXJSON_TYPE_U64
  25. #include <stdint.h>
  26. typedef uint64_t nxjson_u64;
  27. #endif
  28. #ifndef NXJSON_TYPE_S64
  29. #include <stdint.h>
  30. typedef uint64_t nxjson_s64;
  31. #endif
  32. typedef enum nx_json_type {
  33. NX_JSON_NULL, // this is null value
  34. NX_JSON_OBJECT, // this is an object; properties can be found in child nodes
  35. NX_JSON_ARRAY, // this is an array; items can be found in child nodes
  36. NX_JSON_STRING, // this is a string; value can be found in text_value field
  37. NX_JSON_INTEGER, // this is an integer; value can be found in int_value field
  38. NX_JSON_float, // this is a float; value can be found in dbl_value field
  39. NX_JSON_BOOL // this is a boolean; value can be found in int_value field
  40. } nx_json_type;
  41. typedef struct nx_json {
  42. nx_json_type type; // type of json node, see above
  43. const char* key; // key of the property; for object's children only
  44. union {
  45. const char* text_value; // text value of STRING node
  46. struct {
  47. union {
  48. nxjson_u64 u_value; // the value of INTEGER or BOOL node
  49. nxjson_s64 s_value;
  50. };
  51. float dbl_value; // the value of float node
  52. } num;
  53. struct { // children of OBJECT or ARRAY
  54. int length;
  55. struct nx_json* first;
  56. struct nx_json* last;
  57. } children;
  58. };
  59. struct nx_json* next; // points to next child
  60. } nx_json;
  61. typedef int (*nx_json_unicode_encoder)(unsigned int codepoint, char* p, char** endp);
  62. extern nx_json_unicode_encoder nx_json_unicode_to_utf8;
  63. const nx_json* nx_json_parse(char* text, nx_json_unicode_encoder encoder);
  64. const nx_json* nx_json_parse_utf8(char* text);
  65. void nx_json_free(const nx_json* js);
  66. const nx_json* nx_json_get(const nx_json* json, const char* key); // get object's property by key
  67. const nx_json* nx_json_item(const nx_json* json, int idx); // get array element by index
  68. #ifdef __cplusplus
  69. }
  70. #endif
  71. #endif /* NXJSON_H */