Simplify the interface
This commit is contained in:
1 parent
840d5122b5
commit
6e88c54244
1 file changed
+21
-34
+21
-34
@@ -58,7 +58,10 @@ static inline bool real_in_str(float *r, FILE *f)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Let's define the header & its interface */
|
/* Let's define the JSON structure & its interface.
|
||||||
|
The JSON structure is incomplete as it can only be complete
|
||||||
|
after the definition of all its content.
|
||||||
|
*/
|
||||||
|
|
||||||
typedef struct json_node_s *json_t;
|
typedef struct json_node_s *json_t;
|
||||||
|
|
||||||
@@ -86,6 +89,7 @@ VARIANT_DEF2(variant_json,
|
|||||||
(array, array_json_t, ARRAY_OPLIST(array_json, JSON_OPLIST)),
|
(array, array_json_t, ARRAY_OPLIST(array_json, JSON_OPLIST)),
|
||||||
(dict, dict_json_t, DICT_OPLIST(dict_json, STRING_OPLIST, JSON_OPLIST)))
|
(dict, dict_json_t, DICT_OPLIST(dict_json, STRING_OPLIST, JSON_OPLIST)))
|
||||||
|
|
||||||
|
/* Let's complete the structure */
|
||||||
struct json_node_s {
|
struct json_node_s {
|
||||||
variant_json_t json;
|
variant_json_t json;
|
||||||
};
|
};
|
||||||
@@ -93,60 +97,43 @@ struct json_node_s {
|
|||||||
|
|
||||||
/* Body definition */
|
/* Body definition */
|
||||||
|
|
||||||
void json_new(json_t *p)
|
|
||||||
{
|
|
||||||
*p = malloc (sizeof (struct json_node_s));
|
|
||||||
if (*p == NULL) abort();
|
|
||||||
}
|
|
||||||
|
|
||||||
void json_init(json_t *p)
|
void json_init(json_t *p)
|
||||||
{
|
{
|
||||||
*p = NULL;
|
assert (p != NULL);
|
||||||
}
|
*p = malloc (sizeof (struct json_node_s));
|
||||||
|
if (*p == NULL) abort();
|
||||||
void json_init_new(json_t *p)
|
|
||||||
{
|
|
||||||
json_new(p);
|
|
||||||
variant_json_init((*p)->json);
|
variant_json_init((*p)->json);
|
||||||
}
|
}
|
||||||
|
|
||||||
void json_clear (json_t p)
|
void json_clear (json_t p)
|
||||||
{
|
{
|
||||||
if (p != NULL) {
|
assert (p != NULL);
|
||||||
variant_json_clear (p->json);
|
variant_json_clear (p->json);
|
||||||
free(p);
|
free(p);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void json_init_set(json_t *p, json_t o)
|
void json_init_set(json_t *p, json_t o)
|
||||||
{
|
{
|
||||||
if (o == NULL) {
|
assert (p != NULL);
|
||||||
*p = NULL;
|
*p = malloc (sizeof (struct json_node_s));
|
||||||
} else {
|
if (*p == NULL) abort();
|
||||||
json_new(p);
|
variant_json_init_set((*p)->json, o->json);
|
||||||
variant_json_init_set((*p)->json, o->json);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void json_set(json_t *p, json_t o)
|
void json_set(json_t *p, json_t o)
|
||||||
{
|
{
|
||||||
json_clear(*p);
|
assert (p != NULL);
|
||||||
json_init_set(p, o);
|
variant_json_set((*p)->json, o->json);
|
||||||
}
|
}
|
||||||
|
|
||||||
void json_out_str(FILE *f, json_t o)
|
void json_out_str(FILE *f, json_t o)
|
||||||
{
|
{
|
||||||
if (o != NULL) {
|
variant_json_out_str(f, o->json);
|
||||||
variant_json_out_str(f, o->json);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool json_in_str(json_t *p, FILE *f)
|
bool json_in_str(json_t *p, FILE *f)
|
||||||
{
|
{
|
||||||
if (*p == NULL) {
|
assert (p != NULL);
|
||||||
json_new(p);
|
|
||||||
variant_json_init((*p)->json);
|
|
||||||
}
|
|
||||||
return variant_json_in_str((*p)->json, f);
|
return variant_json_in_str((*p)->json, f);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -160,8 +147,8 @@ static json_t generate(void)
|
|||||||
string_t s;
|
string_t s;
|
||||||
|
|
||||||
/* This is a low level generation for example */
|
/* This is a low level generation for example */
|
||||||
json_init_new(&p);
|
json_init(&p);
|
||||||
json_init_new(&k);
|
json_init(&k);
|
||||||
array_json_init(a);
|
array_json_init(a);
|
||||||
dict_json_init(d);
|
dict_json_init(d);
|
||||||
string_init (s);
|
string_init (s);
|
||||||
|
|||||||
Reference in new issue
Block a user