]> git.ipfire.org Git - thirdparty/hostap.git/commitdiff
JSON: Add helper functions for building tokens
authorJouni Malinen <jouni@codeaurora.org>
Wed, 27 Nov 2019 14:06:43 +0000 (16:06 +0200)
committerJouni Malinen <j@w1.fi>
Thu, 28 Nov 2019 14:39:09 +0000 (16:39 +0200)
Signed-off-by: Jouni Malinen <jouni@codeaurora.org>
src/utils/json.c
src/utils/json.h

index 0b7de780b28910b55fcb8812c13d4a72357a62aa..9ec7ac941a65d4c5daa3282063248c2c1b7ee263 100644 (file)
@@ -574,3 +574,79 @@ void json_print_tree(struct json_token *root, char *buf, size_t buflen)
        buf[0] = '\0';
        json_print_token(root, 1, buf, buflen);
 }
+
+
+void json_add_int(struct wpabuf *json, const char *name, int val)
+{
+       wpabuf_printf(json, "\"%s\":%d", name, val);
+}
+
+
+void json_add_string(struct wpabuf *json, const char *name, const char *val)
+{
+       wpabuf_printf(json, "\"%s\":\"%s\"", name, val);
+}
+
+
+int json_add_string_escape(struct wpabuf *json, const char *name,
+                          const void *val, size_t len)
+{
+       char *tmp;
+       size_t tmp_len = 6 * len + 1;
+
+       tmp = os_malloc(tmp_len);
+       if (!tmp)
+               return -1;
+       json_escape_string(tmp, tmp_len, val, len);
+       json_add_string(json, name, tmp);
+       bin_clear_free(tmp, tmp_len);
+       return 0;
+}
+
+
+int json_add_base64url(struct wpabuf *json, const char *name, const void *val,
+                      size_t len)
+{
+       char *b64;
+
+       b64 = base64_url_encode(val, len, NULL);
+       if (!b64)
+               return -1;
+       json_add_string(json, name, b64);
+       os_free(b64);
+       return 0;
+}
+
+
+void json_start_object(struct wpabuf *json, const char *name)
+{
+       if (name)
+               wpabuf_printf(json, "\"%s\":", name);
+       wpabuf_put_u8(json, '{');
+}
+
+
+void json_end_object(struct wpabuf *json)
+{
+       wpabuf_put_u8(json, '}');
+}
+
+
+void json_start_array(struct wpabuf *json, const char *name)
+{
+       if (name)
+               wpabuf_printf(json, "\"%s\":", name);
+       wpabuf_put_u8(json, '[');
+}
+
+
+void json_end_array(struct wpabuf *json)
+{
+       wpabuf_put_u8(json, ']');
+}
+
+
+void json_value_sep(struct wpabuf *json)
+{
+       wpabuf_put_u8(json, ',');
+}
index 8faa95d8bf20d7b9696930d43df0d3f470ac1566..ca4a2e47e5ff74ae29ad027b967938441e122ab7 100644 (file)
@@ -38,5 +38,16 @@ struct json_token * json_get_member(struct json_token *json, const char *name);
 struct wpabuf * json_get_member_base64url(struct json_token *json,
                                          const char *name);
 void json_print_tree(struct json_token *root, char *buf, size_t buflen);
+void json_add_int(struct wpabuf *json, const char *name, int val);
+void json_add_string(struct wpabuf *json, const char *name, const char *val);
+int json_add_string_escape(struct wpabuf *json, const char *name,
+                          const void *val, size_t len);
+int json_add_base64url(struct wpabuf *json, const char *name, const void *val,
+                      size_t len);
+void json_start_object(struct wpabuf *json, const char *name);
+void json_end_object(struct wpabuf *json);
+void json_start_array(struct wpabuf *json, const char *name);
+void json_end_array(struct wpabuf *json);
+void json_value_sep(struct wpabuf *json);
 
 #endif /* JSON_H */