refactor: introduced convenience functions for json_writer

pull/3/head
Falk Werner 4 years ago
parent 5cdeb6d759
commit e0b01cfe19

@ -47,8 +47,7 @@ wfp_impl_credentials_write(
json_t * contents = credentials->contents;
json_object_foreach(contents, key, value)
{
wfp_impl_json_writer_object_key(writer, key);
wfp_impl_json_writer_write_string(writer, json_string_value(value));
wfp_impl_json_writer_object_write_string(writer, key, json_string_value(value));
}
wfp_impl_json_writer_object_end(writer);

@ -360,4 +360,42 @@ wfp_impl_json_writer_array_end(
wfp_impl_json_writer_pop(writer);
wfp_impl_json_writer_end_value(writer);
}
}
void
wfp_impl_json_writer_object_write_int(
struct wfp_json_writer * writer,
char const * key,
int value)
{
wfp_impl_json_writer_object_key(writer, key);
wfp_impl_json_writer_write_int(writer, value);
}
void
wfp_impl_json_writer_object_write_string(
struct wfp_json_writer * writer,
char const * key,
char const * value)
{
wfp_impl_json_writer_object_key(writer, key);
wfp_impl_json_writer_write_string(writer, value);
}
void
wfp_impl_json_writer_object_begin_object(
struct wfp_json_writer * writer,
char const * key)
{
wfp_impl_json_writer_object_key(writer, key);
wfp_impl_json_writer_object_begin(writer);
}
void
wfp_impl_json_writer_object_begin_array(
struct wfp_json_writer * writer,
char const * key)
{
wfp_impl_json_writer_object_key(writer, key);
wfp_impl_json_writer_array_begin(writer);
}

@ -66,6 +66,28 @@ wfp_impl_json_writer_object_key(
struct wfp_json_writer * writer,
char const * key);
extern void
wfp_impl_json_writer_object_write_int(
struct wfp_json_writer * writer,
char const * key,
int value);
extern void
wfp_impl_json_writer_object_write_string(
struct wfp_json_writer * writer,
char const * key,
char const * value);
extern void
wfp_impl_json_writer_object_begin_object(
struct wfp_json_writer * writer,
char const * key);
extern void
wfp_impl_json_writer_object_begin_array(
struct wfp_json_writer * writer,
char const * key);
extern void
wfp_impl_json_writer_array_begin(
struct wfp_json_writer * writer);

@ -61,11 +61,8 @@ static char * wfp_jsonrpc_request_create(
struct wfp_json_writer * writer = wfp_impl_json_writer_create(128, LWS_PRE);
wfp_impl_json_writer_object_begin(writer);
wfp_impl_json_writer_object_key(writer, "method");
wfp_impl_json_writer_write_string(writer, method);
wfp_impl_json_writer_object_key(writer, "params");
wfp_impl_json_writer_array_begin(writer);
wfp_impl_json_writer_object_write_string(writer, "method", method);
wfp_impl_json_writer_object_begin_array(writer, "params");
for (char const * param_type = param_info; '\0' != *param_type; param_type++)
{
switch(*param_type)
@ -98,8 +95,7 @@ static char * wfp_jsonrpc_request_create(
if (0 != id)
{
wfp_impl_json_writer_object_key(writer, "id");
wfp_impl_json_writer_write_int(writer, id);
wfp_impl_json_writer_object_write_int(writer, "id", id);
}
wfp_impl_json_writer_object_end(writer);

@ -145,8 +145,7 @@ TEST(json_writer, write_one_element_object)
{
wfp_json_writer * writer = wfp_impl_json_writer_create(128,0);
wfp_impl_json_writer_object_begin(writer);
wfp_impl_json_writer_object_key(writer, "answer");
wfp_impl_json_writer_write_int(writer, 42);
wfp_impl_json_writer_object_write_int(writer, "answer", 42);
wfp_impl_json_writer_object_end(writer);
char * data = wfp_impl_json_writer_take_data(writer, nullptr);
@ -162,14 +161,9 @@ TEST(json_writer, write_mixed_object)
wfp_json_writer * writer = wfp_impl_json_writer_create(128,0);
wfp_impl_json_writer_object_begin(writer);
wfp_impl_json_writer_object_key(writer, "a");
wfp_impl_json_writer_write_int(writer, 42);
wfp_impl_json_writer_object_key(writer, "b");
wfp_impl_json_writer_write_string(writer, "0");
wfp_impl_json_writer_object_key(writer, "c");
wfp_impl_json_writer_array_begin(writer);
wfp_impl_json_writer_object_write_int(writer, "a", 42);
wfp_impl_json_writer_object_write_string(writer, "b", "0");
wfp_impl_json_writer_object_begin_array(writer, "c");
wfp_impl_json_writer_array_end(writer);
wfp_impl_json_writer_object_end(writer);
@ -187,12 +181,9 @@ TEST(json_writer, write_nested_object)
wfp_json_writer * writer = wfp_impl_json_writer_create(128,0);
wfp_impl_json_writer_object_begin(writer);
wfp_impl_json_writer_object_key(writer, "a");
wfp_impl_json_writer_object_begin(writer);
wfp_impl_json_writer_object_key(writer, "b");
wfp_impl_json_writer_object_begin(writer);
wfp_impl_json_writer_object_key(writer, "c");
wfp_impl_json_writer_object_begin(writer);
wfp_impl_json_writer_object_begin_object(writer, "a");
wfp_impl_json_writer_object_begin_object(writer, "b");
wfp_impl_json_writer_object_begin_object(writer, "c");
wfp_impl_json_writer_object_end(writer);
wfp_impl_json_writer_object_end(writer);
wfp_impl_json_writer_object_end(writer);

Loading…
Cancel
Save