Skip to content

Commit

Permalink
JSON: write all keys in lower case
Browse files Browse the repository at this point in the history
Postgresql and MySQL use different cases for database tables (lower
case vs. CamelCase).
To prevent problems, the JSON output converts all keys to
lowercase.

Fixes #493: bareos-webui with MySQL
  • Loading branch information
joergsteffens authored and Marco van Wieringen committed Jul 27, 2015
1 parent a3f26c5 commit 8b033a3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/lib/mem_pool.c
Expand Up @@ -718,6 +718,11 @@ int POOL_MEM::strcpy(const char *str)
return len - 1;
}

void POOL_MEM::toLower()
{
lcase(mem);
}

int POOL_MEM::bsprintf(const char *fmt, ...)
{
int len;
Expand Down
1 change: 1 addition & 0 deletions src/lib/mem_pool.h
Expand Up @@ -103,6 +103,7 @@ class POOL_MEM {
int strcpy(const char *str);
int strcat(POOL_MEM &str);
int strcat(const char *str);
void toLower();
size_t strlen() { return ::strlen(mem); };
int bsprintf(const char *fmt, ...);
int bvsprintf(const char *fmt, va_list arg_ptr);
Expand Down
9 changes: 7 additions & 2 deletions src/lib/output_formatter.c
Expand Up @@ -411,26 +411,30 @@ void OUTPUT_FORMATTER::finalize_result(bool result)
bool OUTPUT_FORMATTER::json_key_value_add(const char *key, uint64_t value)
{
json_t *json_obj = NULL;
POOL_MEM lkey(key);

lkey.toLower();
json_obj = (json_t *)result_stack_json->last();
if (json_obj == NULL) {
Emsg2(M_ERROR, 0, "No json object defined to add %s: %llu", key, value);
}
json_object_set(json_obj, key, json_integer(value));
json_object_set(json_obj, lkey.c_str(), json_integer(value));

return true;
}

bool OUTPUT_FORMATTER::json_key_value_add(const char *key, const char *value)
{
json_t *json_obj = NULL;
POOL_MEM lkey(key);

lkey.toLower();
json_obj = (json_t *)result_stack_json->last();
if (json_obj == NULL) {
Emsg2(M_ERROR, 0, "No json object defined to add %s: %s", key, value);
return false;
}
json_object_set(json_obj, key, json_string(value));
json_object_set(json_obj, lkey.c_str(), json_string(value));

return true;
}
Expand All @@ -439,6 +443,7 @@ void OUTPUT_FORMATTER::json_add_message(const char *type, POOL_MEM &message)
{
json_t *message_type_array;
json_t *message_json=json_string(message.c_str());

if (type != NULL) {
message_type_array = json_object_get(message_object_json, type);
} else {
Expand Down

0 comments on commit 8b033a3

Please sign in to comment.