Commit Graph

61 Commits

Author SHA1 Message Date
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
gabrielmer 230e226da0
fix: amend incorrect int bound check (#65) 2023-11-24 17:18:23 +01:00
Jacek Sieka bb53d49caf
missing raises (#61) 2023-07-06 09:13:29 +02:00
tersec e32e8ad916
import std/[enumutils, typetraits] directly, not via stew/shims (#59) 2023-06-11 12:14:50 +00:00
jangko aa44ee61dd
works with nim devel 2023-06-05 15:23:36 +07:00
Etan Kissling ed4440d881
use string value when encoding enums (#55)
Currently, we encode enum values always as the numeric value `ord(val)`.
unless explicitly overridden using `serializesAsTextInJson` or with a
custom `writeValue` implementation. Reduce verbosity by automatically
doing that.
2023-05-27 12:42:08 +03:00
jangko f64d55f7ff
fix missing annotation for nim 2.0 2023-04-17 09:39:52 +07:00
Zahary Karadjov b42cd3c0d9
Make Reader.skipSingleJsValue part of the public API 2022-11-23 13:32:11 +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 97cf184119
Rename some definitions to avoid clashes with stew/bitops2 2022-07-14 16:05:55 +03:00
Zahary Karadjov aca66fbd8e
bugfix: even the Option fields were mandatory under requireAllFields 2022-07-14 15:14:10 +03:00
Zahary Karadjov dbe0f1ae51
Remove a string-to-cstring conversion warning on Nim 1.6 2022-06-19 13:06:02 +03:00
Zahary Karadjov 5034fef8d0
Avoid a dangerous string to cstring conversion 2022-06-07 12:35:20 +03:00
tersec 7d3d339e73
rm TaintedString (#43) 2022-06-04 08:58:13 +02: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 461fd03edb
Better default JsonReaderError.formatMsg implementation 2022-03-09 18:24:02 +02:00
Jacek Sieka a12beabd2f
Revert "add missed exceptions (#36)" (#39)
This reverts commit 232710f5b2.
2022-02-21 10:29:04 +01:00
jangko ea965bbe00
silence spurious warning: 'ReaderType' is declared but not used 2022-02-18 22:57:58 +07:00
Jacek Sieka cd044da00f
clean up imports/exports (#31) 2022-02-18 10:26:15 +01:00
Antonis Geralis 232710f5b2
add missed exceptions (#36)
* add missed exceptions

* Update json_serialization/reader.nim

Co-authored-by: Jacek Sieka <arnetheduck@gmail.com>
2022-02-18 10:24:20 +01:00
Kim De Mey c5f0e2465e
Add gcsafe and raises annotations to the formatMsg methods (#33) 2021-11-18 15:24:59 +01:00
Zahary Karadjov 4f3775ddf4 Add JsonReader.init(..., requireAllFields: bool) 2021-08-11 20:01:47 +03:00
Zahary Karadjov fe8a82ca76
Adapt to the latest nim-serialization API 2021-03-19 04:13:55 +02:00
Zahary Karadjov 706eb5740e
Add support for serialization flavors 2021-03-18 13:01:06 +02:00
Zahary Karadjov 32f75d93b0
Add support for TaintedString 2020-11-12 21:00:10 +02:00
Zahary Karadjov 1dccd4b2ef
Another readObjectFields convenience helper 2020-08-02 20:15:44 +03:00
Zahary Karadjov ec374f26ca
Break reader.readObject into smaller reusable pieces 2020-08-02 13:22:24 +03:00
Zahary Karadjov 9ca88fdcd4
Turn a potential assertion failure into a syntax error 2020-08-01 19:28:36 +03:00
Zahary Karadjov d5eb9427b8
Serialization of JsonNode fields 2020-07-24 23:16:35 +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 1cf51931f1
Implement forward-compatible deserialization 2020-06-30 22:11:56 +03:00
Zahary Karadjov 463a0cf1b7
Address review comments; Passing test suite 2020-06-22 17:42:50 +03:00
Zahary Karadjov 6e92113a06
Stricter error handling 2020-06-16 01:03:00 +03:00
Zahary Karadjov 8d4c31f7c2
Allow custom serializers to report unexpected values in the Json file 2020-05-30 22:40:17 +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 bed0e403a4
Add support for not nil ref types 2020-04-23 01:39:56 +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
Zahary Karadjov f4bd22e054 Support for tuples 2019-08-14 16:18:42 +02:00
Alexander Ivanov e49b8bdb43 Implement support for normal and ordered tables 2019-08-02 13:33:44 +03:00
Zahary Karadjov 997ae368a5
Add a test case for case objects 2019-08-01 17:12:31 +03: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
Zahary Karadjov 95bdb8acc1
Export more procs useful in low-level deserializers 2019-07-19 02:01:34 +03:00
Zahary Karadjov 73d9e0d458
Handle changes in the serialization APIs 2019-07-18 21:20:17 +03:00
Zahary Karadjov 7922a83c4d
Serialization for sets 2019-07-16 13:20:05 +03:00
Jacek Sieka e127b49cd4
fix typo 2019-06-28 09:50:51 +02:00