Metreeca Gear
    Preparing search index...

    Function json

    • Creates a JSON parser.

      The generated task converts a feed of JSON documents into a feed of values, one value per document, so that a consumer works on structured data rather than on text.

      A document is given either as text or as a response carrying it as its body, and is parsed on its own. A document holding no text, or only whitespace, contributes no value, as does a response carrying no body.

      Response bodies are decoded as UTF-8, the only encoding JSON is exchanged under, and bytes that are not valid UTF-8 are read as replacement characters.

      A response stating a content type that is not a JSON one, application/json or a +json format such as application/ld+json, or a charset other than UTF-8, is reported to the log and read all the same, so that a mis-declared source is diagnosed without being shut out.

      Note

      • Incremental: each value is emitted as soon as its document is drawn, so the feed produced runs dry as the feed drawn from does and an endless source is read as long as it is consumed.
      • Materialising: a document is held in memory while it is parsed, as parsing requires it as a single contiguous string, so peak memory use is about twice the size of the largest document rather than of the feed.
      • Stateless: every document is parsed on its own, so the outcome is unaffected by how the feed is split across nested feeds or runs.
      Warning

      A document that cannot be parsed is skipped and reported to the log, leaving the feed to run to completion.

      Type Parameters

      • V extends Value = Object

        The type of the value produced; the parsed document is emitted as is, without being validated against it; defaults to a JSON Object

      Returns Task<string | Response, V>

      A task converting a feed of JSON documents, given as text or as responses, into a feed of values

      Error While the feed is consumed, whatever the source reports while producing documents, or whatever reading the body of a response reports