Commit Graph

12 Commits

Author SHA1 Message Date
andri lim b14f5b58e9
Deconvolute optional fields writer (#77) 2024-01-17 07:48:42 +07:00
jangko 9c74b885ea
Writer produce correct top-level or in-array optional elem when custom flavor omit optional fields 2024-01-11 16:51:00 +07:00
jangko 61bae43e01
Improve lexer flexibility 2023-12-26 16:03:46 +07:00
jangko 34dc825066
Add copyright year to source file 2023-12-25 22:55:07 +07:00
zah f42567c00c
Basic support for Json flavours without default object serialization (#66)
Other changes:

* Migrate many procs accepting JsonReader to JsonLexer in order to
  reduce the number of generic instantiations and the resulting code
  bloat
2023-12-19 12:00:24 +02:00
Etan Kissling 85b7ea093c
add `{.raises.}` annotation to `writeValue` (#64)
Tag `writeValue` overrides with `{.raises: [IOError].}`.

The override in `writer.nim` also needs `gcsafe` to support recursion
in Nim 2.0.
2023-08-19 13:47:32 +02:00
Tanguy e5b18fb710
bugfix: a leading field with a 'none' value was producing an incorrect encoding (#50)
The field was omitted, but not the comma following it, resulting in an
encoding such as '{, otherFields: ...}'
2022-07-15 10:23:35 +00:00
Zahary Karadjov bedbe6595a
Avoid overloading the separate classes of writeField ops 2022-06-19 12:38:44 +03:00
Zahary Karadjov 95399caff3
Use latest nim-serialization; Add tests for useCustomSerialization 2022-06-18 13:34:04 +03:00
zah b9af0be99d
Don't write empty optional fields (#47) 2022-06-16 17:14:00 +03:00
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
Zahary Karadjov 9d909d352a Add support and tests for Option and reference types 2019-06-24 17:38:37 +03:00