26 Commits

Author SHA1 Message Date
George Kiagiadakis
64c233f3f4 spa-json: wrap the data instead of the spa_json* in _parser_get_json()
This allows the returned WpSpaJson object to be kept around
after the parser has advanced to the next token. The behaviour
of the _new_wrap() function is to wrap the underlying spa_json*
and it breaks as soon as the parser advances.
2024-03-04 07:07:56 +00:00
George Kiagiadakis
d07b6188e5 spa-json: add new "undefined" parser constructor
This allows reading non-standard JSON data that is not a valid JSON
object or array, but can be treated as such.
2024-03-04 07:07:56 +00:00
George Kiagiadakis
42b64bfc28 spa-json: rename _from_string() to _wrap_string() and add new "from" variants
The previous naming convention was confusing because it did not make
it explicit that the string is not being copied. We had this wrong already
in the Lua bindings and thanks to some miracle it hasn't backfired so far
(it was using the "wrap" behaviour with a string that doesn't stay alive).

In some places we actually need the "copy" behaviour and in some other
places we need the "wrap" behaviour, so let's have both variants available.
2023-11-14 12:36:10 +02:00
George Kiagiadakis
67f9f63520 Merge branch '0.4' into next 2023-09-01 19:12:20 +03:00
Tom A. Wagner
6e81dc7a66 docs: Add missing '\since' attributes to types and functions that have been added in versions later than 0.4.0 2023-08-30 11:20:06 +02:00
George Kiagiadakis
a0497b4256 Merge branch '0.4' into next 2023-06-21 20:59:04 +03:00
Julian Bouzas
e24e52c6d3 spa-json: encode the string directly into the builder
Some special characters like '\v' are encoded using 6 characters, which
currently does not work because the VLA size asumes a maximum of 4 characters
per encoded special character. This patch refactors this logic to avoid using
VLAs at all and encodes the string directly into the builder.

See #471
2023-06-19 12:03:23 -04:00
Julian Bouzas
e91a9583d8 spa-json: Fix conditional jump on uninitialised value when adding empty strings to builder
Similar to wp_spa_json_builder_add_property(), we need to make sure the dst
array in wp_spa_json_builder_add_string() has room for the null character
because builder_add() expects it.

Fixes #471
2023-06-19 08:07:02 -04:00
George Kiagiadakis
4736d56557 log: implement a log topics system, like pipewire
The intention is to make checks for enabled log topics faster.

Every topic has its own structure that is statically defined in the file
where the logs are printed from. The structure is initialized transparently
when it is first used and it contains all the log level flags for the levels
that this topic should print messages. It is then checked on the wp_log()
macro before printing the message.

Topics from SPA/PipeWire are also handled natively, so messages are printed
directly without checking if the topic is enabled, since the PipeWire and SPA
macros do the checking themselves.

Messages coming from GLib are checked inside the handler.

An internal WpLogFields object is used to manage the state of each log
message, populating all the fields appropriately from the place they
are coming from (wp_log, spa_log, glib log), formatting the message and
then printing it. For printing to the journald, we still use the glib
message handler, converting all the needed fields to GLogField on demand.
That message handler does not do any checks for the topic or the level, so
we can just call it to send the message.
2023-05-16 20:42:28 +03:00
Julian Bouzas
9f2168e022 spa-json: add _add_from_string() and _add_from_stringn() builder APIs 2023-04-17 07:47:09 -04:00
Ashok Sidipotu
229f5d0630 spa-json: add _is_container () API 2023-04-17 07:44:19 -04:00
Julian Bouzas
10f85549a4 spa-json: make sure we only add encoded string data
The spa_json_encode_string() API does not add a null terminator character. We
need to use the return value to know the size of the encoded string when adding
it to the builder.
2023-01-04 10:07:36 -05:00
Julian Bouzas
55691a0d81 spa-json: always advance to the end of the nested array/object
Fixes size issue with nested objects when they are the last property value of
the parent object.
2022-08-28 10:50:25 -04:00
George Kiagiadakis
11902666f9 wp_spa_json_to_string: add since marker 2022-06-27 17:28:36 +03:00
Julian Bouzas
b6e8e7e064 spa-json: encode string in wp_spa_json_new_string() 2022-06-27 10:07:16 +00:00
Julian Bouzas
0996f5a5ca spa-json: add wp_spa_json_to_string() API
Since the string length returned by wp_spa_pod_get_data() does not always match
the size of the actual json object because it is stored in contiguous memory, we
cannot always rely on that API to get the json string data.

The new wp_spa_pod_to_string() always allocates a new string with the same
length as the json size, guaranteeing that the string returned always represents
the json object, regardless of whether it is nested or not. It is always
recommented to use wp_spa_pod_to_string() unless you know what you are doing.
2022-06-27 10:07:16 +00:00
Julian Bouzas
a19c7f3d2f spa-json: only add the json data represented by its size
The data pointer of a WpSpaJson object can be bigger than the actual WpSpaJson
size. For example, this happens when iterating an array: instead of
re-allocating the nested data into a new region of memory, the iterator uses
the same region of memory as the parent, and just sets the pointer data and size
to the correct location when creating the nested WpSpaJson object.

This patch makes sure only the data represented by the size is added into the
builder when adding a json object, fixing issues that were happening before when
adding nested objects directly from a second WpSpaJson object.
2022-06-27 10:07:16 +00:00
Julian Bouzas
e9be326954 spa-json: add _new_from_stringn() API 2022-03-24 04:35:12 -04:00
Julian Bouzas
a68a7cec0e spa-json: allow parsing unquoted keys when getting object properties
Unquoted strings are used in wireplumber and pipewire configuration files.
2022-03-24 07:21:25 +00:00
George Kiagiadakis
8d4fe81ebd spa-json: allocate enough space for parsing strings without looping
The string size is always less or equal to the size of the token (len),
so by allocating len+1 bytes we have enough space to write the whole
token and the null byte, if needed.
2022-03-16 21:11:09 +02:00
Ashok Sidipotu
2248b8ec1f spa-json: Parse unquoted string literals
- pipewire supports parsing unquoted strings and they are used
  extensively in JSON .conf files.
- Extend this support into wireplumber, as it wants to use the pipewire
  like syntax in its JSON .conf files.
2022-03-16 20:51:49 +02:00
Julian Bouzas
2a6902a634 spa-json: fix format and remove unneded WP_API 2022-02-15 16:19:47 -05:00
Julian Bouzas
e429db7e8c spa-json: fix va_list APIs for different architectures
The va_list type might not always be a pointer in some architectures, so we
cannot guarantee it will be modified after using it for a second time in another
function. This fixes the issue by using macros so args does not get copied, and
always gets modified when using it more than once.
2022-02-09 07:59:59 -05:00
Julian Bouzas
585ad72c7c spa-json: add null character when finishing building json 2022-02-01 16:07:12 +00:00
Julian Bouzas
46ab414a04 spa-json: fix coverity scan issues 2022-01-25 21:18:16 -05:00
Julian Bouzas
4c1574117a spa-json: add new WpSpaJson API 2022-01-25 07:20:35 -05:00