<p>Kevin Harwell has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/16641">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">json: Add a object create with vars method and integer access macro<br><br>Add a function that will create an ast_json object out of a list of<br>Asterisk variables. An excludes string can also optionally be passed<br>in.<br><br>Also, add a macro to make it easier to get object integers.<br><br>Change-Id: I5f34f18e102126aef3997f19a553a266d70d6226<br>---<br>M include/asterisk/json.h<br>M main/json.c<br>M tests/test_json.c<br>3 files changed, 84 insertions(+), 0 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/41/16641/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/include/asterisk/json.h b/include/asterisk/json.h</span><br><span>index ce0baa4..e8d6bd7 100644</span><br><span>--- a/include/asterisk/json.h</span><br><span>+++ b/include/asterisk/json.h</span><br><span>@@ -539,6 +539,15 @@</span><br><span> struct ast_json *ast_json_object_create(void);</span><br><span> </span><br><span> /*!</span><br><span style="color: hsl(120, 100%, 40%);">+ * \brief Create a new JSON object using the given variables</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param variables A list of Asterisk variables</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param excludes Variable names to exclude (optional)</span><br><span style="color: hsl(120, 100%, 40%);">+ * \return Newly allocated object.</span><br><span style="color: hsl(120, 100%, 40%);">+ * \return \c NULL on error.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+struct ast_json *ast_json_object_create_vars(const struct ast_variable *variables, const char *excludes);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*!</span><br><span> * \brief Get size of JSON object.</span><br><span> * \since 12.0.0</span><br><span> * \param object JSON object.</span><br><span>@@ -573,6 +582,14 @@</span><br><span> #define ast_json_object_string_get(object, key) ast_json_string_get(ast_json_object_get(object, key))</span><br><span> </span><br><span> /*!</span><br><span style="color: hsl(120, 100%, 40%);">+ * \brief Get an integer field from a JSON object.</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param integer JSON integer.</span><br><span style="color: hsl(120, 100%, 40%);">+ * \return Value of a JSON integer.</span><br><span style="color: hsl(120, 100%, 40%);">+ * \return 0 if \a integer is not a JSON integer.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+#define ast_json_object_integer_get(object, key) ast_json_integer_get(ast_json_object_get(object, key))</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*!</span><br><span> * \brief Set a field in a JSON object.</span><br><span> * \since 12.0.0</span><br><span> *</span><br><span>diff --git a/main/json.c b/main/json.c</span><br><span>index 09101aa..abaae6d 100644</span><br><span>--- a/main/json.c</span><br><span>+++ b/main/json.c</span><br><span>@@ -852,3 +852,22 @@</span><br><span> </span><br><span> return ret;</span><br><span> }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+struct ast_json *ast_json_object_create_vars(const struct ast_variable *variables, const char *excludes)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ const struct ast_variable *i;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_json *obj;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ obj = ast_json_object_create();</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!obj) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ for (i = variables; i; i = i->next) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!excludes || !strstr(excludes, i->name)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_json_object_set(obj, i->name, ast_json_string_create(i->value));</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return obj;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span>diff --git a/tests/test_json.c b/tests/test_json.c</span><br><span>index 8dbb872..edf4a9d 100644</span><br><span>--- a/tests/test_json.c</span><br><span>+++ b/tests/test_json.c</span><br><span>@@ -37,6 +37,7 @@</span><br><span> </span><br><span> #include "asterisk.h"</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+#include "asterisk/config.h"</span><br><span> #include "asterisk/json.h"</span><br><span> #include "asterisk/module.h"</span><br><span> #include "asterisk/test.h"</span><br><span>@@ -1090,6 +1091,51 @@</span><br><span> return AST_TEST_PASS;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+AST_TEST_DEFINE(json_test_object_create_vars)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);</span><br><span style="color: hsl(120, 100%, 40%);">+ RAII_VAR(struct ast_variable *, vars, NULL, ast_variables_destroy);</span><br><span style="color: hsl(120, 100%, 40%);">+ const char *value;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_variable *new_var;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ switch (cmd) {</span><br><span style="color: hsl(120, 100%, 40%);">+ case TEST_INIT:</span><br><span style="color: hsl(120, 100%, 40%);">+ info->name = "object_create_vars";</span><br><span style="color: hsl(120, 100%, 40%);">+ info->category = CATEGORY;</span><br><span style="color: hsl(120, 100%, 40%);">+ info->summary = "Testing JSON object creation initialized using Asterisk variables.";</span><br><span style="color: hsl(120, 100%, 40%);">+ info->description = "Test JSON abstraction library.";</span><br><span style="color: hsl(120, 100%, 40%);">+ return AST_TEST_NOT_RUN;</span><br><span style="color: hsl(120, 100%, 40%);">+ case TEST_EXECUTE:</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* NULL case */</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_test_validate(test, (uut = ast_json_object_create_vars(NULL, NULL)));</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_test_validate(test, !(value = ast_json_object_string_get(uut, "foo")));</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_test_validate(test, (new_var = ast_variable_new("foo", "bar", "")));</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_variable_list_append(&vars, new_var);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_test_validate(test, (new_var = ast_variable_new("bar", "baz", "")));</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_variable_list_append(&vars, new_var);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Variables case */</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_json_unref(uut);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_test_validate(test, (uut = ast_json_object_create_vars(vars, NULL)));</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_test_validate(test, (value = ast_json_object_string_get(uut, "foo")));</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_test_validate(test, !strcmp("bar", value));</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_test_validate(test, (value = ast_json_object_string_get(uut, "bar")));</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_test_validate(test, !strcmp("baz", value));</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Variables with excludes case */</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_json_unref(uut);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_test_validate(test, (uut = ast_json_object_create_vars(vars, "foo")));</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_test_validate(test, !(value = ast_json_object_string_get(uut, "foo")));</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_test_validate(test, (value = ast_json_object_string_get(uut, "bar")));</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_test_validate(test, !strcmp("baz", value));</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return AST_TEST_PASS;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> AST_TEST_DEFINE(json_test_dump_load_string)</span><br><span> {</span><br><span> RAII_VAR(struct ast_json *, uut, NULL, ast_json_unref);</span><br><span>@@ -1738,6 +1784,7 @@</span><br><span> AST_TEST_UNREGISTER(json_test_object_null);</span><br><span> AST_TEST_UNREGISTER(json_test_object_iter);</span><br><span> AST_TEST_UNREGISTER(json_test_object_iter_null);</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_TEST_UNREGISTER(json_test_object_create_vars);</span><br><span> AST_TEST_UNREGISTER(json_test_dump_load_string);</span><br><span> AST_TEST_UNREGISTER(json_test_dump_load_str);</span><br><span> AST_TEST_UNREGISTER(json_test_dump_str_fail);</span><br><span>@@ -1794,6 +1841,7 @@</span><br><span> AST_TEST_REGISTER(json_test_object_null);</span><br><span> AST_TEST_REGISTER(json_test_object_iter);</span><br><span> AST_TEST_REGISTER(json_test_object_iter_null);</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_TEST_REGISTER(json_test_object_create_vars);</span><br><span> AST_TEST_REGISTER(json_test_dump_load_string);</span><br><span> AST_TEST_REGISTER(json_test_dump_load_str);</span><br><span> AST_TEST_REGISTER(json_test_dump_str_fail);</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/16641">change 16641</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/c/asterisk/+/16641"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 18 </div>
<div style="display:none"> Gerrit-Change-Id: I5f34f18e102126aef3997f19a553a266d70d6226 </div>
<div style="display:none"> Gerrit-Change-Number: 16641 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Kevin Harwell <kharwell@digium.com> </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>