nmsg 1.1.2
nmsg_json.h
1#ifndef NMSG_JSON_H
2#define NMSG_JSON_H
3
4#include "strbuf.h"
5#include "libmy/my_alloc.h"
6
7static inline void
8num_to_str(int num, int size, char * ptr) {
9 int ndx = size - 1;
10
11 while (size > 0) {
12 int digit = num % 10;
13 ptr[ndx] = '0' + digit;
14 --ndx;
15 --size;
16 num /= 10;
17 }
18}
19
20static inline size_t
21vnum_to_str(uint64_t num, char *ptr) {
22 uint64_t tmp = num;
23 size_t ndx, left, ndigits = 0;
24
25 do {
26 ndigits++;
27 tmp /= 10;
28 } while (tmp != 0);
29
30 left = ndigits;
31 ndx = left - 1;
32 while(left > 0) {
33 int digit = num % 10;
34 ptr[ndx] = '0' + digit;
35 --ndx;
36 --left;
37 num = num/10;
38 }
39
40 ptr[ndigits] = '\0';
41
42 return ndigits;
43}
44
45static inline void
46declare_json_value(struct nmsg_strbuf *sb, const char *name, bool is_first) {
47 if (!is_first) {
48 nmsg_strbuf_append_str(sb, ",\"", 2);
49 } else {
50 nmsg_strbuf_append_str(sb, "\"", 1);
51 }
52
53 nmsg_strbuf_append_str(sb, name, strlen(name));
54 nmsg_strbuf_append_str(sb, "\":", 2);
55}
56
57static inline void
58append_json_value_string(struct nmsg_strbuf *sb, const char *val, size_t vlen) {
59 nmsg_strbuf_append_str(sb, "\"", 1);
60 nmsg_strbuf_append_str_json(sb, val, vlen);
61 nmsg_strbuf_append_str(sb, "\"", 1); // guaranteed success x 3
62}
63
64/* More performant variant for when we know data doesn't need to be escaped. */
65static inline void
66append_json_value_string_noescape(struct nmsg_strbuf *sb, const char *val, size_t vlen) {
67 nmsg_strbuf_append_str(sb, "\"", 1);
68 nmsg_strbuf_append_str(sb, val, vlen);
69 nmsg_strbuf_append_str(sb, "\"", 1); // guaranteed success x 3
70}
71
72static inline void
73append_json_value_int(struct nmsg_strbuf *sb, uint64_t val) {
74 char numbuf[32];
75 size_t nlen;
76
77 nlen = vnum_to_str(val, numbuf);
78 nmsg_strbuf_append_str(sb, numbuf, nlen); // guaranteed succes
79}
80
81static inline void
82append_json_value_bool(struct nmsg_strbuf *sb, bool val) {
83
84 if (val)
85 nmsg_strbuf_append_str(sb, "true", 4);
86 else
87 nmsg_strbuf_append_str(sb, "false", 5); // guaranteed success
88}
89
90static inline void
91append_json_value_double(struct nmsg_strbuf *sb, double val) {
92 char dubbuf[64], *endp;
93 size_t dlen;
94
95 dlen = snprintf(dubbuf, sizeof(dubbuf), "%.18f", val);
96 dubbuf[sizeof(dubbuf)-1] = 0;
97
98 /* Trim possible trailing numerical zero padding */
99 endp = dubbuf + dlen - 1;
100 while (*endp != '\0' && endp > dubbuf) {
101 if (*endp != '0' || *(endp-1) == '.')
102 break;
103 *endp-- = '\0';
104 dlen--;
105 }
106
107 nmsg_strbuf_append_str(sb, dubbuf, dlen); // guaranteed success
108}
109
110static inline void
111append_json_value_null(struct nmsg_strbuf *sb) {
112 nmsg_strbuf_append_str(sb, "null", 4); // guaranteed success
113}
114
115#endif /* NMSG_JSON_H */
String buffers.
nmsg_res nmsg_strbuf_append_str_json(struct nmsg_strbuf *sb, const char *str, size_t len)
Append to a string buffer.
nmsg_res nmsg_strbuf_append_str(struct nmsg_strbuf *sb, const char *str, size_t len)
Append to a string buffer.
String buffer.
Definition strbuf.h:28