Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
at v5.0-rc1 73 lines 2.8 kB view raw
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 * This program is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU General Public License 10 * as published by the Free Software Foundation; either version 11 * 2 of the License, or (at your option) any later version. 12 * 13 * Authors: Stephen Hemminger <stephen@networkplumber.org> 14 */ 15 16#ifndef _JSON_WRITER_H_ 17#define _JSON_WRITER_H_ 18 19#include <stdbool.h> 20#include <stdint.h> 21#include <stdarg.h> 22 23/* Opaque class structure */ 24typedef struct json_writer json_writer_t; 25 26/* Create a new JSON stream */ 27json_writer_t *jsonw_new(FILE *f); 28/* End output to JSON stream */ 29void jsonw_destroy(json_writer_t **self_p); 30 31/* Cause output to have pretty whitespace */ 32void jsonw_pretty(json_writer_t *self, bool on); 33 34/* Add property name */ 35void jsonw_name(json_writer_t *self, const char *name); 36 37/* Add value */ 38void jsonw_vprintf_enquote(json_writer_t *self, const char *fmt, va_list ap); 39void jsonw_printf(json_writer_t *self, const char *fmt, ...); 40void jsonw_string(json_writer_t *self, const char *value); 41void jsonw_bool(json_writer_t *self, bool value); 42void jsonw_float(json_writer_t *self, double number); 43void jsonw_float_fmt(json_writer_t *self, const char *fmt, double num); 44void jsonw_uint(json_writer_t *self, uint64_t number); 45void jsonw_hu(json_writer_t *self, unsigned short number); 46void jsonw_int(json_writer_t *self, int64_t number); 47void jsonw_null(json_writer_t *self); 48void jsonw_lluint(json_writer_t *self, unsigned long long int num); 49 50/* Useful Combinations of name and value */ 51void jsonw_string_field(json_writer_t *self, const char *prop, const char *val); 52void jsonw_bool_field(json_writer_t *self, const char *prop, bool value); 53void jsonw_float_field(json_writer_t *self, const char *prop, double num); 54void jsonw_uint_field(json_writer_t *self, const char *prop, uint64_t num); 55void jsonw_hu_field(json_writer_t *self, const char *prop, unsigned short num); 56void jsonw_int_field(json_writer_t *self, const char *prop, int64_t num); 57void jsonw_null_field(json_writer_t *self, const char *prop); 58void jsonw_lluint_field(json_writer_t *self, const char *prop, 59 unsigned long long int num); 60void jsonw_float_field_fmt(json_writer_t *self, const char *prop, 61 const char *fmt, double val); 62 63/* Collections */ 64void jsonw_start_object(json_writer_t *self); 65void jsonw_end_object(json_writer_t *self); 66 67void jsonw_start_array(json_writer_t *self); 68void jsonw_end_array(json_writer_t *self); 69 70/* Override default exception handling */ 71typedef void (jsonw_err_handler_fn)(const char *); 72 73#endif /* _JSON_WRITER_H_ */