# parsejson

- マニュアル
- 標準ライブラリ
- 索引

検索
分類:

This module implements a json parser. It is used and exported by the `json` standard library module, but can also be used in its own right.

# インポート

strutils, lexbase, streams, unicode

# 型

    JsonEventKind = enum
      jsonError,                  ## an error occurred during parsing
      jsonEof,                    ## end of file reached
      jsonString,                 ## a string literal
      jsonInt,                    ## an integer literal
      jsonFloat,                  ## a float literal
      jsonTrue,                   ## the value ``true``
      jsonFalse,                  ## the value ``false``
      jsonNull,                   ## the value ``null``
      jsonObjectStart,            ## start of an object: the ``{`` token
      jsonObjectEnd,              ## end of an object: the ``}`` token
      jsonArrayStart,             ## start of an array: the ``[`` token
      jsonArrayEnd                ## start of an array: the ``]`` token

  enumeration of all events that may occur when parsing Source Edit

    TokKind = enum
      tkError, tkEof, tkString, tkInt, tkFloat, tkTrue, tkFalse, tkNull, tkCurlyLe,
      tkCurlyRi, tkBracketLe, tkBracketRi, tkColon, tkComma

  ソース 編集

    JsonError = enum
      errNone,                    ## no error
      errInvalidToken,            ## invalid token
      errStringExpected,          ## string expected
      errColonExpected,           ## ``:`` expected
      errCommaExpected,           ## ``,`` expected
      errBracketRiExpected,       ## ``]`` expected
      errCurlyRiExpected,         ## ``}`` expected
      errQuoteExpected,           ## ``"`` or ``'`` expected
      errEOC_Expected,            ## ``*/`` expected
      errEofExpected,             ## EOF expected
      errExprExpected             ## expr expected

  enumeration that lists all errors that can occur Source Edit

    JsonParser = object of BaseLexer
      a*: string
      tok*: TokKind
      kind: JsonEventKind
      err: JsonError
      state: seq[ParserState]
      filename: string
      rawStringLiterals: bool

  the parser object. ソース 編集

    JsonKindError = object of ValueError

  raised by the `to` macro if the JSON kind is incorrect. ソース 編集

    JsonParsingError = object of ValueError

  is raised for a JSON error Source Edit

# 定数

    errorMessages: array[JsonError, string] = ["no error", "invalid token",
                                           "string expected", "\':\' expected",
                                           "\',\' expected", "\']\' expected",
                                           "\'}\' expected",
                                           "\'\"\' or \"\'\" expected",
                                           "\'*/\' expected", "EOF expected",
                                           "expression expected"]

  ソース 編集

# プロシージャ

    proc open(my: var JsonParser; input: Stream; filename: string; rawStringLiterals = false) {...}{.
        raises: [Defect, IOError, OSError], tags: [ReadIOEffect].}

  initializes the parser with an input stream. `Filename` is only used for nice error messages. If `rawStringLiterals` is true, string literals are kept with their surrounding quotes and escape sequences in them are left untouched too. ソース 編集

    proc close(my: var JsonParser) {...}{.inline, raises: [Exception, IOError, OSError],
                                 tags: [WriteIOEffect].}

  closes the parser `my` and its associated input stream. ソース 編集

    proc str(my: JsonParser): string {...}{.inline, raises: [], tags: [].}

  returns the character data for the events: `jsonInt`, `jsonFloat`, `jsonString` Source Edit

    proc getInt(my: JsonParser): BiggestInt {...}{.inline, raises: [ValueError], tags: [].}

  returns the number for the event: `jsonInt` Source Edit

    proc getFloat(my: JsonParser): float {...}{.inline, raises: [ValueError], tags: [].}

  returns the number for the event: `jsonFloat` Source Edit

    proc kind(my: JsonParser): JsonEventKind {...}{.inline, raises: [], tags: [].}

  returns the current event type for the JSON parser Source Edit

    proc getColumn(my: JsonParser): int {...}{.inline, raises: [], tags: [].}

  get the current column the parser has arrived at. ソース 編集

    proc getLine(my: JsonParser): int {...}{.inline, raises: [], tags: [].}

  get the current line the parser has arrived at. ソース 編集

    proc getFilename(my: JsonParser): string {...}{.inline, raises: [], tags: [].}

  get the filename of the file that the parser processes. ソース 編集

    proc errorMsg(my: JsonParser): string {...}{.raises: [ValueError], tags: [].}

  returns a helpful error message for the event `jsonError` Source Edit

    proc errorMsgExpected(my: JsonParser; e: string): string {...}{.raises: [ValueError],
        tags: [].}

  returns an error message "`e` expected" in the same format as the other error messages Source Edit

    proc parseEscapedUTF16(buf: cstring; pos: var int): int {...}{.raises: [], tags: [].}

  ソース 編集

    proc getTok(my: var JsonParser): TokKind {...}{.raises: [Defect, IOError, OSError],
                                          tags: [ReadIOEffect].}

  ソース 編集

    proc next(my: var JsonParser) {...}{.raises: [Defect, IOError, OSError], tags: [ReadIOEffect].}

  retrieves the first/next event. This controls the parser. ソース 編集

    proc raiseParseErr(p: JsonParser; msg: string) {...}{.noinline, noreturn,
        raises: [JsonParsingError, ValueError], tags: [].}

  raises an `EJsonParsingError` exception. ソース 編集

    proc eat(p: var JsonParser; tok: TokKind) {...}{.raises: [Defect, IOError, OSError,
        JsonParsingError, ValueError], tags: [ReadIOEffect].}

  ソース 編集