mirror of
https://gitlab.freedesktop.org/pipewire/pipewire.git
synced 2025-11-27 07:00:12 -05:00
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:
parent
a10cb3a597
commit
54326abd54
2 changed files with 14 additions and 2 deletions
|
|
@ -598,15 +598,26 @@ int pw_properties_serialize_dict(FILE *f, const struct spa_dict *dict, uint32_t
|
||||||
{
|
{
|
||||||
const struct spa_dict_item *it;
|
const struct spa_dict_item *it;
|
||||||
int count = 0;
|
int count = 0;
|
||||||
|
char key[1024];
|
||||||
|
|
||||||
spa_dict_for_each(it, dict) {
|
spa_dict_for_each(it, dict) {
|
||||||
size_t len = it->value ? strlen(it->value) : 0;
|
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) {
|
if (it->value == NULL) {
|
||||||
fprintf(f, "null");
|
fprintf(f, "null");
|
||||||
} else if (spa_json_is_null(it->value, len) ||
|
} else if (spa_json_is_null(it->value, len) ||
|
||||||
spa_json_is_float(it->value, len) ||
|
spa_json_is_float(it->value, len) ||
|
||||||
spa_json_is_bool(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);
|
fprintf(f, "%s", it->value);
|
||||||
} else {
|
} else {
|
||||||
encode_string(f, it->value);
|
encode_string(f, it->value);
|
||||||
|
|
|
||||||
|
|
@ -100,6 +100,7 @@ pw_properties_get(const struct pw_properties *properties, const char *key);
|
||||||
const char *
|
const char *
|
||||||
pw_properties_iterate(const struct pw_properties *properties, void **state);
|
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);
|
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) {
|
static inline bool pw_properties_parse_bool(const char *value) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue