5#include "libmy/my_alloc.h"
8num_to_str(
int num,
int size,
char * ptr) {
13 ptr[ndx] =
'0' + digit;
21vnum_to_str(uint64_t num,
char *ptr) {
23 size_t ndx, left, ndigits = 0;
34 ptr[ndx] =
'0' + digit;
46declare_json_value(
struct nmsg_strbuf *sb,
const char *name,
bool is_first) {
48 nmsg_strbuf_append_str(sb,
",\"", 2);
50 nmsg_strbuf_append_str(sb,
"\"", 1);
53 nmsg_strbuf_append_str(sb, name, strlen(name));
54 nmsg_strbuf_append_str(sb,
"\":", 2);
58append_json_value_string(
struct nmsg_strbuf *sb,
const char *val,
size_t vlen) {
59 nmsg_strbuf_append_str(sb,
"\"", 1);
64 nmsg_strbuf_append_str_json(sb, val, vlen);
65 nmsg_strbuf_append_str(sb,
"\"", 1);
70append_json_value_string_noescape(
struct nmsg_strbuf *sb,
const char *val,
size_t vlen) {
71 nmsg_strbuf_append_str(sb,
"\"", 1);
76 nmsg_strbuf_append_str(sb, val, vlen);
77 nmsg_strbuf_append_str(sb,
"\"", 1);
81append_json_value_int(
struct nmsg_strbuf *sb, uint64_t val) {
85 nlen = vnum_to_str(val, numbuf);
86 nmsg_strbuf_append_str(sb, numbuf, nlen);
90append_json_value_bool(
struct nmsg_strbuf *sb,
bool val) {
93 nmsg_strbuf_append_str(sb,
"true", 4);
95 nmsg_strbuf_append_str(sb,
"false", 5);
99append_json_value_double(
struct nmsg_strbuf *sb,
double val) {
100 char dubbuf[64], *endp;
103 dlen = snprintf(dubbuf,
sizeof(dubbuf),
"%.18f", val);
104 dubbuf[
sizeof(dubbuf)-1] = 0;
107 endp = dubbuf + dlen - 1;
108 while (*endp !=
'\0' && endp > dubbuf) {
109 if (*endp !=
'0' || *(endp-1) ==
'.')
115 nmsg_strbuf_append_str(sb, dubbuf, dlen);
119append_json_value_null(
struct nmsg_strbuf *sb) {
120 nmsg_strbuf_append_str(sb,
"null", 4);