Commit Graph

116 Commits

Author SHA1 Message Date
Jacek Sieka 6eadb6e939
check length before converting to range type (#102)
The index type of an array is a `range` meaning that converting an
out-of-bounds value to it will raise a `Defect`.

This PR fixes the defect but does nothing for arrays which are not
"full" - probably, this should become an error.

* bump version
2024-10-19 11:28:17 +02:00
andri lim 96fcb658b4
Fix import stew/results to pkg/results (#101) 2024-10-15 18:24:47 +07:00
metagn 9110cc8d7a
fix stylecheck for BiggestUInt (#99)
* fix reader_desc.nim

* fix test_serialization.nim

* workaround segfault on C++ related to unittest2

* link to nim issue as comment
2024-10-10 12:26:09 +00:00
Jacek Sieka ab1a061756
avoid threadvar for field table (#98)
* avoid threadvar for field table

* bump
2024-09-30 15:21:02 +02:00
Jacek Sieka b83739b430
allow serializing distinct arrays (#97)
A complement to
https://github.com/status-im/nim-json-serialization/pull/93
2024-09-26 16:19:22 +02:00
andri lim 8a4ed98bbd
Add compile time switch to alter encoder enum representation (#95)
* Add compile time switch to alter encoder enum representation

* Fix test

* Add enum section in README.md

Add more tests and encoder features of enums

* Add enums section to README.md
2024-07-27 07:27:11 +07:00
Etan Kissling 89f7be1783
extend automatic serialization support for `distinct` in Nim 2 (#93)
In Nim 2, `distinct` values no longer match `value is object` but need
to be checked separately with `value is distinct`. The underlying type
can be unwrapped with `distinctBase` to inspect whether this is a value
of type `distinct object` or `distinct` something-else.
2024-07-03 16:11:05 +02:00
andri lim 4d0b0662ed
Workaround Nim regression #23611 related to dirty template (#90) 2024-05-20 22:12:03 +07:00
andri lim 1ac1d69f95
Add JsonValueRef compare operation (#89) 2024-03-20 21:32:43 +07:00
jangko 7516a92eb8
Fix JsonValueRef.Object comparison 2024-02-29 13:58:51 +07:00
Etan Kissling 56c788bb25
avoid `XDeclaredButNotUsed` when no field `isnot JsonVoid` (#87)
Move the type definitions for `Writer` and `Flavor` closer to their
usage to avoid verbose hints when they are never used in an invocation.
2024-02-28 22:01:21 +00:00
Jacek Sieka c869dae884
include flavor in missing `readValue` error (#86) 2024-02-20 20:19:02 +01:00
jangko 224fd6efc9
Fix missing std/strutils import for parseEnum 2024-01-27 16:32:14 +07:00
tersec 6b3c9b5e43
rm deprecated ValidIpAddress support (#80) 2024-01-27 15:38:33 +07:00
andri lim d9394dc728
Resilience against null fields (#78)
* Resilience against fields with null value

* writeField helper also handle optional fields correctly

* Use uint4 for test_parser's parseValue

* Add parseObjectWithoutSkip and parseObjectSkipNullFields
2024-01-17 13:39:29 +07:00
andri lim b14f5b58e9
Deconvolute optional fields writer (#77) 2024-01-17 07:48:42 +07:00
jangko 0db6307eb3
Add flavor feature: reader will skip null fields 2024-01-15 10:31:19 +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 6f639dd6c1
Restore accidentally deleted raises pragma 2023-12-29 12:36:09 +07:00
jangko 6417b7656b
Reduce declared but not used warning 2023-12-29 12:34:39 +07:00
jangko 547604004d
Fix JsonValueRef nil comparison 2023-12-29 12:33:58 +07:00
jangko ce214b000d
Improve nested structure depth limit check 2023-12-29 12:31:56 +07:00
jangko 61a03e20d3
Add JsonVoid, JsonNumber, and JsonValueRef to JsonWriter 2023-12-27 11:58:02 +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 1693db7a57
Restore the reader.skipSingleJsValue API (#67) 2023-12-19 14:34:50 +02: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
gabrielmer 230e226da0
fix: amend incorrect int bound check (#65) 2023-11-24 17:18:23 +01: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
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
tersec 5a7f9a86cb
fix Nim 1.6 deprecation warning (#48) 2022-07-01 17:48:29 +00: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 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
Zahary Karadjov cc5038e0e2
Add writer.stepwiseArrayCreation 2022-06-17 20:16:28 +03:00
zah b9af0be99d
Don't write empty optional fields (#47) 2022-06-16 17:14:00 +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