Skip to main content
  • Home
  • login
  • Browse the archive

    swh mirror partner logo
swh logo
SoftwareHeritage
Software
Heritage
Mirror
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

  • c938f85
  • /
  • json-encoder.md
Raw File
Permalinks

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • content
  • directory
content badge Iframe embedding
swh:1:cnt:6e4c6e3126da0ad954c2ec87253690042b5d5ecc
directory badge Iframe embedding
swh:1:dir:c938f85f8c8795a42466aba0c8d80ded8e38806b
json-encoder.md
JSON Encoder
============

Approach
--------

The JSON encoder exists to support qlog implementation. There is no intention to
implement a decoder at this time. The encoder is intended to support automation
using immediate calls without the use of an intermediate syntax tree
representation and is expected to be zero-allocation in most cases. This enables
highly efficient serialization when called from QUIC code without dynamic memory
allocation.

An example usage is as follows:

```c
int generate_json(BIO *b)
{
    int ret = 1;
    JSON_ENC z;

    if (!ossl_json_init(&z, b, 0))
        return 0;

    ossl_json_object_begin(&z);
    {
        ossl_json_key(&z, "key");
        ossl_json_str(&z, "value");

        ossl_json_key(&z, "key2");
        ossl_json_u64(&z, 42);

        ossl_json_key(&z, "key3");
        ossl_json_array_begin(&z);
        {
            ossl_json_null(&z);
            ossl_json_f64(&z, 42.0);
            ossl_json_str(&z, "string");
        }
        ossl_json_array_end(&z);
    }
    ossl_json_object_end(&z);

    if (ossl_json_get_error_flag(&z))
        ret = 0;

    ossl_json_cleanup(&z);
    return ret;
}
```

The zero-allocation, immediate-output design means that most API calls
correspond directly to immediately generated output; however there is some
minimal state tracking. The API guarantees that it will never generate invalid
JSON, with two exceptions:

- it is the caller's responsibility to avoid generating duplicate keys;
- it is the caller's responsibility to provide valid UTF-8 strings.

Since the JSON encoder is for internal use only, its structure is defined in
headers and can be incorporated into other objects without a heap allocation.
The JSON encoder maintains an internal write buffer and a small state tracking
stack (1 bit per level of depth in a JSON hierarchy).

JSON-SEQ
--------

The encoder supports JSON-SEQ (RFC 7464), as this is an optimal format for
outputting qlog for our purposes.

Number Handling
---------------

It is an unfortunate reality that many JSON implementations are not able to
handle integers outside `[-2**53 + 1, 2**53 - 1]`. This leads to the I-JSON
specification, RFC 7493, which recommends that values outside these ranges are
encoded as strings.

An optional I-JSON mode is offered, in which case integers outside these ranges
are automatically serialized as strings instead.

Error Handling
--------------

Error handling is deferred to improve ergonomics. If any call to a JSON encoder
fails, all future calls also fail and the caller is expected to ascertain that
the encoding process failed by calling `ossl_json_get_error_flag`.

API
---

The API is documented in `include/internal/json_enc.h`.

ENEA — Copyright (C), ENEA. License: GNU AGPLv3+.
Legal notes  ::  JavaScript license information ::  Web API

back to top