Commit Graph

19 Commits

Author SHA1 Message Date
Jordan Hrycaj 3509706517
Lazy JSON parser (#42)
* Proper error handling when parsed number exceeds uint64

details:
  Returns an "errNonPortableInt" error

* need legacy flag for unit tests

* lazy numeric token parser

why:
  Numeric data may have a custom format. In particular,numeric data may be
  Uint256 which is not a JSON standard and might lead to an overflow.

details:
  Numeric values are assigned a preliminary token type tkNumeric without
  being fully parsed. This can be used to insert a custom parser.
  Otherwise the value is parsed implicitly when querying/fetching the
  token type.

  + tok:     replaced by getter tok() resolving lazy stuff (if necessary)
  + tokKind: current type without auto-resolving

  This lazy scheme could be extended to other custom types as long as
  the first token letter determines the custom type.

* activate lazy parsing in reader

howto:
  + no code change if a custom reader refers to an existing reader
    type FancyInt = distinct int
    proc readValue(reader: var JsonReader, value: var FancyInt) =
      value = reader.readValue(int).FancyInt

  + bespoke reader for cusom parsing
    type FancyUint = distinct uint
    proc readValue(reader: var JsonReader, value: var FancyUint) =
      if reader.lexer.lazyTok == tkNumeric:
        var accu: FancyUint
        reader.lexer.customIntValueIt:
          accu = accu * 10 + it.u256
        value = accu
      elif reader.lexer.tok == tkString:
        value = reader.lexer.strVal.parseUint.FancyUint
        ...
      reader.lexer.next

  + full code explanation at json_serialisation/reader.readValue()

* Add lazy parsing for customised string objects

why:
  This allows parsing large or specialised strings without storing it
  in the lexer state descriptor.

details:
  Similar logic applies as for the cusomised number parser. For mostly
  all practical cases, a DSL template is available serving as wrapper
  around the character/byte item processor code.

* fix typo in unit test
2022-05-05 17:33:40 +01:00
Jacek Sieka cd044da00f
clean up imports/exports (#31) 2022-02-18 10:26:15 +01:00
Zahary Karadjov 3b0c9eafa4
Bugfix: incorrect logic in the parsing of comments fails to detect EOF properly 2020-10-09 19:32:25 +03:00
Zahary Karadjov 25adda6f3f
Alternative implementation that doesn't require rewind points 2020-07-21 22:09:38 +03:00
Zahary Karadjov 4d8145c67b
WIP Deserialization of the JsonString literal fields 2020-07-17 23:58:02 +03:00
Zahary Karadjov 2f5e71b25f
Use the latest FastStreams API 2020-05-13 11:43:52 +03:00
Jacek Sieka cb695d175f reader support for stringlikes 2020-04-29 19:37:34 +03:00
Zahary Karadjov 96a337d334
stream.eof -> not stream.readable 2020-04-14 17:08:07 +03:00
Zahary Karadjov a8c5604808
Stop using the AsciiStream flavour 2020-04-13 17:01:49 +03:00
Zahary Karadjov 16931f4fa3
Use the improved InputStream API 2020-04-10 16:46:12 +03:00
Zahary Karadjov 6e9d69dafb
Handle int overflows better; Support Nim 1.2 2020-03-25 18:43:49 +02:00
Zahary Karadjov 88b79e2300
Remove warnings in Nim 1.0.2 2019-11-04 18:42:34 +00:00
andri lim a0607c6375 fix 32 bit problem 2019-08-14 15:44:59 +02:00
Zahary Karadjov 0c12b0f42d
In error messages, display the start position of strings instead of the end pos 2019-07-21 17:12:48 +03:00
Jacek Sieka f349761b76
std_shims -> stew 2019-07-07 11:46:32 +02:00
Zahary Karadjov c2daa5a23c Improved error handling and pretty-printing for arrays 2019-03-20 01:54:16 +02:00
Zahary Karadjov 49c07ca512 Handle the latest OutputStream changes 2019-03-11 11:39:19 +02:00
Zahary Karadjov edda2577a3 Handle large unsigned values and add Portable JsonMode in the Lexer 2019-01-21 19:40:14 +02:00
Zahary Karadjov 6d3fae7df2 Json deserialisation; Tests 2018-12-19 12:47:53 +02:00