void ul_jsonwrt_indent(struct ul_jsonwrt *fmt);
void ul_jsonwrt_open(struct ul_jsonwrt *fmt, const char *name, int type);
void ul_jsonwrt_close(struct ul_jsonwrt *fmt, int type);
+void ul_jsonwrt_empty(struct ul_jsonwrt *fmt, const char *name, int type);
void ul_jsonwrt_flush(struct ul_jsonwrt *fmt);
#define ul_jsonwrt_root_open(_f) ul_jsonwrt_open(_f, NULL, UL_JSON_OBJECT)
#define ul_jsonwrt_array_open(_f, _n) ul_jsonwrt_open(_f, _n, UL_JSON_ARRAY)
#define ul_jsonwrt_array_close(_f) ul_jsonwrt_close(_f, UL_JSON_ARRAY)
+#define ul_jsonwrt_array_empty(_f, _n) ul_jsonwrt_empty(_f, _n, UL_JSON_ARRAY)
#define ul_jsonwrt_object_open(_f, _n) ul_jsonwrt_open(_f, _n, UL_JSON_OBJECT)
#define ul_jsonwrt_object_close(_f) ul_jsonwrt_close(_f, UL_JSON_OBJECT)
+#define ul_jsonwrt_object_empty(_f, _n) ul_jsonwrt_empty(_f, _n, UL_JSON_OBJECT)
#define ul_jsonwrt_value_open(_f, _n) ul_jsonwrt_open(_f, _n, UL_JSON_VALUE)
#define ul_jsonwrt_value_close(_f) ul_jsonwrt_close(_f, UL_JSON_VALUE)
-
void ul_jsonwrt_value_raw(struct ul_jsonwrt *fmt,
const char *name, const char *data);
void ul_jsonwrt_value_s(struct ul_jsonwrt *fmt,
const char *name, long double data);
void ul_jsonwrt_value_boolean(struct ul_jsonwrt *fmt,
const char *name, int data);
-void ul_jsonwrt_value_null(struct ul_jsonwrt *fmt,
- const char *name);
+
+#define ul_jsonwrt_value_null(_f, _n) ul_jsonwrt_empty(_f, _n, UL_JSON_VALUE)
#endif /* UTIL_LINUX_JSONWRT_H */
fputs(" ", fmt->out);
}
-void ul_jsonwrt_open(struct ul_jsonwrt *fmt, const char *name, int type)
+static void print_name(struct ul_jsonwrt *fmt, const char *name)
{
if (name) {
if (fmt->after_close)
else
ul_jsonwrt_indent(fmt);
}
+}
+
+void ul_jsonwrt_open(struct ul_jsonwrt *fmt, const char *name, int type)
+{
+ print_name(fmt, name);
switch (type) {
case UL_JSON_OBJECT:
fmt->after_close = 0;
}
+void ul_jsonwrt_empty(struct ul_jsonwrt *fmt, const char *name, int type)
+{
+ print_name(fmt, name);
+
+ switch (type) {
+ case UL_JSON_OBJECT:
+ fputs(name ? ": {}" : "{}", fmt->out);
+ break;
+ case UL_JSON_ARRAY:
+ fputs(name ? ": []" : "[]", fmt->out);
+ break;
+ case UL_JSON_VALUE:
+ fputs(name ? ": null" : "null", fmt->out);
+ break;
+ }
+
+ fmt->after_close = 1;
+}
+
void ul_jsonwrt_close(struct ul_jsonwrt *fmt, int type)
{
assert(fmt->indent > 0);
fmt->after_close = 1;
}
+
void ul_jsonwrt_flush(struct ul_jsonwrt *fmt)
{
fflush(fmt->out);
fputs(data ? "true" : "false", fmt->out);
ul_jsonwrt_value_close(fmt);
}
-
-void ul_jsonwrt_value_null(struct ul_jsonwrt *fmt,
- const char *name)
-{
- ul_jsonwrt_value_open(fmt, name);
- fputs("null", fmt->out);
- ul_jsonwrt_value_close(fmt);
-}