sam

Fast and just works JSON-Binding for Nim (requires Jsmn)

This module is to celebrate 100 days old of Sam - my second princess

Installation

nimble install sam

Types

JsonNode = ref object
  mapper: Mapper
  pos: int
JsonRaw {...}{.borrow: `.`.} = distinct string

Procs

proc `$`(n: JsonNode): string {...}{.raises: [], tags: [].}
proc loads(target: var any; json: string; bufferSize = 256)
proc parse(json: string; bufferSize = 256): JsonNode {...}{.raises: [Exception], tags: [].}
proc parse(json: string; tokens: seq[JsmnToken]): JsonNode {...}{.raises: [], tags: [].}
Load a parsed JSON tokens and returns a JsonNode
proc addslashes(s: string; prefix = "\""; suffix = "\""): string {...}{.inline, noSideEffect,
    extern: "nsuEscape", raises: [], tags: [].}
A modified version of strutils.escape <strutils.html#escape,string,string,string>`_
proc dumps(t: auto; x: var string; namingConverter: NamingConverter = nil)
Serialize t into x
proc dumps(t: auto; namingConverter: NamingConverter = nil): string
Serialize t to a JSON formatted
proc `%`(x: auto): JsonRaw {...}{.inline.}
Convert x to a raw json string (JsonRaw is not wrapped when added to json string)
proc isObject(n: JsonNode): bool {...}{.raises: [], tags: [].}
proc isArray(n: JsonNode): bool {...}{.raises: [], tags: [].}
proc isString(n: JsonNode): bool {...}{.raises: [], tags: [].}
proc isPrimitive(n: JsonNode): bool {...}{.raises: [], tags: [].}

Funcs

func `[]`(n: JsonNode; key: string): JsonNode {...}{.noSideEffect, raises: [], tags: [].}
Get a field from a json object, raises FieldError if field does not exists
func `[]`(n: JsonNode; idx: int): JsonNode {...}{.noSideEffect, raises: [IndexError], tags: [].}
Get a field from json array, raises IndexError if array is empty or index out of bounds
func len(n: JsonNode): int {...}{.raises: [], tags: [].}
Returns the number of elements in a json array
func hasKey(n: JsonNode; key: string): bool {...}{.raises: [], tags: [].}
Checks if field exists in object
func toStr(node: JsonNode): string {...}{.inline, raises: [], tags: [].}
Retrieves the string value of a JSMN_STRING node
func toInt(node: JsonNode): int {...}{.inline, raises: [ValueError], tags: [].}
Retrieves the int value of a JSMN_PRIMITIVE node
func toFloat(node: JsonNode): float {...}{.inline, raises: [ValueError], tags: [].}
Retrieves the float value of a JSMN_PRIMITIVE node
func toBool(node: JsonNode): bool {...}{.inline, raises: [], tags: [].}
Retrieves the bool value of a JSMN_PRIMITIVE node
func toObj[T](node: JsonNode): T
Map a JSMN_OBJECT node into a Nim object

Iterators

iterator items(n: JsonNode): JsonNode {...}{.raises: [], tags: [].}
Iterator for the items of an array node
iterator pairs(n: JsonNode): tuple[key: string, val: JsonNode] {...}{.raises: [], tags: [].}
Iterator for the child elements of an object node

Macros

macro `$$`(x: untyped): untyped
Convert anything to a json string