Skip to content

Commit

Permalink
json: add get_object API and fix str init
Browse files Browse the repository at this point in the history
  • Loading branch information
Emmanuel Schmidbauer committed Jan 29, 2018
1 parent d40c65a commit 5fe8618
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/modules/json/api.h
Expand Up @@ -25,11 +25,12 @@
#include "../../core/sr_module.h"

typedef struct json_object *(*json_parse_f) (const char *str);
typedef str (*json_extract_field_f)(
struct json_object *json_obj, char *json_name);
typedef struct json_object *(*json_get_object_f) (struct json_object *json_obj, const char *str);
typedef str (*json_extract_field_f) (struct json_object *json_obj, char *json_name);

typedef struct json_api {
json_parse_f json_parse;
json_get_object_f get_object;
json_extract_field_f extract_field;
} json_api_t;

Expand Down
6 changes: 3 additions & 3 deletions src/modules/json/json_mod.c
Expand Up @@ -65,9 +65,8 @@ struct module_exports exports = {
0 /* per-child init function */
};

str _json_extract_field(struct json_object *json_obj, char *json_name)
{
str val;
str _json_extract_field(struct json_object *json_obj, char *json_name) {
str val = {0, 0};
json_extract_field(json_name, val);
return val;
}
Expand All @@ -81,6 +80,7 @@ int bind_json(json_api_t *api) {
return -1;
}
api->json_parse = json_parse;
api->get_object = json_get_object;
api->extract_field = _json_extract_field;
return 0;
}
Expand Down

2 comments on commit 5fe8618

@miconda
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the code I think it is better to change the prototype of:

str _json_extract_field(struct json_object *json_obj, char *json_name)

to:

int _json_extract_field(struct json_object *json_obj, char *json_name, str *val)

Now it returns a variable declared on the stack, which could work with recent compilers, being returned by value. str itself is not a large structure, but returning structure values is not recommended in C, code analyzers complain about (same for passing a structure value as parameter -- it is recommended to use pointers).

@eschmidbauer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 im on it

Please sign in to comment.