w3c / json-ld-api

JSON-LD 1.1 Processing Algorithms and API Specification
https://w3c.github.io/json-ld-api/
Other
73 stars 29 forks source link

It is not clear enough what "JSON Serialization" is #605

Open anatoly-scherbakov opened 6 days ago

anatoly-scherbakov commented 6 days ago

Context

https://github.com/w3c/json-ld-api/issues/579 (for JSON-LD 1.1 API) or https://github.com/json-ld/yaml-ld/issues/143 (for YAML-LD, which derives from JSON-LD) seem to stem from a common issue: a reader of the API specification might be unclear about what is meant under "JSON Serialization" in the spec.

§ 1.5 Example Conventions says:

Note that in the examples used in this document, output is of necessity shown in serialized form as JSON. While the algorithms describe operations on the JSON-LD internal representation, when they as displayed as examples, the JSON serialization is used. In particular, the internal representation use of maps are represented using JSON objects.

A programmer will likely expect that JSON Serialization is a string. For instance, Working With JSON Data in Python → Serializing JSON says:

What happens after a computer processes lots of information? It needs to take a data dump. Accordingly, the json library exposes the dump() method for writing data to files. There is also a dumps() method (pronounced as “dump-s”) for writing to a Python string.

The explanation in the API will not convince the reader otherwise. § 9.1 The JsonLdProcessor Interfaceexpand() instructs the implementation, at item 9), to

Resolve the promise with expanded output transforming expanded output from the internal representation to a JSON serialization.

Thus, expand() output type must be a String. Right?

No. Because the same § 9.1 The JsonLdProcessor Interface section, in its IDL block, specifies:

static Promise<sequence<JsonLdRecord>> expand(
    JsonLdInput input,
    optional JsonLdOptions options = {});

In fact, the expand() return value is a sequence of JsonLdRecord objects, which probably are represented as map (or as an object, or as a dict) in the programming language powering an implementation.

Alternatives

Also

I think we need to also very explicitly explain the difference among

with an example.

Decision

(TODO: subject to discussion)

In the placeholder above, I will summarize what we've come up with, for future reference.

What do you think? :thinking:

gkellogg commented 5 days ago

We definitely need to clean this up. Probably, clarify that the API methods return the JsonLdRecord (or sequence of JsonLdRecord) which is the WebIDL description of the internal representation, although internal representation includes map, array, number, or JSON Literal.

Probably something in the API section that generally says that the default serialization of the resulting internal representation is JSON and that processors MUST return this value when application/json or a subtype is expected.