162306a36Sopenharmony_ci/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */ 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Simple streaming JSON writer 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * This takes care of the annoying bits of JSON syntax like the commas 662306a36Sopenharmony_ci * after elements 762306a36Sopenharmony_ci * 862306a36Sopenharmony_ci * Authors: Stephen Hemminger <stephen@networkplumber.org> 962306a36Sopenharmony_ci */ 1062306a36Sopenharmony_ci 1162306a36Sopenharmony_ci#ifndef _JSON_WRITER_H_ 1262306a36Sopenharmony_ci#define _JSON_WRITER_H_ 1362306a36Sopenharmony_ci 1462306a36Sopenharmony_ci#include <stdbool.h> 1562306a36Sopenharmony_ci#include <stdint.h> 1662306a36Sopenharmony_ci#include <stdarg.h> 1762306a36Sopenharmony_ci#include <stdio.h> 1862306a36Sopenharmony_ci#include <linux/compiler.h> 1962306a36Sopenharmony_ci 2062306a36Sopenharmony_ci/* Opaque class structure */ 2162306a36Sopenharmony_citypedef struct json_writer json_writer_t; 2262306a36Sopenharmony_ci 2362306a36Sopenharmony_ci/* Create a new JSON stream */ 2462306a36Sopenharmony_cijson_writer_t *jsonw_new(FILE *f); 2562306a36Sopenharmony_ci/* End output to JSON stream */ 2662306a36Sopenharmony_civoid jsonw_destroy(json_writer_t **self_p); 2762306a36Sopenharmony_ci 2862306a36Sopenharmony_ci/* Cause output to have pretty whitespace */ 2962306a36Sopenharmony_civoid jsonw_pretty(json_writer_t *self, bool on); 3062306a36Sopenharmony_ci 3162306a36Sopenharmony_ci/* Reset separator to create new JSON */ 3262306a36Sopenharmony_civoid jsonw_reset(json_writer_t *self); 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_ci/* Add property name */ 3562306a36Sopenharmony_civoid jsonw_name(json_writer_t *self, const char *name); 3662306a36Sopenharmony_ci 3762306a36Sopenharmony_ci/* Add value */ 3862306a36Sopenharmony_civoid __printf(2, 0) jsonw_vprintf_enquote(json_writer_t *self, const char *fmt, 3962306a36Sopenharmony_ci va_list ap); 4062306a36Sopenharmony_civoid __printf(2, 3) jsonw_printf(json_writer_t *self, const char *fmt, ...); 4162306a36Sopenharmony_civoid jsonw_string(json_writer_t *self, const char *value); 4262306a36Sopenharmony_civoid jsonw_bool(json_writer_t *self, bool value); 4362306a36Sopenharmony_civoid jsonw_float(json_writer_t *self, double number); 4462306a36Sopenharmony_civoid jsonw_float_fmt(json_writer_t *self, const char *fmt, double num); 4562306a36Sopenharmony_civoid jsonw_uint(json_writer_t *self, uint64_t number); 4662306a36Sopenharmony_civoid jsonw_hu(json_writer_t *self, unsigned short number); 4762306a36Sopenharmony_civoid jsonw_int(json_writer_t *self, int64_t number); 4862306a36Sopenharmony_civoid jsonw_null(json_writer_t *self); 4962306a36Sopenharmony_civoid jsonw_lluint(json_writer_t *self, unsigned long long int num); 5062306a36Sopenharmony_ci 5162306a36Sopenharmony_ci/* Useful Combinations of name and value */ 5262306a36Sopenharmony_civoid jsonw_string_field(json_writer_t *self, const char *prop, const char *val); 5362306a36Sopenharmony_civoid jsonw_bool_field(json_writer_t *self, const char *prop, bool value); 5462306a36Sopenharmony_civoid jsonw_float_field(json_writer_t *self, const char *prop, double num); 5562306a36Sopenharmony_civoid jsonw_uint_field(json_writer_t *self, const char *prop, uint64_t num); 5662306a36Sopenharmony_civoid jsonw_hu_field(json_writer_t *self, const char *prop, unsigned short num); 5762306a36Sopenharmony_civoid jsonw_int_field(json_writer_t *self, const char *prop, int64_t num); 5862306a36Sopenharmony_civoid jsonw_null_field(json_writer_t *self, const char *prop); 5962306a36Sopenharmony_civoid jsonw_lluint_field(json_writer_t *self, const char *prop, 6062306a36Sopenharmony_ci unsigned long long int num); 6162306a36Sopenharmony_civoid jsonw_float_field_fmt(json_writer_t *self, const char *prop, 6262306a36Sopenharmony_ci const char *fmt, double val); 6362306a36Sopenharmony_ci 6462306a36Sopenharmony_ci/* Collections */ 6562306a36Sopenharmony_civoid jsonw_start_object(json_writer_t *self); 6662306a36Sopenharmony_civoid jsonw_end_object(json_writer_t *self); 6762306a36Sopenharmony_ci 6862306a36Sopenharmony_civoid jsonw_start_array(json_writer_t *self); 6962306a36Sopenharmony_civoid jsonw_end_array(json_writer_t *self); 7062306a36Sopenharmony_ci 7162306a36Sopenharmony_ci/* Override default exception handling */ 7262306a36Sopenharmony_citypedef void (jsonw_err_handler_fn)(const char *); 7362306a36Sopenharmony_ci 7462306a36Sopenharmony_ci#endif /* _JSON_WRITER_H_ */ 75