Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NULL repeated fields should be converted to an empty list #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/protobuf2json.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,21 @@ static int protobuf2json_process_message(
);
}
}
else {
json_t *array = json_array();
if (!array) {
SET_ERROR_STRING_AND_RETURN(
PROTOBUF2JSON_ERR_CANNOT_ALLOCATE_MEMORY,
"Cannot allocate JSON structure using json_array()"
);
}
if (json_object_set_new(*json_message, field_descriptor->name, array)) {
SET_ERROR_STRING_AND_RETURN(
PROTOBUF2JSON_ERR_JANSSON_INTERNAL,
"Error in json_object_set_new()"
);
}
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion test/run-tmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,8 @@ void read_file_success(void) {
json_string,
"{\n"
" \"name\": \"John Doe\",\n"
" \"id\": 42\n"
" \"id\": 42,\n"
" \"phone\": []\n"
"}"
);

Expand Down
3 changes: 2 additions & 1 deletion test/test-json2protobuf-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ TEST_IMPL(json2protobuf_file__success) {
json_string,
"{\n"
" \"name\": \"John Doe\",\n"
" \"id\": 42\n"
" \"id\": 42,\n"
" \"phone\": []\n"
"}"
);

Expand Down
3 changes: 2 additions & 1 deletion test/test-protobuf2json-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ TEST_IMPL(protobuf2json_file__success) {
json_string,
"{\n"
" \"name\": \"John Doe\",\n"
" \"id\": 42\n"
" \"id\": 42,\n"
" \"phone\": []\n"
"}"
);

Expand Down
6 changes: 4 additions & 2 deletions test/test-protobuf2json-string.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ TEST_IMPL(protobuf2json_string__required_field) {
json_string,
"{\n"
" \"name\": \"John Doe\",\n"
" \"id\": 42\n"
" \"id\": 42,\n"
" \"phone\": []\n"
"}"
);

Expand Down Expand Up @@ -58,7 +59,8 @@ TEST_IMPL(protobuf2json_string__optional_field) {
"{\n"
" \"name\": \"John Doe\",\n"
" \"id\": 42,\n"
" \"email\": \"[email protected]\"\n"
" \"email\": \"[email protected]\",\n"
" \"phone\": []\n"
"}"
);

Expand Down
42 changes: 39 additions & 3 deletions test/test-reversible.c
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,11 @@ TEST_IMPL(reversible__numbers) {
" true,\n"
" false,\n"
" false\n"
" ]\n"
" ],\n"
" \"value_enum\": [],\n"
" \"value_string\": [],\n"
" \"value_bytes\": [],\n"
" \"value_message\": []\n"

"}"
;
Expand Down Expand Up @@ -339,11 +343,27 @@ TEST_IMPL(reversible__strings) {

const char *expected_json_string = \
"{\n"
" \"value_int32\": [],\n"
" \"value_sint32\": [],\n"
" \"value_sfixed32\": [],\n"
" \"value_uint32\": [],\n"
" \"value_fixed32\": [],\n"
" \"value_int64\": [],\n"
" \"value_sint64\": [],\n"
" \"value_sfixed64\": [],\n"
" \"value_uint64\": [],\n"
" \"value_fixed64\": [],\n"
" \"value_float\": [],\n"
" \"value_double\": [],\n"
" \"value_bool\": [],\n"
" \"value_enum\": [],\n"
" \"value_string\": [\n"
" \"qwerty \",\n" /* Note: \0-byte terminated string */
" \"qwerty \",\n" /* Note: \0-byte terminated string */
" \"\"\n" /* Note: \0-byte terminated string */
" ]\n"
" ],\n"
" \"value_bytes\": [],\n"
" \"value_message\": []\n"
"}"
;

Expand Down Expand Up @@ -400,12 +420,28 @@ TEST_IMPL(reversible__bytes) {

const char *expected_json_string = \
"{\n"
" \"value_int32\": [],\n"
" \"value_sint32\": [],\n"
" \"value_sfixed32\": [],\n"
" \"value_uint32\": [],\n"
" \"value_fixed32\": [],\n"
" \"value_int64\": [],\n"
" \"value_sint64\": [],\n"
" \"value_sfixed64\": [],\n"
" \"value_uint64\": [],\n"
" \"value_fixed64\": [],\n"
" \"value_float\": [],\n"
" \"value_double\": [],\n"
" \"value_bool\": [],\n"
" \"value_enum\": [],\n"
" \"value_string\": [],\n"
" \"value_bytes\": [\n"
" \"AA==\",\n" /* "\0" */
" \"cXdlcnR5IAAgMTIzNA==\",\n" /* "qwerty \0 12345" */
" \"cXdlcnR5IA==\",\n" /* "qwerty \0" */
" \"ACAxMjM0NQ==\"\n" /* "\0 12345" */
" ]\n"
" ],\n"
" \"value_message\": []\n"
"}"
;

Expand Down