summaryrefslogtreecommitdiffstats
path: root/lib/jsonwrt.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/jsonwrt.c')
-rw-r--r--lib/jsonwrt.c37
1 files changed, 26 insertions, 11 deletions
diff --git a/lib/jsonwrt.c b/lib/jsonwrt.c
index 8ca1d4d..243ed82 100644
--- a/lib/jsonwrt.c
+++ b/lib/jsonwrt.c
@@ -26,12 +26,12 @@
* }
* }
*/
-static void fputs_quoted_case_json(const char *data, FILE *out, int dir)
+static void fputs_quoted_case_json(const char *data, FILE *out, int dir, size_t size)
{
const char *p;
fputc('"', out);
- for (p = data; p && *p; p++) {
+ for (p = data; p && *p && (!size || p < data + size); p++) {
const unsigned int c = (unsigned int) *p;
@@ -98,9 +98,9 @@ static void fputs_quoted_case_json(const char *data, FILE *out, int dir)
fputc('"', out);
}
-#define fputs_quoted_json(_d, _o) fputs_quoted_case_json(_d, _o, 0)
-#define fputs_quoted_json_upper(_d, _o) fputs_quoted_case_json(_d, _o, 1)
-#define fputs_quoted_json_lower(_d, _o) fputs_quoted_case_json(_d, _o, -1)
+#define fputs_quoted_json(_d, _o) fputs_quoted_case_json(_d, _o, 0, 0)
+#define fputs_quoted_json_upper(_d, _o) fputs_quoted_case_json(_d, _o, 1, 0)
+#define fputs_quoted_json_lower(_d, _o) fputs_quoted_case_json(_d, _o, -1, 0)
void ul_jsonwrt_init(struct ul_jsonwrt *fmt, FILE *out, int indent)
{
@@ -154,12 +154,6 @@ void ul_jsonwrt_open(struct ul_jsonwrt *fmt, const char *name, int type)
void ul_jsonwrt_close(struct ul_jsonwrt *fmt, int type)
{
- if (fmt->indent == 1) {
- fputs("\n}\n", fmt->out);
- fmt->indent--;
- fmt->after_close = 1;
- return;
- }
assert(fmt->indent > 0);
switch (type) {
@@ -168,6 +162,8 @@ void ul_jsonwrt_close(struct ul_jsonwrt *fmt, int type)
fputc('\n', fmt->out);
ul_jsonwrt_indent(fmt);
fputs("}", fmt->out);
+ if (fmt->indent == 0)
+ fputs("\n", fmt->out);
break;
case UL_JSON_ARRAY:
fmt->indent--;
@@ -204,6 +200,17 @@ void ul_jsonwrt_value_s(struct ul_jsonwrt *fmt,
ul_jsonwrt_value_close(fmt);
}
+void ul_jsonwrt_value_s_sized(struct ul_jsonwrt *fmt,
+ const char *name, const char *data, size_t size)
+{
+ ul_jsonwrt_value_open(fmt, name);
+ if (data && *data)
+ fputs_quoted_case_json(data, fmt->out, 0, size);
+ else
+ fputs("null", fmt->out);
+ ul_jsonwrt_value_close(fmt);
+}
+
void ul_jsonwrt_value_u64(struct ul_jsonwrt *fmt,
const char *name, uint64_t data)
{
@@ -212,6 +219,14 @@ void ul_jsonwrt_value_u64(struct ul_jsonwrt *fmt,
ul_jsonwrt_value_close(fmt);
}
+void ul_jsonwrt_value_double(struct ul_jsonwrt *fmt,
+ const char *name, long double data)
+{
+ ul_jsonwrt_value_open(fmt, name);
+ fprintf(fmt->out, "%Lg", data);
+ ul_jsonwrt_value_close(fmt);
+}
+
void ul_jsonwrt_value_boolean(struct ul_jsonwrt *fmt,
const char *name, int data)
{