properties: improve serialize method

Properly escape the keys because we're trying to generate
valid JSON.
Always place ',' after items.
Add a flag to add a '\n' before each item.
Don't try to string-encode the value when it's already a string.
This commit is contained in:
Wim Taymans 2021-06-09 17:57:58 +02:00
parent a10cb3a597
commit 54326abd54
2 changed files with 14 additions and 2 deletions

View file

@ -598,15 +598,26 @@ int pw_properties_serialize_dict(FILE *f, const struct spa_dict *dict, uint32_t
{
const struct spa_dict_item *it;
int count = 0;
char key[1024];
spa_dict_for_each(it, dict) {
size_t len = it->value ? strlen(it->value) : 0;
fprintf(f, " \"%s\" = ", it->key);
if (spa_json_encode_string(key, sizeof(key)-1, it->key) >= (int)sizeof(key)-1)
continue;
fprintf(f, "%s%s %s: ",
count == 0 ? "" : ",",
flags & PW_PROPERTIES_FLAG_NL ? "\n" : "",
key);
if (it->value == NULL) {
fprintf(f, "null");
} else if (spa_json_is_null(it->value, len) ||
spa_json_is_float(it->value, len) ||
spa_json_is_bool(it->value, len) ||
spa_json_is_container(it->value, len)) {
spa_json_is_container(it->value, len) ||
spa_json_is_string(it->value, len)) {
fprintf(f, "%s", it->value);
} else {
encode_string(f, it->value);

View file

@ -100,6 +100,7 @@ pw_properties_get(const struct pw_properties *properties, const char *key);
const char *
pw_properties_iterate(const struct pw_properties *properties, void **state);
#define PW_PROPERTIES_FLAG_NL (1<<0)
int pw_properties_serialize_dict(FILE *f, const struct spa_dict *dict, uint32_t flags);
static inline bool pw_properties_parse_bool(const char *value) {