json_writer.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
  2. /*
  3. * Simple streaming JSON writer
  4. *
  5. * This takes care of the annoying bits of JSON syntax like the commas
  6. * after elements
  7. *
  8. * Authors: Stephen Hemminger <stephen@networkplumber.org>
  9. */
  10. #ifndef _JSON_WRITER_H_
  11. #define _JSON_WRITER_H_
  12. #include <stdbool.h>
  13. #include <stdint.h>
  14. #include <stdarg.h>
  15. #include <stdio.h>
  16. #include <linux/compiler.h>
  17. /* Opaque class structure */
  18. typedef struct json_writer json_writer_t;
  19. /* Create a new JSON stream */
  20. json_writer_t *jsonw_new(FILE *f);
  21. /* End output to JSON stream */
  22. void jsonw_destroy(json_writer_t **self_p);
  23. /* Cause output to have pretty whitespace */
  24. void jsonw_pretty(json_writer_t *self, bool on);
  25. /* Reset separator to create new JSON */
  26. void jsonw_reset(json_writer_t *self);
  27. /* Add property name */
  28. void jsonw_name(json_writer_t *self, const char *name);
  29. /* Add value */
  30. void __printf(2, 0) jsonw_vprintf_enquote(json_writer_t *self, const char *fmt,
  31. va_list ap);
  32. void __printf(2, 3) jsonw_printf(json_writer_t *self, const char *fmt, ...);
  33. void jsonw_string(json_writer_t *self, const char *value);
  34. void jsonw_bool(json_writer_t *self, bool value);
  35. void jsonw_float(json_writer_t *self, double number);
  36. void jsonw_float_fmt(json_writer_t *self, const char *fmt, double num);
  37. void jsonw_uint(json_writer_t *self, uint64_t number);
  38. void jsonw_hu(json_writer_t *self, unsigned short number);
  39. void jsonw_int(json_writer_t *self, int64_t number);
  40. void jsonw_null(json_writer_t *self);
  41. void jsonw_lluint(json_writer_t *self, unsigned long long int num);
  42. /* Useful Combinations of name and value */
  43. void jsonw_string_field(json_writer_t *self, const char *prop, const char *val);
  44. void jsonw_bool_field(json_writer_t *self, const char *prop, bool value);
  45. void jsonw_float_field(json_writer_t *self, const char *prop, double num);
  46. void jsonw_uint_field(json_writer_t *self, const char *prop, uint64_t num);
  47. void jsonw_hu_field(json_writer_t *self, const char *prop, unsigned short num);
  48. void jsonw_int_field(json_writer_t *self, const char *prop, int64_t num);
  49. void jsonw_null_field(json_writer_t *self, const char *prop);
  50. void jsonw_lluint_field(json_writer_t *self, const char *prop,
  51. unsigned long long int num);
  52. void jsonw_float_field_fmt(json_writer_t *self, const char *prop,
  53. const char *fmt, double val);
  54. /* Collections */
  55. void jsonw_start_object(json_writer_t *self);
  56. void jsonw_end_object(json_writer_t *self);
  57. void jsonw_start_array(json_writer_t *self);
  58. void jsonw_end_array(json_writer_t *self);
  59. /* Override default exception handling */
  60. typedef void (jsonw_err_handler_fn)(const char *);
  61. #endif /* _JSON_WRITER_H_ */