jsonlines

This is a simple library which implements a simple JSON Lines parser (also known as NDJSON or newline-delimited JSON).

See: https://neroist.github.io/jsonlines

Procs

proc `$`(jsonl: JsonLines): string {....raises: [KeyError], tags: [].}
Convert JsonLines into a string
proc `[]=`(jsonl: JsonLines; idx: int; val: JsonNode | BackwardsIndex)
Assign JSON node in JsonLines jsonl at index idx
jsonl:The JsonLines object to get the node from
idx:The index of the JsonNode to retrieve
val:The node to assign the index to
proc `[]`(jsonl: JsonLines; idx: int | BackwardsIndex): JsonNode
Get JSON node in JsonLines jsonl at index idx
jsonl:The JsonLines object to get the node from
idx:The index of the JsonNode to retrieve
proc add(jsonl: JsonLines; node: JsonNode) {....raises: [], tags: [].}
Add JsonNode to JsonLines jsonl
jsonl:The JsonLines object to add the node to
node:The JsonNode to add
proc len(jsonl: JsonLines): int {....raises: [], tags: [].}
Get the number of nodes in JsonLines jsonl
proc parseJsonLines(buffer: string; rawIntegers = false; rawFloats = false;
                    ignoreEmptyLines: bool = true): JsonLines {.
    ...raises: [IOError, OSError, JsonParsingError, ValueError, Exception],
    tags: [ReadIOEffect, WriteIOEffect].}

Parses JSON Lines from buffer.

If buffer contains extra data, it will raise JsonParsingError.

The rawIntegers and rawFloats parameters are the same as the ones in parseJson

Note: On the JS backend, these parameters are ignored.
buffer:The string of Json Lines data to parse
rawIntegers:If true, integer literals will not be converted to a JInt field but kept as raw numbers via JString.
rawFloats:If is true, floating point literals will not be converted to a JFloat field but kept as raw numbers via JString.
ignoreEmptyLines:Whether or not to ignore empty lines in the buffer.
proc parseJsonLines(s: Stream; filename: string = "input"; rawIntegers = false;
                    rawFloats = false; ignoreEmptyLines: bool = true): JsonLines {.
    ...raises: [IOError, OSError, JsonParsingError, ValueError, Exception],
    tags: [ReadIOEffect, WriteIOEffect].}

Parses from a stream s into JsonLines. filename is only needed for nice error messages.

If s contains extra data, it will raise JsonParsingError.

The rawIntegers and rawFloats parameters are the same as the ones in parseJson

Note: On the JS backend, these parameters are ignored.
s:The stream of Json Lines data to parse
rawIntegers:If true, integer literals will not be converted to a JInt field but kept as raw numbers via JString.
rawFloats:If is true, floating point literals will not be converted to a JFloat field but kept as raw numbers via JString.
ignoreEmptyLines:Whether or not to ignore empty lines in the stream s.
proc parseJsonLinesFile(filename: string; rawIntegers = false;
                        rawFloats = false; ignoreEmptyLines: bool = true): JsonLines {.
    ...raises: [IOError, OSError, JsonParsingError, ValueError, Exception],
    tags: [ReadIOEffect, WriteIOEffect].}

Parses file into JsonLines.

If file contains extra data, it will raise JsonParsingError.

Warning: This proc is not defined for the JS backend
proc pretty(jsonl: JsonLines; indent: int = 2): string {....raises: [KeyError],
    tags: [].}

Prettifies JsonLines jsonl by making it easier to view.

However, this results in invalid JsonLines, unable to be parsed.

jsonl:The JsonLines to prettify (or beautify)
indent:How muuch to indent, in spaces
proc toJArray(jsonl: JsonLines): JsonNode {....raises: [], tags: [].}
Convert JsonLines to a JArray
proc toJsonLines(nodes: openArray[JsonNode]): JsonLines {....raises: [], tags: [].}
Convert open array of JsonNodes to JsonLines

Iterators

iterator items(jsonl: JsonLines): JsonNode {....raises: [], tags: [].}
iterator jsonLines(buffer: string; rawIntegers = false; rawFloats = false;
                   ignoreEmptyLines: bool = true): JsonNode {.
    ...raises: [IOError, OSError, JsonParsingError, ValueError, Exception],
    tags: [ReadIOEffect, WriteIOEffect].}

Convinience iterator to iterate through the JSON values in JsonLines document buffer

The rawIntegers and rawFloats parameters are the same as the ones in parseJson

Note: On the JS backend, these parameters are ignored.
buffer:The string of Json Lines data to parse
rawIntegers:If true, integer literals will not be converted to a JInt field but kept as raw numbers via JString.
rawFloats:If is true, floating point literals will not be converted to a JFloat field but kept as raw numbers via JString.
ignoreEmptyLines:Whether or not to ignore empty lines in the buffer.
iterator mitems(jsonl: var JsonLines): JsonNode {....raises: [], tags: [].}
iterator mpairs(jsonl: var JsonLines): tuple[idx: int, node: var JsonNode] {.
    ...raises: [], tags: [].}
iterator pairs(jsonl: JsonLines): tuple[idx: int, node: JsonNode] {....raises: [],
    tags: [].}