Flexible JSON serialization not relying on run-time type information
Go to file
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
.github/workflows CI: test with multiple Nim versions (#35) 2022-01-06 20:10:03 +01:00
json_serialization Lazy JSON parser (#42) 2022-05-05 17:33:40 +01:00
tests Lazy JSON parser (#42) 2022-05-05 17:33:40 +01:00
.appveyor.yml revert, just in case some action-at-a-distance thing is causing Unhandled exception: assignment to discriminant changes object branch; compile with -d:nimOldCaseObjects for a transition period [FieldError] 2022-04-07 10:51:02 +00:00
.gitignore add github action 2020-12-24 16:04:58 +07:00
.travis.yml revert, just in case some action-at-a-distance thing is causing Unhandled exception: assignment to discriminant changes object branch; compile with -d:nimOldCaseObjects for a transition period [FieldError] 2022-04-07 10:51:02 +00:00
LICENSE-APACHEv2 licenses 2019-06-18 19:52:12 +02:00
LICENSE-MIT licenses 2019-06-18 19:52:12 +02:00
README.md CI: refactor Nim compiler caching (#28) 2021-06-03 02:16:30 +02:00
json_serialization.nim Adapt to the latest nim-serialization API 2021-03-19 04:13:55 +02:00
json_serialization.nimble Lazy JSON parser (#42) 2022-05-05 17:33:40 +01:00

README.md

nim-json-serialization

Build Status (Travis) Windows build status (Appveyor) License: Apache License: MIT Stability: experimental Github action

Flexible JSON serialization not relying on run-time type information.

License

Licensed and distributed under either of

or

at your option. These files may not be copied, modified, or distributed except according to those terms.