diff mbox

[COMMITTED] benchtests: Print string array elements, int and uint in json

Message ID 1498155434-22182-1-git-send-email-siddhesh@sourceware.org
State New
Headers show

Commit Message

Siddhesh Poyarekar June 22, 2017, 6:17 p.m. UTC
Enhance the json module in benchtests to print signed and unsigned
integers and string array elements.

	* benchtests/json-lib.h: Include inttypes.h.
	(json_attr_int, json_attr_int, json_element_string,
	json_element_int, json_element_uint): New functions.
	* benchtests/json-lib.c: (json_attr_int, json_attr_int,
	json_element_string, json_element_int, json_element_uint): New
	functions.
---
 benchtests/json-lib.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++
 benchtests/json-lib.h |  6 +++++
 2 files changed, 68 insertions(+)
diff mbox

Patch

diff --git a/benchtests/json-lib.c b/benchtests/json-lib.c
index 61573c5..9743506 100644
--- a/benchtests/json-lib.c
+++ b/benchtests/json-lib.c
@@ -101,6 +101,32 @@  json_attr_string (json_ctx_t *ctx, const char *name, const char *s)
 }
 
 void
+json_attr_uint (json_ctx_t *ctx, const char *name, uint64_t d)
+{
+  if (!ctx->first_element)
+    fprintf (ctx->fp, ",\n");
+  else
+    ctx->first_element = false;
+
+  do_indent (ctx);
+
+  fprintf (ctx->fp, "\"%s\": %" PRIu64 , name, d);
+}
+
+void
+json_attr_int (json_ctx_t *ctx, const char *name, int64_t d)
+{
+  if (!ctx->first_element)
+    fprintf (ctx->fp, ",\n");
+  else
+    ctx->first_element = false;
+
+  do_indent (ctx);
+
+  fprintf (ctx->fp, "\"%s\": %" PRId64 , name, d);
+}
+
+void
 json_attr_double (json_ctx_t *ctx, const char *name, double d)
 {
   if (!ctx->first_element)
@@ -137,6 +163,42 @@  json_array_end (json_ctx_t *ctx)
 }
 
 void
+json_element_string (json_ctx_t *ctx, const char *s)
+{
+  if (!ctx->first_element)
+    fprintf (ctx->fp, ", \"%s\"", s);
+  else
+    {
+      fprintf (ctx->fp, "\"%s\"", s);
+      ctx->first_element = false;
+    }
+}
+
+void
+json_element_uint (json_ctx_t *ctx, uint64_t d)
+{
+  if (!ctx->first_element)
+    fprintf (ctx->fp, ", %" PRIu64, d);
+  else
+    {
+      fprintf (ctx->fp, "%" PRIu64, d);
+      ctx->first_element = false;
+    }
+}
+
+void
+json_element_int (json_ctx_t *ctx, int64_t d)
+{
+  if (!ctx->first_element)
+    fprintf (ctx->fp, ", %" PRId64, d);
+  else
+    {
+      fprintf (ctx->fp, "%" PRId64, d);
+      ctx->first_element = false;
+    }
+}
+
+void
 json_element_double (json_ctx_t *ctx, double d)
 {
   if (!ctx->first_element)
diff --git a/benchtests/json-lib.h b/benchtests/json-lib.h
index 8a4e591..b3df4b5 100644
--- a/benchtests/json-lib.h
+++ b/benchtests/json-lib.h
@@ -20,6 +20,7 @@ 
 #define __JSON_LIB_H__
 
 #include <stdbool.h>
+#include <inttypes.h>
 #include <stdio.h>
 
 struct json_ctx
@@ -37,9 +38,14 @@  void json_document_end (json_ctx_t *ctx);
 void json_attr_object_begin (json_ctx_t *ctx, const char *name);
 void json_attr_object_end (json_ctx_t *ctx);
 void json_attr_string (json_ctx_t *ctx, const char *name, const char *s);
+void json_attr_int (json_ctx_t *ctx, const char *name, int64_t d);
+void json_attr_uint (json_ctx_t *ctx, const char *name, uint64_t d);
 void json_attr_double (json_ctx_t *ctx, const char *name, double d);
 void json_array_begin (json_ctx_t *ctx, const char *name);
 void json_array_end (json_ctx_t *ctx);
+void json_element_string (json_ctx_t *ctx, const char *s);
+void json_element_int (json_ctx_t *ctx, int64_t d);
+void json_element_uint (json_ctx_t *ctx, uint64_t d);
 void json_element_double (json_ctx_t *ctx, double d);
 void json_element_object_begin (json_ctx_t *ctx);
 void json_element_object_end (json_ctx_t *ctx);