Module jsmn

This module is a fork of Jsmn - a world fastest JSON parser/tokenizer - in pure Nim

It also supports PARENT LINKS and STRICT MODE, you can enable them via command line switch

Installation

nimble install jsmn

Usage

import jsmn

const
  json = """{
    "user": "johndoe",
    "admin": false,
    "uid": 1000,
    "groups": ["users", "wheel", "audio", "video"]}"""

var
  tokens: array[32, JsmnToken] # expect not more than 32 tokens

let r = parseJson(json, tokens)

for i in 1..r:
  var token = addr tokens[i]
  echo "Kind: ", token.kind
  echo "Value: ", json[token.start..<token.stop]

Types

JsmnKind = enum
  JSMN_UNDEFINED,             ## Undefined
  JSMN_OBJECT,                ## Object, tuple
  JSMN_ARRAY,                 ## Array, seq
  JSMN_STRING,                ## String
  JSMN_PRIMITIVE              ## Other primitive: number, boolean (true/false) or null
JSON type identifier
JsmnToken = object
  kind*: JsmnKind
  start*: int                  ## start position in JSON data string
  stop*: int                   ## end position in JSON data string
  size*: int
  when defined(JSMN_PARENT_LINKS):
      parent*: int

  
JSON token description.
JsmnException = object of ValueError
JsmnNotEnoughTokensException = object of JsmnException
  
not enough tokens, JSON string is too large
JsmnNotEnoughJsonDataException = object of JsmnException
  
JSON string is too short, expecting more JSON data

Procs

proc parseJson(json: string; tokens: var openArray[JsmnToken]): int {.raises: [
    JsmnNotEnoughTokensException, JsmnBadTokenException,
    JsmnNotEnoughJsonDataException], tags: [].}
Parse a JSON data string into and array of tokens, each describing a single JSON object.
proc parseJson(json: string): seq[JsmnToken] {.
    raises: [JsmnNotEnoughJsonDataException, JsmnBadTokenException], tags: [].}
Parse a JSON data and returns a sequence of tokens